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 moveObjs = new Dictionary(); [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)); } } /// /// 销毁所有子级信息 /// /// 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 }