Пример #1
0
        public override void OnInspectorGUI()
        {
            transform = target as Transform;

            if (!EditorGUIUtility.wideMode)
            {
                EditorGUIUtility.wideMode   = true;
                EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth - 212;
            }

            serializedObject.UpdateIfRequiredOrScript();

            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.PropertyField(properties.Position, Content.Position);
                using (new EditorGUI.DisabledGroupScope(properties.Position.vector3Value == Vector3.zero))
                    if (GUILayout.Button(Content.ResetPosition, Styles.ResetButton))
                    {
                        properties.Position.vector3Value = Vector3.zero;
                    }
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                rotationGUI.Draw();
                using (new EditorGUI.DisabledGroupScope(rotationGUI.eulerAngles == Vector3.zero))
                    if (GUILayout.Button(Content.ResetRotation, Styles.ResetButton))
                    {
                        rotationGUI.Reset();
                        if (Tools.current == Tool.Rotate)
                        {
                            if (Tools.pivotRotation == PivotRotation.Global)
                            {
                                Tools.handleRotation = Quaternion.identity;
                                SceneView.RepaintAll();
                            }
                        }
                    }
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.PropertyField(properties.Scale, Content.Scale);
                using (new EditorGUI.DisabledGroupScope(properties.Scale.vector3Value == Vector3.one))
                    if (GUILayout.Button(Content.ResetScale, Styles.ResetButton))
                    {
                        properties.Scale.vector3Value = Vector3.one;
                    }
            }

            // I can hard code this b/c the transform inspector is always drawn in the same spot lmao
            var dragRect = new Rect(16, 105, EditorGUIUtility.labelWidth - 10, 10);

            var e = Event.current;

            if (dragRect.Contains(e.mousePosition) && e.type == EventType.MouseDown && e.button == 0)
            {
                var currentScale = properties.Scale.vector3Value;
                xyRatio = currentScale.y / currentScale.x;
                xzRatio = currentScale.z / currentScale.x;
            }

            using (var check = new EditorGUI.ChangeCheckScope())
            {
                var c = GUI.color;
                GUI.color = Color.clear;
                var newScaleX = CustomFloatField.Draw(new Rect(), dragRect, properties.Scale.vector3Value.x, EditorStyles.numberField);

                if (check.changed)
                {
                    var currentScale = properties.Scale.vector3Value;

                    var delta = newScaleX - properties.Scale.vector3Value.x;

                    currentScale.x += delta;
                    currentScale.y += delta * xyRatio;
                    currentScale.z += delta * xzRatio;

                    properties.Scale.vector3Value = currentScale;
                }

                GUI.color = c;
            }

            #region Copy

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("复制坐标:", GUILayout.Width(60));
            if (GUILayout.Button("X"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosX     = transform.position.x;
                copyData.CopyPosX = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("Y"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosY     = transform.position.y;
                copyData.CopyPosY = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosZ     = transform.position.z;
                copyData.CopyPosZ = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("X-Y"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosX     = transform.position.x;
                copyData.PosY     = transform.position.y;
                copyData.CopyPosX = true;
                copyData.CopyPosY = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("X-Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosX     = transform.position.x;
                copyData.PosZ     = transform.position.z;
                copyData.CopyPosX = true;
                copyData.CopyPosZ = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("Y-Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosY     = transform.position.y;
                copyData.PosZ     = transform.position.z;
                copyData.CopyPosY = true;
                copyData.CopyPosZ = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("X-Y-Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosX     = transform.position.x;
                copyData.PosY     = transform.position.y;
                copyData.PosZ     = transform.position.z;
                copyData.CopyPosX = true;
                copyData.CopyPosY = true;
                copyData.CopyPosZ = true;
                textEd.text       = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("复制旋转:", GUILayout.Width(60));
            if (GUILayout.Button("X"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationX     = transform.rotation.eulerAngles.x;
                copyData.CopyRotationX = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("Y"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationY     = transform.rotation.eulerAngles.y;
                copyData.CopyRotationY = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationZ     = transform.rotation.eulerAngles.z;
                copyData.CopyRotationZ = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("X-Y"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationX     = transform.rotation.eulerAngles.x;
                copyData.RotationY     = transform.rotation.eulerAngles.y;
                copyData.CopyRotationX = true;
                copyData.CopyRotationY = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("X-Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationX     = transform.rotation.eulerAngles.x;
                copyData.RotationZ     = transform.rotation.eulerAngles.z;
                copyData.CopyRotationX = true;
                copyData.CopyRotationZ = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("Y-Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationY     = transform.rotation.eulerAngles.y;
                copyData.RotationZ     = transform.rotation.eulerAngles.z;
                copyData.CopyRotationY = true;
                copyData.CopyRotationZ = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("X-Y-Z"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.RotationX     = transform.rotation.eulerAngles.x;
                copyData.RotationY     = transform.rotation.eulerAngles.y;
                copyData.RotationZ     = transform.rotation.eulerAngles.z;
                copyData.CopyRotationX = true;
                copyData.CopyRotationY = true;
                copyData.CopyRotationZ = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("复制坐标和旋转"))
            {
                TextEditor        textEd   = new TextEditor();
                TransformCopyData copyData = new TransformCopyData();
                copyData.PosX          = transform.position.x;
                copyData.PosY          = transform.position.y;
                copyData.PosZ          = transform.position.z;
                copyData.CopyPosX      = true;
                copyData.CopyPosY      = true;
                copyData.CopyPosZ      = true;
                copyData.RotationX     = transform.rotation.eulerAngles.x;
                copyData.RotationY     = transform.rotation.eulerAngles.y;
                copyData.RotationZ     = transform.rotation.eulerAngles.z;
                copyData.CopyRotationX = true;
                copyData.CopyRotationY = true;
                copyData.CopyRotationZ = true;
                textEd.text            = copyData.ToString();
                textEd.OnFocus();
                textEd.Copy();
            }
            if (GUILayout.Button("粘贴"))
            {
                TransformCopyData transformCopyData = new TransformCopyData(GUIUtility.systemCopyBuffer);
                if (transformCopyData != null)
                {
                    Transform[] transforms = Selection.transforms;
                    for (int i = 0; i < transforms.Length; i++)
                    {
                        Transform trans = transforms[i];
                        trans.position = transformCopyData.GetCopyPos(trans.position);
                        trans.rotation = Quaternion.Euler(transformCopyData.GetCopyRotation(trans.rotation.eulerAngles));
                    }
                }
                EditorUtility.SetDirty(target);
            }
            EditorGUILayout.EndHorizontal();

            #endregion

            serializedObject.ApplyModifiedProperties();

            EditorGUIUtility.labelWidth = 0;

            var position = transform.position;

            if
            (
                Mathf.Abs(position.x) > MaxDistanceFromOrigin ||
                Mathf.Abs(position.y) > MaxDistanceFromOrigin ||
                Mathf.Abs(position.z) > MaxDistanceFromOrigin
            )
            {
                EditorGUILayout.HelpBox(Content.FloatingPointWarning, MessageType.Warning);
            }
        }
Пример #2
0
        public override void OnInspectorGUI()
        {
            if (!EditorGUIUtility.wideMode)
            {
                EditorGUIUtility.wideMode   = true;
                EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth - 212;
            }

            serializedObject.UpdateIfRequiredOrScript();

            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.PropertyField(properties.Position, Content.Position);
                if (GUILayout.Button(Content.ResetPosition, Styles.ResetButton))
                {
                    properties.Position.vector3Value = Vector3.zero;
                }
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                rotationGUI.Draw();
                if (GUILayout.Button(Content.ResetRotation, Styles.ResetButton))
                {
                    rotationGUI.Reset();
                    if (Tools.current == Tool.Rotate)
                    {
                        if (Tools.pivotRotation == PivotRotation.Global)
                        {
                            Tools.handleRotation = Quaternion.identity;
                            SceneView.RepaintAll();
                        }
                    }
                }
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.PropertyField(properties.Scale, Content.Scale);
                if (GUILayout.Button(Content.ResetScale, Styles.ResetButton))
                {
                    properties.Scale.vector3Value = Vector3.one;
                }
            }

            // I can hard code this b/c the transform inspector is always drawn in the same spot lmao
            var dragRect = new Rect(16, 105, EditorGUIUtility.labelWidth - 10, 10);

            var e = Event.current;

            if (dragRect.Contains(e.mousePosition) && e.type == EventType.MouseDown && e.button == 0)
            {
                var currentScale = properties.Scale.vector3Value;
                xyRatio = currentScale.y / currentScale.x;
                xzRatio = currentScale.z / currentScale.x;
            }

            using (var check = new EditorGUI.ChangeCheckScope())
            {
                var c = GUI.color;
                GUI.color = Color.clear;
                var newScaleX = CustomFloatField.Draw(new Rect(), dragRect, properties.Scale.vector3Value.x, EditorStyles.numberField);

                if (check.changed)
                {
                    var currentScale = properties.Scale.vector3Value;

                    var delta = newScaleX - properties.Scale.vector3Value.x;

                    currentScale.x += delta;
                    currentScale.y += delta * xyRatio;
                    currentScale.z += delta * xzRatio;

                    properties.Scale.vector3Value = currentScale;
                }

                GUI.color = c;
            }

            serializedObject.ApplyModifiedProperties();

            EditorGUIUtility.labelWidth = 0;

            var transform = target as Transform;
            var position  = transform.position;

            if
            (
                Mathf.Abs(position.x) > MaxDistanceFromOrigin ||
                Mathf.Abs(position.y) > MaxDistanceFromOrigin ||
                Mathf.Abs(position.z) > MaxDistanceFromOrigin
            )
            {
                EditorGUILayout.HelpBox(Content.FloatingPointWarning, MessageType.Warning);
            }
        }