123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using System;
- using UnityEngine.UI;
- public class MoveItemManager : MonoBehaviour
- {
- [TableList]
- public List<MoveItemInfo> moveItemInfos;
- [LabelText("播完反向播放")]
- public bool m_Reverse;
- public Button m_PlayButton;
- public Button m_CloseButton;
- private void Start()
- {
- m_PlayButton?.onClick.AddListener(OnPlayButtonClick);
- m_CloseButton?.onClick.AddListener(OnCloseBtnClick);
- PlayMoveItemByIndex(0);
- }
- private bool runing;
- private bool Runing
- {
- set
- {
- runing = value;
- }
- get
- {
- return runing;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="index">index 从1开始</param>
- public void PlayMoveItemByIndex(int index = 0)
- {
- if (index > moveItemInfos.Count - 1)
- {
- Debug.Log("正播播放序列完成");
- if (m_Reverse)
- {
- PlayMoveItemByReverseIndex(moveItemInfos.Count - 1);
- }
- }
- else
- {
- StopAllCoroutines();
- Debug.Log("播放" + moveItemInfos[index].moveItem.name);
- StartCoroutine(MovePathByItem(moveItemInfos[index], false, () =>
- {
- moveItemInfos[index].moveItem.gameObject.SetActive(false);
- PlayMoveItemByIndex(index + 1);
- }));
- }
- }
- public void PlayMoveItemByReverseIndex(int index)
- {
- if (index < 0)
- {
- Debug.Log("反向播放序列完成");
- }
- else
- {
- StopAllCoroutines();
- Debug.Log("反向播放" + moveItemInfos[index].moveItem.name);
- StartCoroutine(MovePathByItem(moveItemInfos[index], true, () =>
- {
- PlayMoveItemByReverseIndex(index - 1);
- }));
- }
- }
- IEnumerator MovePathByItem(MoveItemInfo moveItemInfo, bool isReverse, Action finishedCallBack = null)
- {
- float timer = 0;
- float lerp = 0;
- while (timer < moveItemInfo.moveItem.m_Duration)
- {
- MoveItem moveItem = moveItemInfo.moveItem;
- moveItem.gameObject.SetActive(true);
- moveItem.OpenHighter(true);
- lerp = isReverse? (1 - timer / moveItem.m_Duration) :timer / moveItem.m_Duration;
- if (moveItem.m_UserGlobalDirection)
- {
- moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
- moveItemInfo.initPosition + moveItem.m_Direction, lerp);
- }
- else
- {
- moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
- moveItemInfo.initPosition + moveItem.transform.TransformDirection(moveItem.m_Direction), lerp);
- }
- timer += Time.deltaTime;
- yield return new WaitForEndOfFrame();
- }
- moveItemInfo.finish = true;
- moveItemInfo.moveItem?.OpenHighter(false);
- finishedCallBack.Invoke();
- }
- /// <summary>
- /// 获取所有的组件
- /// </summary>
- [Button("获取所有的组件")]
- private void GetAllMoveItems()
- {
- moveItemInfos = new List<MoveItemInfo>();
- MoveItem[] moveItems = transform.GetComponentsInChildren<MoveItem>();
- foreach (var item in moveItems)
- {
- MoveItemInfo moveItemInfo = new MoveItemInfo();
- moveItemInfo.moveItem = item;
- moveItemInfo.initPosition = item.transform.position;
- moveItemInfos.Add(moveItemInfo);
- }
- }
- /// <summary>
- /// 播放按钮点击
- /// </summary>
- private void OnPlayButtonClick()
- {
- if (Time.timeScale == 0)
- {
- Time.timeScale = 1;
- if (m_PlayButton)
- {
- m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "暂停";
- }
- }
- else
- {
- Time.timeScale = 0;
- if (m_PlayButton)
- {
- m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "播放";
- }
- }
- }
- private void OnCloseBtnClick()
- {
- Application.Quit();
- }
- }
- [Serializable]
- public class MoveItemInfo
- {
- [GUIColor("FinishedStateColor")]
- public MoveItem moveItem;
- [LabelText("父级对象")]
- /// <summary>
- /// 初始化位置
- /// </summary>
- [HideInInspector]
- public Vector3 initPosition;
- [LabelText("是否完成")]
- public bool finish;
- private Color FinishedStateColor()
- {
- if (finish)
- {
- return new Color(0.7f, 1f, 0.7f);
- }
- return GUI.color;
- }
- }
|