万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    uGUIのリセットボタン

    NGUIはpositionなどのリセットが便利。 ※デフォルトのは出ないのは注意!

    http://answers.unity3d.com/questions/1076559/how-do-i-get-the-default-inspector-for-a-customedi.html

    RectTransformでも同じことをしたい。

    f:id:shinriyo:20140911162548p:plain

    NGUIはNGUITransformInspector.csにある。 uGUIでも使いたいので移植。 RectTransform.csをEditorに入れて使おう。

    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    using UnityEngine.UI;
    
    [CustomEditor(typeof(RectTransform))]
    public class RectTransformInspector : Editor
    {
        static public RectTransformInspector instance;
    
        static public void SetLabelWidth (float width)
        {
    #if UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_5_4
            EditorGUIUtility.LookLikeControls(width);
    #else
            EditorGUIUtility.labelWidth = width;
    #endif
        }
    
        void OnDestroy () { instance = null; }
    
        public override void OnInspectorGUI ()
        {
            // デフォルトも表示させる場合が・・できない.
            DrawDefaultInspector();
    
            RectTransform trans = target as RectTransform;
            SetLabelWidth (15f);
    
            Vector3 pos;
            Vector3 rot;
            Vector3 scale;
    
            // Position
            EditorGUILayout.BeginHorizontal ();
            {
                if (DrawButton ("P", "Reset Position", IsResetPositionValid (trans), 20f))
                {
                    RegisterUndo ("Reset Position", trans);
                    trans.localPosition = Vector3.zero;
                }
                pos = DrawVector3 (trans.localPosition);
            }
            EditorGUILayout.EndHorizontal ();
    
            // Rotation
            EditorGUILayout.BeginHorizontal ();
            {
                if (DrawButton ("R", "Reset Rotation", IsResetRotationValid (trans), 20f))
                {
                    RegisterUndo ("Reset Rotation", trans);
                    trans.localEulerAngles = Vector3.zero;
                }
                rot = DrawVector3 (trans.localEulerAngles);
            }
            EditorGUILayout.EndHorizontal ();
    
            // Scale
            EditorGUILayout.BeginHorizontal ();
            {
                if (DrawButton ("S", "Reset Scale", IsResetScaleValid (trans), 20f))
                {
                    RegisterUndo ("Reset Scale", trans);
                    trans.localScale = Vector3.one;
                }
                scale = DrawVector3 (trans.localScale);
            }
            EditorGUILayout.EndHorizontal ();
        }
    
        /// <summary>
        /// Helper function that draws a button in an enabled or disabled state.
        /// </summary>
        static bool DrawButton (string title, string tooltip, bool enabled, float width)
        {
            if (enabled)
            {
                // Draw a regular button
                return GUILayout.Button (new GUIContent (title, tooltip), GUILayout.Width (width));
            }
            else
            {
                // Button should be disabled -- draw it darkened and ignore its return value
                Color color = GUI.color;
                GUI.color = new Color (1f, 1f, 1f, 0.25f);
                GUILayout.Button (new GUIContent (title, tooltip), GUILayout.Width (width));
                GUI.color = color;
                return false;
            }
        }
    
        /// <summary>
        /// Helper function that determines whether its worth it to show the reset position button.
        /// </summary>
        static bool IsResetPositionValid (Transform targetTransform)
        {
            Vector3 v = targetTransform.localPosition;
            return (v.x != 0f || v.y != 0f || v.z != 0f);
        }
    
        /// <summary>
        /// Helper function that determines whether its worth it to show the reset rotation button.
        /// </summary>
        static bool IsResetRotationValid (Transform targetTransform)
        {
            Vector3 v = targetTransform.localEulerAngles;
            return (v.x != 0f || v.y != 0f || v.z != 0f);
        }
    
        /// <summary>
        /// Helper function that determines whether its worth it to show the reset scale button.
        /// </summary>
        static bool IsResetScaleValid (Transform targetTransform)
        {
            Vector3 v = targetTransform.localScale;
            return (v.x != 1f || v.y != 1f || v.z != 1f);
        }
    
        /// <summary>
        /// Helper function that draws a field of 3 floats.
        /// </summary>
        static Vector3 DrawVector3 (Vector3 value)
        {
            GUILayoutOption opt = GUILayout.MinWidth (30f);
            value.x = EditorGUILayout.FloatField ("X", value.x, opt);
            value.y = EditorGUILayout.FloatField ("Y", value.y, opt);
            value.z = EditorGUILayout.FloatField ("Z", value.z, opt);
            return value;
        }
    
        /// <summary>
        /// Create an undo point for the specified objects.
        /// </summary>
        static public void RegisterUndo (string name, params Object[] objects)
        {
            if (objects != null && objects.Length > 0)
            {
    #if UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
                UnityEditor.Undo.RegisterUndo(objects, name);
    #else
                UnityEditor.Undo.RecordObjects (objects, name);
    #endif
                foreach (Object obj in objects)
                {
                    if (obj == null)
                    {
                        continue;
                    }
                    EditorUtility.SetDirty (obj);
                }
            }
        }
    }

    Unity4入門 最新開発環境による簡単3Dゲーム制作

    Unity4入門 最新開発環境による簡単3Dゲーム制作