123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using Sirenix.OdinInspector;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- [ExecuteInEditMode]
- public class SimpleMoveItem : SerializedMonoBehaviour
- {
- [DictionaryDrawerSettings()]
- public Dictionary<GameObject, Transform> moveObjs = new Dictionary<GameObject, Transform>();
- [LabelText("相对移动位置")]
- public Vector3 localMovePos = new Vector3(0, 0, 0);
- [LabelText("相对旋转角度")]
- public float rotAngle = 0;
- [LabelText("运行时长")]
- public float duration = 1;
- public void SetInitState(bool reverse)
- {
- foreach (var item in moveObjs)
- {
- if (!reverse)
- {
- item.Key.transform.position = item.Value.position;
- item.Key.transform.rotation = item.Value.rotation;
- }
- else
- {
- item.Key.transform.rotation = item.Value.rotation;
- item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(localMovePos), rotAngle);
- item.Key.transform.position = item.Value.position + item.Value.TransformDirection(localMovePos);
- }
- }
- }
- public void SetFinishedState(bool reverse)
- {
- foreach (var item in moveObjs)
- {
- if (reverse)
- {
- item.Key.transform.position = item.Value.position;
- item.Key.transform.rotation = item.Value.rotation;
- }
- else
- {
- item.Key.transform.rotation = item.Value.rotation;
- item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(localMovePos), rotAngle);
- item.Key.transform.position = item.Value.position + item.Value.TransformDirection(localMovePos);
- }
- }
- }
- #if UNITY_EDITOR
- [FoldoutGroup("快捷编辑", Order = -1)]
- [Button("刷新物体及锚点")]
- public void AddSelectObj()
- {
- if (Selection.gameObjects.Length == 0) return;
- foreach (var item in moveObjs)
- {
- if (item.Value != null)
- {
- DestroyImmediate(item.Value.gameObject);
- }
- }
- moveObjs.Clear();
- foreach (var item in Selection.gameObjects)
- {
- GameObject tmpItem = new GameObject("锚点_" + item.gameObject.name);
- tmpItem.transform.parent = transform;
- tmpItem.transform.position = item.transform.position;
- tmpItem.transform.rotation = item.transform.rotation;
- moveObjs.Add(item, tmpItem.transform);
- }
- }
- [HideInInspector]
- public bool isShowPath = true;
- public bool ShowPath { get { return isShowPath; } }
- private void OnDrawGizmos()
- {
- DrawPathLine();
- }
- public void DrawPathLine()
- {
- if (!isShowPath) return;
- foreach (var item in moveObjs)
- {
- Gizmos.color = Color.yellow;
- Gizmos.DrawLine(item.Value.position, item.Value.position + item.Value.TransformDirection(localMovePos));
- }
- }
- /// <summary>
- /// 销毁所有子级信息
- /// </summary>
- /// <param name="item"></param>
- private void DestroyChild(Transform item)
- {
- if (item.childCount == 0) return;
- for (int i = item.childCount - 1; i >= 0; i--)
- {
- DestroyImmediate(item.GetChild(i).gameObject);
- }
- }
- [Button("Play")]
- public void Play()
- {
- isPlaying = true;
- timer = 0;
- }
- [HideInInspector]
- public bool isPlaying;
- private void OnEnable()
- {
- EditorApplication.update += EditorUpdate;
- Selection.selectionChanged += SelectionChanged;
- }
- private void OnDisable()
- {
- EditorApplication.update -= EditorUpdate;
- Selection.selectionChanged -= SelectionChanged;
- }
- private float timer = 0;
- void EditorUpdate()
- {
- if (!isPlaying) return;
- foreach (var item in moveObjs)
- {
- item.Key.transform.position = Vector3.Lerp(item.Value.position, item.Value.position + item.Value.TransformDirection(localMovePos), timer / duration);
- item.Key.transform.rotation = item.Value.rotation;
- item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(localMovePos), rotAngle * timer / duration);
- }
- timer += Time.deltaTime;
- if (timer > duration)
- {
- timer = 0;
- isPlaying = false;
- foreach (var item in moveObjs)
- {
- item.Key.transform.position = item.Value.position;
- item.Key.transform.rotation = item.Value.rotation;
- }
- }
- }
- void SelectionChanged()
- {
- if (Selection.gameObjects.Contains(this.gameObject))
- {
- isShowPath = true;
- }
- else
- {
- isShowPath = false;
- }
- }
- #endif
- }
|