- 已加入
- 2019年11月16日
- 留言内容
- 12
- 编程经验
- 1-3
可以说我有一个2x2的正方形。当我向上滑动手指时,我希望它在Y轴上变大,而在X轴方向上变小..当我向下滑动手指时,我希望它在Y轴上变小而在X轴上变大。
我有一些代码,但我不喜欢它的工作方式。有人可以帮助我创造更好的东西吗?
我有一些代码,但我不喜欢它的工作方式。有人可以帮助我创造更好的东西吗?
使用System.Collections;
使用UnityEngine.Android;
using UnityEngine;
使用System.Diagnostics;
公共课ScaleChanger:MonoBehaviour
{
私人刚体rb;
私人流通量
public float sens;
私人Vector3规模;
私人浮动deltaYX;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
如果(Input.touchCount> 0)
{
触摸touch = Input.GetTouch(0);
Vector3 touchPos =(touch.position * Time.fixedDeltaTime);
switch (touch.phase)
{
案例TouchPhase.Began:
deltaY = touchPos.y-transform.localScale.y;
UnityEngine.Debug.Log("transform.localScale.y ="+ transform.localScale.y +" touchPos.y="+touchPos.y+" deltaY="+deltaY);
break;
大小写TouchPhase.Moved:
scale = new Vector3(Mathf.Clamp((deltaY-touchPos.y)+1 + transform.localScale.x,1,5),Mathf.Clamp(touchPos.y-deltaY,1,4),1);
UnityEngine.Debug.Log("x= "+((deltaY-touchPos.y)+ 1 + transform.localScale.x)+"y="+(touchPos.y-deltaY));
transform.localScale =比例尺;
break;
}
}
}
}
使用System.Collections;
使用UnityEngine.Android;
using UnityEngine;
使用System.Diagnostics;
公共课ScaleChanger:MonoBehaviour
{
私人刚体rb;
私人流通量
public float sens;
私人Vector3规模;
private float distance;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
如果(Input.touchCount> 0)
{
触摸touch = Input.GetTouch(0);
Vector3 touchPos =(touch.position * Time.fixedDeltaTime);
scale = new Vector3(transform.localScale.x, transform.localScale.y, 2);
switch (touch.phase)
{
案例TouchPhase.Began:
//deltaY is the starting point of your finger
deltaY = touchPos.y;
break;
大小写TouchPhase.Moved:
//finger down
if (deltaY - 3 > touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5), 2);
}
//finger up
else if (deltaY + 3 <= touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5), 2);
UnityEngine.Debug.Log("ok 3");
}
//finger down
else if (deltaY - 2 > touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 2 - touchPos.y) / (sens * 5), 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 2 - touchPos.y) / (sens * 5), 1, 5), 2);
}
//finger up little
else if (deltaY + 2 <= touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 2 - touchPos.y) * -1) / (sens * 5), 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 2 - touchPos.y) * -1) / (sens * 5), 1, 5), 2);
UnityEngine.Debug.Log("ok 2");
}
//finger down very little
else if (deltaY - 1.5 > touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 1.5f - touchPos.y) / (sens * 10), 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 1.5f - touchPos.y) / (sens * 10), 1, 5), 2);
}
//finger up very little
else if (deltaY + 1.5 <= touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 1.5f - touchPos.y) * -1) / (sens * 10), 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 1.5f - touchPos.y) * -1) / (sens * 10), 1, 5), 2);
UnityEngine.Debug.Log("ok 1");
}
/* deltaY live movement try (not working properly)*/
//finger up deltaY renew
if (deltaY < touchPos.y - 5)
{
deltaY = touchPos.y - 3;
}
//finger down deltaY renew
if (deltaY > touchPos.y + 5)
{
deltaY = touchPos.y + 4;
}
transform.localScale = scale;
break;
}
}
}
}
我只建议您创建一个仅用于计算向量和钳位的内部类。这只是偏爱;但是我更喜欢将逻辑从何处计算出来,以及用新参数初始化对象的位置,并改用返回类型。它还使故障排除变得更容易,并且在C#中被认为是最佳实践。看 //forum.unity.com/threads/implement-separate-classes-for-ui-and-logic-game.490118/ 并且 罗伯特的编码规则:#11将用户界面逻辑与业务逻辑分开 虽然这不是世界上最好的帖子,但他的博客提出了与我相同的观点。C#:scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5), 2);
sens
depending on up or down?if (deltaY - 3 > touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5),
Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5),
2);
}
else if (deltaY + 3 <= touchPos.y)
{
scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5),
Mathf.Clamp(transform.localScale.y + ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5),
2);
}
sens
is multiplied by the same factor (5 or 10) regardless of up or down direction?ScaleChanger
class:class SensitivityFactor
{
public float Level { get; set; }
public float Factor { get; set; }
}
static List<SensitivityFactor> SensitivityFactors = new List<SensitivityFactor>()
{
new SensitivityFactor() { Level = 3.0f, Factor = 3.0f },
new SensitivityFactor() { Level = 2.0f, Factor = 5.0f },
new SensitivityFactor() { Level = 1.5f, Factor = 10.0f },
}
var realDelta = deltaY - touchPos.y;
var absDelta = Math.Abs(realDelta);
var sensitivityFactor = SensitivityFactors.FirstOrDefault(sf => sf.Level >= absDelta);
if (sensitivityFactor != null)
{
var signDelta = Math.Abs(realDelta);
var adjust = (realDelta + sensitivityFactor.Level) * signDelta / (sens * sensitivityFactor.Factor);
scale = new Vector3(Mathf.Clamp(transform.localScale.x - adjust, 1, 5),
Mathf.Clamp(transform.localScale.y + adjust, 1, 5),
2);
}
由于某种原因,如果我不将其乘以3,它会比另一个方向慢,而且我真的不知道为什么。所以我觉得很好Why do does these two vary for the factor multiplied withsens
depending on up or down?
In all the other cases,C#:if (deltaY - 3 > touchPos.y) { scale = new Vector3(Mathf.Clamp(transform.localScale.x + (deltaY + 3 - touchPos.y) / sens, 1, 5), Mathf.Clamp(transform.localScale.y - (deltaY + 3 - touchPos.y) / sens, 1, 5), 2); } else if (deltaY + 3 <= touchPos.y) { scale = new Vector3(Mathf.Clamp(transform.localScale.x - ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5), Mathf.Clamp(transform.localScale.y + ((deltaY + 3 - touchPos.y) * -1) / sens * 3, 1, 5), 2); }
sens
is multiplied by the same factor (5 or 10) regardless of up or down direction?
非常感谢您的帮助。.il查找并尝试执行您所说的.. il保持更新我只建议您创建一个仅用于计算向量和钳位的内部类。这只是偏爱;但是我更喜欢将逻辑从何处计算出来,以及用新参数初始化对象的位置,并改用返回类型。它还使故障排除变得更容易,并且在C#中被认为是最佳实践。看 //forum.unity.com/threads/implement-separate-classes-for-ui-and-logic-game.490118/ 并且 罗伯特的编码规则:#11将用户界面逻辑与业务逻辑分开 虽然这不是世界上最好的帖子,但他的博客提出了与我相同的观点。
很高兴您找到了行之有效的解决方案。
il更改脚本,我会及时通知您假设向上或向下的因数相同,则以下内容应缩短第10条后的第36-68行。
First declare the following class and static list within yourScaleChanger
class:
C#:class SensitivityFactor { public float Level { get; set; } public float Factor { get; set; } } static List<SensitivityFactor> SensitivityFactors = new List<SensitivityFactor>() { new SensitivityFactor() { Level = 3.0f, Factor = 3.0f }, new SensitivityFactor() { Level = 2.0f, Factor = 5.0f }, new SensitivityFactor() { Level = 1.5f, Factor = 10.0f }, }
然后,您可以将第36-68行替换为:
C#:var realDelta = deltaY - touchPos.y; var absDelta = Math.Abs(realDelta); var sensitivityFactor = SensitivityFactors.FirstOrDefault(sf => sf.Level >= absDelta); if (sensitivityFactor != null) { var signDelta = Math.Abs(realDelta); var adjust = (realDelta + sensitivityFactor.Level) * signDelta / (sens * sensitivityFactor.Factor); scale = new Vector3(Mathf.Clamp(transform.localScale.x - adjust, 1, 5), Mathf.Clamp(transform.localScale.y + adjust, 1, 5), 2); }