| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- using ChivaXR;
- using Sirenix.OdinInspector;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- [ExecuteInEditMode]
- public class LuoShuanMenuAnimation : SerializedMonoBehaviour
- {
- private bool showAniInfo = false;
- private float timer = 0;
- private bool isInit = true;
- private LuoShuanObj aniMenuItem;
- private int objListCount = 0;
- private int runListIndex = 0;
- private int aniState = 0;
- private int num = 0;
- [HideInInspector]
- public bool reverse = false;
- private float lerp;
- /// <summary> 存放锚点服务体的列表 </summary>
- private List<GameObject> tempMenuList = new List<GameObject>();
- public enum objType { 螺丝,螺母,垫片 }
- [Tooltip("和物体名称一样,只用于在执行队列中起提示作用,无实际代码逻辑作用")]
- [LabelText("物体类型"), PropertyOrder(0),OnValueChanged("ChangeType")]
- public objType itemType = objType.螺丝;
- void ChangeType()
- {
- objName= itemType.ToString();
- if (objName.Equals("垫片"))
- {
- localMovePos = Vector3.zero;
- rotAngle = 0;
- duration = 0;
- showAniInfo = true;
- }
- else
- {
- showAniInfo = false;
- }
- }
- [LabelText("是否对角拆除"), PropertyOrder(0)]
- public bool playType = true;
- [Tooltip("和物体类型一样,只用于在执行队列中起提示作用,无实际代码逻辑作用")]
- [LabelText("物体名称"), PropertyOrder(0)]
- public string objName = "螺丝";
- [FoldoutGroup("动画属性"), LabelText("移动方向"), PropertyOrder(1),HideIf("showAniInfo")]
- public Vector3 localMovePos;
- [FoldoutGroup("动画属性"), LabelText("旋转角度"), PropertyOrder(1), HideIf("showAniInfo")]
- public float rotAngle = 0;
- [FoldoutGroup("动画属性"), LabelText("运行时长"), PropertyOrder(1), HideIf("showAniInfo")]
- public float duration;
- [LabelText("执行队列"), PropertyOrder(1)]
- public List<LuoShuanObj> menuList = new List<LuoShuanObj>();
- [Button("添加组"), PropertyOrder(2)]
- public void AddMenu()
- {
- if (Selection.gameObjects.Length == 0) return;
- List<LsListSort> objList = new List<LsListSort> ();
- foreach (GameObject obj in Selection.gameObjects)
- {
- objList.Add(new LsListSort(obj));
- }
- objList.Sort(objListSort);
- List<GameObject> sortList = new List<GameObject>();
- foreach (LsListSort obj in objList)
- {
- sortList.Add(obj.Object);
- }
- menuList.Add(new LuoShuanObj(objName, sortList, localMovePos, rotAngle, duration, new List<GameObject>(), showAniInfo,itemType));
- }
- [Button("生成锚点"), PropertyOrder(2)]
- public void AddSelectObj()
- {
- if (tempMenuList.Count != 0)
- {
- foreach (GameObject obj in tempMenuList)
- DestroyImmediate(obj);
- tempMenuList.Clear();
- }
- if (menuList.Count != 0)
- foreach (LuoShuanObj lsObj in menuList)
- {
- if (lsObj.tmpList.Count != 0)
- {
- foreach (GameObject obj in lsObj.tmpList)
- DestroyImmediate(obj);
- }
- lsObj.tmpList.Clear();
- }
- foreach (LuoShuanObj lsObj in menuList)
- {
- if (lsObj.objList.Count == 0) return;
- GameObject tmpList = new GameObject("锚点组_" + lsObj.objName);
- tmpList.transform.parent = transform;
- foreach (GameObject obj in lsObj.objList)
- {
- GameObject tmpItem = new GameObject("锚点_" + obj.gameObject.name);
- tmpItem.transform.parent = tmpList.transform;
- tmpItem.transform.position = obj.transform.position;
- tmpItem.transform.rotation = obj.transform.rotation;
- lsObj.tmpList.Add(tmpItem);
- }
- tempMenuList.Add(tmpList);
- }
- }
- [ShowIf("infoBoxIsShow"), PropertyOrder(3)]
- [DisplayAsString]
- public string infoStr = "目前只支持4倍数的物体,请检查";
- private bool infoBoxIsShow = false;
- [Button("Play"), PropertyOrder(2)]
- public void Play()
- {
- isPlaying = true;
- timer = 0;
- isInit = true;
- objListCount = 0;
- runListIndex = 0;
- if (reverse)
- aniState = menuList.Count - 1;
- else
- aniState = 0;
- num = 0;
- }
- public void SetInitState()
- {
- foreach (LuoShuanObj obj in menuList)
- {
- for (int i = 0; i < obj.objList.Count; i++)
- {
- if (!reverse)
- {
- obj.objList[i].transform.position = obj.tmpList[i].transform.position;
- obj.objList[i].transform.rotation = obj.tmpList[i].transform.rotation;
- obj.objList[i].SetActive(true);
- }
- else
- {
- obj.objList[i].transform.position = obj.tmpList[i].transform.position + obj.tmpList[i].transform.TransformDirection(localMovePos);
- obj.objList[i].transform.rotation = obj.tmpList[i].transform.rotation;
- obj.objList[i].transform.RotateAround(obj.tmpList[i].transform.position, obj.tmpList[i].transform.TransformDirection(localMovePos), rotAngle);
- obj.objList[i].SetActive(false);
- }
- }
- }
- }
- public void SetFinishedState()
- {
- foreach (LuoShuanObj obj in menuList)
- {
- for (int i = 0; i < obj.objList.Count; i++)
- {
- if (reverse)
- {
- obj.objList[i].transform.position = obj.tmpList[i].transform.position;
- obj.objList[i].transform.rotation = obj.tmpList[i].transform.rotation;
- obj.objList[i].SetActive(true);
- }
- else
- {
- obj.objList[i].transform.position = obj.tmpList[i].transform.position + obj.tmpList[i].transform.TransformDirection(localMovePos);
- obj.objList[i].transform.rotation = obj.tmpList[i].transform.rotation;
- obj.objList[i].transform.RotateAround(obj.tmpList[i].transform.position, obj.tmpList[i].transform.TransformDirection(localMovePos), rotAngle);
- obj.objList[i].SetActive(false);
- }
- }
- }
- }
- #if UNITY_EDITOR
- [HideInInspector]
- public bool isPlaying;
- [HideInInspector]
- public bool isShowPath = true;
- public bool ShowPath { get { return isShowPath; } }
- private void OnDrawGizmos()
- {
- DrawPathLine();
- }
- public void DrawPathLine()
- {
- if (!isShowPath) return;
- if (tempMenuList.Count != 0)
- {
- foreach (LuoShuanObj lsObj in menuList)
- {
- if (lsObj.tmpList.Count != 0)
- {
- switch (lsObj.itemType)
- {
- case objType.螺丝: Gizmos.color = Color.yellow; break;
- case objType.螺母: Gizmos.color = Color.blue; break;
- //case objType.垫片: return;
- }
- Gizmos.DrawLine(lsObj.tmpList[1].transform.position, lsObj.tmpList[1].transform.position + lsObj.tmpList[1].transform.TransformDirection(lsObj.localMovePos));
- }
- }
- }
- }
- private void OnEnable()
- {
- EditorApplication.update += EditorUpdate;
- Selection.selectionChanged += SelectionChanged;
- }
- private void OnDisable()
- {
- EditorApplication.update -= EditorUpdate;
- Selection.selectionChanged -= SelectionChanged;
- }
- void EditorUpdate()
- {
- if (!isPlaying) return;
- foreach (LuoShuanObj obj in menuList)
- {
- if (obj.objList.Count == 0)
- {
- infoStr = "无物体对象数据,请检查";
- infoBoxIsShow = true;
- return;
- }
- if (obj.tmpList.Count == 0)
- {
- infoStr = "无锚点数据,请检查";
- infoBoxIsShow = true;
- return;
- }
- if (objListCount == 0)
- objListCount = obj.objList.Count;
- else
- if (objListCount != obj.objList.Count)
- {
- infoStr = "列表物体数量不匹配,请检查";
- infoBoxIsShow = true;
- return;
- }
- if(objListCount %4 != 0 && playType == true)
- {
- infoStr = "目前只支持4倍数的物体,请检查";
- infoBoxIsShow = true;
- return;
- }
- infoBoxIsShow = false;
- }
- if (isInit)
- {
- aniMenuItem = menuList[aniState];
- isInit = false;
- }
- if (reverse)
- {
- lerp = (1 - timer / aniMenuItem.duration);
- }
- else
- {
- lerp = timer / aniMenuItem.duration;
- }
- if (aniMenuItem.localMovePos != Vector3.zero)
- {
- if (playType == true)
- {
- PlayByDJ();
- }
- else
- {
- PlayByList();
- }
- }
- timer += Time.deltaTime;
- if (timer > aniMenuItem.duration)
- {
- timer = 0;
- if (playType == true)
- {
- HideObjByDJ();
- }
- else
- {
- HideObjBylist();
- }
- if (reverse)
- if (--aniState < 0)
- {
- aniState = menuList.Count - 1;
- if (num % 2 != 0)
- runListIndex++;
- num++;
- }
- if (!reverse)
- if (++aniState > menuList.Count - 1)
- {
- aniState = 0;
- if (num % 2 != 0)
- runListIndex++;
- num++;
- }
- aniMenuItem = menuList[aniState];
- if (runListIndex > objListCount / 4-1)
- {
- foreach (LuoShuanObj obj in menuList)
- {
- for (int i = 0; i < obj.objList.Count; i++)
- {
- if (obj.duration != 0 )
- {
- obj.objList[i].transform.position = obj.tmpList[i].transform.position;
- obj.objList[i].transform.rotation = obj.tmpList[i].transform.rotation;
- obj.objList[i].SetActive(true);
- }
- obj.objList[i].SetActive(true);
- }
- }
- isPlaying = false;
- }
- }
- }
- public int objListSort(LsListSort obj1, LsListSort obj2)
- {
- if (obj1.objNameNum > obj2.objNameNum)
- return 1;
- else
- return -1;
- }
- public class LsListSort
- {
- public GameObject Object;
- public int objNameNum;
- public LsListSort(GameObject Object)
- {
- this.Object = Object;
- if(Object.name.Length>3)
- objNameNum = Convert.ToInt32(Object.name.Substring(Object.name.Length - 3, 3));
- else
- objNameNum = Convert.ToInt32(Object.name);
- }
- }
- void SelectionChanged()
- {
- if (Selection.gameObjects.Contains(this.gameObject))
- {
- isShowPath = true;
- }
- else
- {
- isShowPath = false;
- }
- }
- #endif
- public IEnumerator IEAniPlay(Action finishedCallBack = null)
- {
- timer = 0;
- isInit = true;
- objListCount = 0;
- runListIndex = 0;
- if (reverse)
- aniState = menuList.Count - 1;
- else
- aniState = 0;
- num = 0;
- foreach (LuoShuanObj obj in menuList)
- {
- if (obj.objList.Count == 0)
- {
- Debug.Log( "无物体对象数据,请检查");
- StopCoroutine(IEAniPlay());
- yield return new WaitForEndOfFrame();
- }
- if (obj.tmpList.Count == 0)
- {
- Debug.Log("无锚点数据,请检查");
- StopCoroutine(IEAniPlay());
- yield return new WaitForEndOfFrame();
- }
- if (objListCount == 0)
- objListCount = obj.objList.Count;
- else
- if (objListCount != obj.objList.Count)
- {
- Debug.Log("列表物体数量不匹配,请检查");
- StopCoroutine(IEAniPlay());
- yield return new WaitForEndOfFrame();
- }
- if (objListCount % 4 != 0 && playType)
- {
- Debug.Log("目前只支持4倍数的物体,请检查");
- StopCoroutine(IEAniPlay());
- yield return new WaitForEndOfFrame();
- }
- }
- aniMenuItem = menuList[aniState];
- while (runListIndex < objListCount / 4 )
- {
- if(playType == true)
- {
- PlayByDJ();
- }
- else
- {
- PlayByList();
- }
- timer += Time.deltaTime;
- if (timer > aniMenuItem.duration)
- {
- timer = 0;
- if (playType == true)
- {
- HideObjByDJ();
- }
- else
- {
- HideObjBylist();
- }
- if(reverse)
- if (--aniState < 0 )
- {
- aniState = menuList.Count - 1;
- if (num % 2 != 0)
- runListIndex++;
- num++;
- }
- if (!reverse)
- if (++aniState > menuList.Count - 1)
- {
- aniState = 0;
- if (num % 2 != 0)
- runListIndex++;
- num++;
- }
- aniMenuItem = menuList[aniState];
- }
- yield return new WaitForEndOfFrame();
- }
- SetFinishedState();
- finishedCallBack?.Invoke();
- }
- private void PlayByDJ()
- {
- if (reverse)
- lerp = (1 - timer / aniMenuItem.duration);
- else
- lerp = timer / aniMenuItem.duration;
- if (aniMenuItem.localMovePos != Vector3.zero)
- {
- if (num % 2 != 0)
- {
- aniMenuItem.objList[runListIndex + objListCount / 4].SetActive(true);
- aniMenuItem.objList[runListIndex + 3 * objListCount / 4].SetActive(true);
- aniMenuItem.objList[runListIndex + objListCount / 4].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex + objListCount / 4].transform.position, aniMenuItem.tmpList[runListIndex + objListCount / 4].transform.position + aniMenuItem.tmpList[runListIndex + objListCount / 4].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex + objListCount / 4].transform.rotation = aniMenuItem.tmpList[runListIndex + objListCount / 4].transform.rotation;
- aniMenuItem.objList[runListIndex + objListCount / 4].transform.RotateAround(aniMenuItem.tmpList[runListIndex + objListCount / 4].transform.position, aniMenuItem.tmpList[runListIndex + objListCount / 4].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- aniMenuItem.objList[runListIndex + 3 * objListCount / 4].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex + 3 * objListCount / 4].transform.position, aniMenuItem.tmpList[runListIndex + 3 * objListCount / 4].transform.position + aniMenuItem.tmpList[runListIndex + 3 * objListCount / 4].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex + 3 * objListCount / 4].transform.rotation = aniMenuItem.tmpList[runListIndex + 3 * objListCount / 4].transform.rotation;
- aniMenuItem.objList[runListIndex + 3 * objListCount / 4].transform.RotateAround(aniMenuItem.tmpList[runListIndex + 3 * objListCount / 4].transform.position, aniMenuItem.tmpList[runListIndex + 3 * objListCount / 4].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- }
- else
- {
- aniMenuItem.objList[runListIndex].SetActive(true);
- aniMenuItem.objList[runListIndex + objListCount / 2].SetActive(true);
- aniMenuItem.objList[runListIndex].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex].transform.position, aniMenuItem.tmpList[runListIndex].transform.position + aniMenuItem.tmpList[runListIndex].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex].transform.rotation = aniMenuItem.tmpList[runListIndex].transform.rotation;
- aniMenuItem.objList[runListIndex].transform.RotateAround(aniMenuItem.tmpList[runListIndex].transform.position, aniMenuItem.tmpList[runListIndex].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- aniMenuItem.objList[runListIndex + objListCount / 2].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex + objListCount / 2].transform.position, aniMenuItem.tmpList[runListIndex + objListCount / 2].transform.position + aniMenuItem.tmpList[runListIndex + objListCount / 2].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex + objListCount / 2].transform.rotation = aniMenuItem.tmpList[runListIndex + objListCount / 2].transform.rotation;
- aniMenuItem.objList[runListIndex + objListCount / 2].transform.RotateAround(aniMenuItem.tmpList[runListIndex + objListCount / 2].transform.position, aniMenuItem.tmpList[runListIndex].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- }
- }
- }
- public void HideObjByDJ()
- {
- if (num % 2 != 0)
- {
- aniMenuItem.objList[runListIndex + objListCount / 4].SetActive(reverse);
- aniMenuItem.objList[runListIndex + 3 * objListCount / 4].SetActive(reverse);
- }
- else
- {
- aniMenuItem.objList[runListIndex].SetActive(reverse);
- aniMenuItem.objList[runListIndex + objListCount / 2].SetActive(reverse);
- }
- }
- public void HideObjBylist()
- {
- if (num % 2 != 0)
- {
- aniMenuItem.objList[runListIndex * 4 + 2].SetActive(reverse);
- aniMenuItem.objList[runListIndex * 4 + 3].SetActive(reverse);
- }
- else
- {
- aniMenuItem.objList[runListIndex * 4].SetActive(reverse);
- aniMenuItem.objList[runListIndex * 4 + 1].SetActive(reverse);
- }
- }
- private void PlayByList()
- {
- if (reverse)
- lerp = (1 - timer / aniMenuItem.duration);
- else
- lerp = timer / aniMenuItem.duration;
- if (aniMenuItem.localMovePos != Vector3.zero)
- {
- if (num % 2 != 0)
- {
- aniMenuItem.objList[runListIndex * 4 + 2].SetActive(true);
- aniMenuItem.objList[runListIndex * 4 + 3].SetActive(true);
- aniMenuItem.objList[runListIndex * 4 + 2].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex * 4 + 2].transform.position, aniMenuItem.tmpList[runListIndex * 4 + 2].transform.position + aniMenuItem.tmpList[runListIndex * 4 + 2].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex * 4 + 2].transform.rotation = aniMenuItem.tmpList[runListIndex * 4 + 2].transform.rotation;
- aniMenuItem.objList[runListIndex * 4 + 2].transform.RotateAround(aniMenuItem.tmpList[runListIndex * 4 + 2].transform.position, aniMenuItem.tmpList[runListIndex * 4 + 2].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- aniMenuItem.objList[runListIndex * 4 + 3].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex * 4 + 3].transform.position, aniMenuItem.tmpList[runListIndex * 4 + 3].transform.position + aniMenuItem.tmpList[runListIndex * 4 + 3].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex * 4 + 3].transform.rotation = aniMenuItem.tmpList[runListIndex * 4 + 3].transform.rotation;
- aniMenuItem.objList[runListIndex * 4 + 3].transform.RotateAround(aniMenuItem.tmpList[runListIndex * 4 + 3].transform.position, aniMenuItem.tmpList[runListIndex * 4 + 3].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- }
- else
- {
- aniMenuItem.objList[runListIndex * 4].SetActive(true);
- aniMenuItem.objList[runListIndex * 4 + 1].SetActive(true);
- aniMenuItem.objList[runListIndex * 4].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex * 4].transform.position, aniMenuItem.tmpList[runListIndex * 4].transform.position + aniMenuItem.tmpList[runListIndex * 4].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex * 4].transform.rotation = aniMenuItem.tmpList[runListIndex * 4].transform.rotation;
- aniMenuItem.objList[runListIndex * 4].transform.RotateAround(aniMenuItem.tmpList[runListIndex * 4].transform.position, aniMenuItem.tmpList[runListIndex * 4].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- aniMenuItem.objList[runListIndex * 4 + 1].transform.position = Vector3.Lerp(aniMenuItem.tmpList[runListIndex * 4 + 1].transform.position, aniMenuItem.tmpList[runListIndex * 4 + 1].transform.position + aniMenuItem.tmpList[runListIndex * 4 + 1].transform.TransformDirection(aniMenuItem.localMovePos), lerp);
- aniMenuItem.objList[runListIndex * 4 + 1].transform.rotation = aniMenuItem.tmpList[runListIndex * 4 + 1].transform.rotation;
- aniMenuItem.objList[runListIndex * 4 + 1].transform.RotateAround(aniMenuItem.tmpList[runListIndex * 4 + 1].transform.position, aniMenuItem.tmpList[runListIndex * 4 + 1].transform.TransformDirection(aniMenuItem.localMovePos), aniMenuItem.rotAngle * lerp);
- }
- }
- }
- [SerializeField]
- public struct LuoShuanObj
- {
- [LabelText("名称")]
- public string objName;
-
- [LabelText("对象列表")]
- public List<GameObject> objList;
- [FoldoutGroup("动画属性"), LabelText("移动方向"), HideIf("showAniInfo")]
- public Vector3 localMovePos;
- [FoldoutGroup("动画属性"), LabelText("旋转角度"), HideIf("showAniInfo")]
- public float rotAngle;
- [FoldoutGroup("动画属性"), LabelText("运行时长"), HideIf("showAniInfo")]
- public float duration;
- [LabelText("锚点列表"), HideIf("showAniInfo")]
- public List<GameObject> tmpList;
- private bool showAniInfo;
- [HideInInspector]
- public objType itemType;
- public LuoShuanObj(string objName, List<GameObject> objList, Vector3 localMovePos, float rotAngle, float duration, List<GameObject> tmpList, bool showAniInfo, objType itemType)
- {
- this.objName = objName;
- this.objList = objList;
- this.localMovePos = localMovePos;
- this.rotAngle = rotAngle;
- this.duration = duration;
- this.tmpList = tmpList;
- this.showAniInfo = showAniInfo;
- this.itemType = itemType;
- }
- }
- }
|