修改滑动条
This commit is contained in:
parent
4302cd5d56
commit
2c54ad0c52
File diff suppressed because it is too large
Load Diff
@ -32545,6 +32545,38 @@ PrefabInstance:
|
||||
propertyPath: speed
|
||||
value: 8
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor1.a
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor1.b
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor1.g
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor1.r
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor2.a
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor2.b
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor2.g
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1310652693, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: textColor2.r
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1402318480, guid: a8cc81ed2c5736a46a14628d8d489ac4, type: 3}
|
||||
propertyPath: canvas
|
||||
value:
|
||||
|
@ -13,37 +13,88 @@ public class ModePanel : MonoBehaviour
|
||||
[Header("滑动条")] public GameObject obj;
|
||||
[Header("速度")] public float speed;
|
||||
|
||||
public Color textColor1;
|
||||
public Color textColor2;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
btn1.onClick.AddListener(async () =>
|
||||
btn1.onClick.AddListener(() =>
|
||||
{
|
||||
MoveToTarget(obj, btn1.GetComponent<RectTransform>().position, speed*100);
|
||||
btn2.GetComponent<Text>().color = Color.white;
|
||||
await Task.Delay(200);
|
||||
btn1.GetComponent<Text>().color = Color.black;
|
||||
MoveToTarget(obj, btn1.GetComponent<RectTransform>().position, speed * 100, btn2, btn1);
|
||||
});
|
||||
btn2.onClick.AddListener(async () =>
|
||||
|
||||
btn2.onClick.AddListener(() =>
|
||||
{
|
||||
MoveToTarget(obj, btn2.GetComponent<RectTransform>().position, speed*100);
|
||||
btn1.GetComponent<Text>().color = Color.white;
|
||||
await Task.Delay(200);
|
||||
btn2.GetComponent<Text>().color = Color.black;
|
||||
MoveToTarget(obj, btn2.GetComponent<RectTransform>().position, speed * 100, btn1, btn2);
|
||||
});
|
||||
}
|
||||
|
||||
// 让物体滑动到目标位置
|
||||
void MoveToTarget(GameObject obj, Vector3 targetPosition, float speed)
|
||||
// 让物体滑动到目标位置,并同步更新按钮文本颜色
|
||||
void MoveToTarget(GameObject obj, Vector3 targetPosition, float speed, Button btnToDisable, Button btnToEnable)
|
||||
{
|
||||
// 计算物体与目标位置的距离
|
||||
float distance = Vector3.Distance(obj.transform.position, targetPosition);
|
||||
// 判断对象类型,如果是UI元素则使用 RectTransform,否则使用普通 Transform
|
||||
if (obj.GetComponent<RectTransform>() != null)
|
||||
{
|
||||
// 处理 UI 元素 (RectTransform)
|
||||
RectTransform rectTransform = obj.GetComponent<RectTransform>();
|
||||
float distance = Vector3.Distance(rectTransform.position, targetPosition);
|
||||
float moveDuration = distance / speed;
|
||||
|
||||
// 根据速度计算持续时间
|
||||
float moveDuration = distance / speed;
|
||||
// 记录滑动条初始位置,防止位置未改变时仍更新文本颜色
|
||||
Vector3 initialPosition = rectTransform.position;
|
||||
|
||||
|
||||
// 使用 DOTween 让物体滑动到目标位置
|
||||
// 使用 DOTween 让物体滑动到目标位置,并使用缓动效果(Ease.InOutQuad)
|
||||
obj.transform.DOMove(targetPosition, moveDuration).SetEase(Ease.InOutQuad);
|
||||
// 使用 DOTween 让物体滑动到目标位置,并同步更新按钮文本颜色
|
||||
Tween moveTween = rectTransform.DOMove(targetPosition, moveDuration).SetEase(Ease.InOutQuad);
|
||||
|
||||
// 确保动画不受 Time.timeScale 的影响
|
||||
moveTween.SetUpdate(true);
|
||||
|
||||
// 使用 OnUpdate 来同步文本颜色的变化
|
||||
moveTween.OnUpdate(() =>
|
||||
{
|
||||
// 判断滑动条是否移动,如果位置没有变化则不更新文本颜色
|
||||
if (rectTransform.position != initialPosition)
|
||||
{
|
||||
// 获取动画的进度
|
||||
float progress = moveTween.ElapsedPercentage();
|
||||
|
||||
// 线性插值计算颜色,随着物体移动,按钮的文本颜色也变化
|
||||
btnToDisable.GetComponent<Text>().color = Color.Lerp(textColor1, textColor2, progress);
|
||||
btnToEnable.GetComponent<Text>().color = Color.Lerp(textColor2, textColor1, progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (obj.GetComponent<Transform>() != null)
|
||||
{
|
||||
// 处理场景中的 3D 物体 (Transform)
|
||||
Transform transform = obj.GetComponent<Transform>();
|
||||
float distance = Vector3.Distance(transform.position, targetPosition);
|
||||
float moveDuration = distance / speed;
|
||||
|
||||
// 记录滑动条初始位置,防止位置未改变时仍更新文本颜色
|
||||
Vector3 initialPosition = transform.position;
|
||||
|
||||
// 使用 DOTween 让物体滑动到目标位置
|
||||
Tween moveTween = transform.DOMove(targetPosition, moveDuration).SetEase(Ease.InOutQuad);
|
||||
|
||||
// 确保动画不受 Time.timeScale 的影响
|
||||
moveTween.SetUpdate(true);
|
||||
|
||||
// 使用 OnUpdate 来同步文本颜色的变化
|
||||
moveTween.OnUpdate(() =>
|
||||
{
|
||||
// 判断滑动条是否移动,如果位置没有变化则不更新文本颜色
|
||||
if (transform.position != initialPosition)
|
||||
{
|
||||
// 获取动画的进度
|
||||
float progress = moveTween.ElapsedPercentage();
|
||||
|
||||
// 线性插值计算颜色,随着物体移动,按钮的文本颜色也变化
|
||||
btnToDisable.GetComponent<Text>().color = Color.Lerp(textColor1, textColor2, progress);
|
||||
btnToEnable.GetComponent<Text>().color = Color.Lerp(textColor2, textColor1, progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user