scene_滑动加点击动画

This commit is contained in:
wulongxiao 2024-11-26 00:39:25 +08:00
parent f22cce4a97
commit f6802b4675
3 changed files with 89 additions and 138 deletions

View File

@ -730,82 +730,6 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 201697259} m_GameObject: {fileID: 201697259}
m_CullTransparentMesh: 1 m_CullTransparentMesh: 1
--- !u!1 &207774471
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 207774472}
- component: {fileID: 207774474}
- component: {fileID: 207774473}
m_Layer: 5
m_Name: duizhao
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &207774472
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 207774471}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1769459764}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 2325, y: 0}
m_SizeDelta: {x: 7479.652, y: 2436}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &207774473
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 207774471}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 0.5529412}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 21300000, guid: 263d5b6b9a7228c4298f92ba8e135997, type: 3}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &207774474
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 207774471}
m_CullTransparentMesh: 1
--- !u!1 &215141885 --- !u!1 &215141885
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1441,6 +1365,12 @@ MonoBehaviour:
mainScene: {fileID: 1769459763} mainScene: {fileID: 1769459763}
minX: -5315 minX: -5315
maxX: 0 maxX: 0
targetPositions:
- 0
- -2100
- -3760
- -5350
mainBtnScript: {fileID: 0}
--- !u!1 &278944241 --- !u!1 &278944241
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1722,7 +1652,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: fa9fccadda7b7ac43b24c43fa954dcce, type: 3} m_Script: {fileID: 11500000, guid: fa9fccadda7b7ac43b24c43fa954dcce, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mainPanel: {fileID: 0} mainPanel: {fileID: 1769459763}
BTN_0: {fileID: 1937427102} BTN_0: {fileID: 1937427102}
BTN_1: {fileID: 1906759230} BTN_1: {fileID: 1906759230}
BTN_2: {fileID: 934652041} BTN_2: {fileID: 934652041}
@ -7314,7 +7244,6 @@ RectTransform:
- {fileID: 1535452030} - {fileID: 1535452030}
- {fileID: 500353810} - {fileID: 500353810}
- {fileID: 1434243873} - {fileID: 1434243873}
- {fileID: 207774472}
m_Father: {fileID: 278122029} m_Father: {fileID: 278122029}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

View File

@ -9,6 +9,7 @@ using UnityEngine.UI;
public class mainBTN : MonoBehaviour public class mainBTN : MonoBehaviour
{ {
public GameObject mainPanel; public GameObject mainPanel;
private RectTransform mainPanelRect;
public GameObject BTN_0; public GameObject BTN_0;
public GameObject BTN_1; public GameObject BTN_1;
@ -22,62 +23,61 @@ public class mainBTN : MonoBehaviour
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
BTN_0.GetComponent<Button>().onClick.AddListener(()=> { // 确保mainPanel不为空
BTN_0.SetActive(false); if (mainPanel == null)
BTN_1.SetActive(true); {
BTN_2.SetActive(true); Debug.LogError("mainPanel is not assigned!");
BTN_3.SetActive(true); return;
}
BTN_0_0.SetActive(true); mainPanelRect = mainPanel.GetComponent<RectTransform>();
BTN_1_1.SetActive(false);
BTN_2_2.SetActive(false);
BTN_3_3.SetActive(false);
});
BTN_1.GetComponent<Button>().onClick.AddListener(() => {
BTN_0.SetActive(true);
BTN_1.SetActive(false);
BTN_2.SetActive(true);
BTN_3.SetActive(true);
BTN_0_0.SetActive(false);
BTN_1_1.SetActive(true);
BTN_2_2.SetActive(false);
BTN_3_3.SetActive(false);
});
BTN_2.GetComponent<Button>().onClick.AddListener(() => {
BTN_0.SetActive(true);
BTN_1.SetActive(true);
BTN_2.SetActive(false);
BTN_3.SetActive(true);
BTN_0_0.SetActive(false);
BTN_1_1.SetActive(false);
BTN_2_2.SetActive(true);
BTN_3_3.SetActive(false);
});
BTN_3.GetComponent<Button>().onClick.AddListener(() => {
BTN_0.SetActive(true);
BTN_1.SetActive(true);
BTN_2.SetActive(true);
BTN_3.SetActive(false);
BTN_0_0.SetActive(false);
BTN_1_1.SetActive(false);
BTN_2_2.SetActive(false);
BTN_3_3.SetActive(true);
});
// 添加按钮点击事件
BTN_0.GetComponent<Button>().onClick.AddListener(() => MovePanel(0));
BTN_1.GetComponent<Button>().onClick.AddListener(() => MovePanel(-2100));
BTN_2.GetComponent<Button>().onClick.AddListener(() => MovePanel(-3760));
BTN_3.GetComponent<Button>().onClick.AddListener(() => MovePanel(-5350));
} }
// 统一的面板移动方法
public void MovePanel(float targetX)
// Update is called once per frame
void Update()
{ {
// 更新按钮的显示和隐藏
BTN_0.SetActive(targetX != 0);
BTN_1.SetActive(targetX != -2100);
BTN_2.SetActive(targetX != -3760);
BTN_3.SetActive(targetX != -5350);
BTN_0_0.SetActive(targetX == 0);
BTN_1_1.SetActive(targetX == -2100);
BTN_2_2.SetActive(targetX == -3760);
BTN_3_3.SetActive(targetX == -5350);
// 启动协程,平滑移动 mainPanel
StartCoroutine(SmoothMovePanel(targetX, 0.3f));
} }
// 协程实现平滑移动
private IEnumerator SmoothMovePanel(float targetX, float duration)
{
Vector2 startPosition = mainPanelRect.anchoredPosition;
Vector2 targetPosition = new Vector2(targetX, startPosition.y);
float elapsedTime = 0f;
while (elapsedTime < duration)
{
// 计算当前进度
float t = elapsedTime / duration;
// 逐渐改变 position
mainPanelRect.anchoredPosition = Vector2.Lerp(startPosition, targetPosition, t);
// 增加时间
elapsedTime += Time.deltaTime;
yield return null;
}
// 最终确保位置为目标位置
mainPanelRect.anchoredPosition = targetPosition;
}
} }

View File

@ -21,6 +21,13 @@ public class sceneContorl : MonoBehaviour
public float minX = -500f; // 最小x值 public float minX = -500f; // 最小x值
public float maxX = 500f; // 最大x值 public float maxX = 500f; // 最大x值
[Header("定义目标位置")]
public float[] targetPositions = new float[] { 0f, -2100f, -3760f, -5350f };
// 引用 mainBTN 脚本
public mainBTN mainBtnScript;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -92,18 +99,33 @@ public class sceneContorl : MonoBehaviour
mainSceneRectTransform.anchoredPosition = newPos; // 设置新的锚点位置 mainSceneRectTransform.anchoredPosition = newPos; // 设置新的锚点位置
// 更新起始位置,用于计算下一帧的增量 // 更新起始位置,用于计算下一帧的增量
m_oneFingerDragStartPos = fingerPos; m_oneFingerDragStartPos = fingerPos;
} }
/// <summary> /// <summary>
/// 屏幕坐标换算成3D坐标 /// 计算当前位置与目标位置数组中哪个位置最接近,并返回对应的目标位置值.
/// </summary> /// </summary>
/// <param name="screenPos">屏幕坐标</param> /// <param name="currentX">当前X坐标</param>
/// <returns></returns> /// <returns>返回距离最近的目标位置值</returns>
Vector3 GetWorldPos(Vector2 screenPos) private float GetClosestTargetValue(float currentX)
{ {
return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(m_cam.transform.position.z))); float closestValue = targetPositions[0]; // 默认目标位置为数组的第一个值
float minDistance = Mathf.Abs(currentX - targetPositions[0]); // 初始最小距离为第一个目标的距离
// 遍历 targetPositions 数组,计算与当前位置的距离
for (int i = 1; i < targetPositions.Length; i++)
{
float distance = Mathf.Abs(currentX - targetPositions[i]); // 计算当前位置与目标的绝对距离
if (distance < minDistance) // 如果当前距离比最小距离小
{
closestValue = targetPositions[i]; // 更新最近的目标位置
minDistance = distance; // 更新最小距离
}
}
return closestValue; // 返回最近目标的值
} }
} }