天天看點

Unity控制在面闆上顯示變量unity之定制Inspector(Ngui)

unity之定制Inspector(Ngui)

剛接觸Unity時知道腳本的共有變量會在Inspector中顯示出來,但是有一些确不行,前些日子添加按鈕的音效,我用的是Ngui按鈕也基本是UIButton,但是給button一個個挂UIPlaySound腳本再挂音樂顯着有些笨拙,且不好控制是否允許播放,我就在UIButton上聲明了兩個變量一個是是否播放音效的bool變量另一個是音樂的枚舉,然後再UIButton腳本中的OnClick方法中播放音樂,但是在UIButton中的變量沒有在面闆中顯示出來,經過一番追此面闆是被定制了,在UIButtonEditor檔案中

using UnityEngine;
using UnityEditor;

[CanEditMultipleObjects]
#if UNITY_3_5
[CustomEditor(typeof(UIButton))]
#else
[CustomEditor(typeof(UIButton), true)]
#endif
public class UIButtonEditor : UIButtonColorEditor
{
    enum Highlight
    {
        DoNothing,
        Press,
    }

    protected override void DrawProperties ()
    {
        SerializedProperty sp = serializedObject.FindProperty("dragHighlight");
        Highlight ht = sp.boolValue ? Highlight.Press : Highlight.DoNothing;
        GUILayout.BeginHorizontal();
        bool highlight = (Highlight)EditorGUILayout.EnumPopup("Drag Over", ht) == Highlight.Press;
        NGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();
        if (sp.boolValue != highlight) sp.boolValue = highlight;

        DrawTransition();
        DrawColors();

        UIButton btn = target as UIButton;

        if (btn.tweenTarget != null)
        {
            UISprite sprite = btn.tweenTarget.GetComponent<UISprite>();
            UI2DSprite s2d = btn.tweenTarget.GetComponent<UI2DSprite>();

            if (sprite != null)
            {
                if (NGUIEditorTools.DrawHeader("Sprites", "Sprites", false, true))
                {
                    NGUIEditorTools.BeginContents(true);
                    EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects);
                    {
                        SerializedObject obj = new SerializedObject(sprite);
                        obj.Update();
                        SerializedProperty atlas = obj.FindProperty("mAtlas");
                        NGUIEditorTools.DrawSpriteField("Normal", obj, atlas, obj.FindProperty("mSpriteName"));
                        obj.ApplyModifiedProperties();

                        NGUIEditorTools.DrawSpriteField("Hover", serializedObject, atlas, serializedObject.FindProperty("hoverSprite"), true);
                        NGUIEditorTools.DrawSpriteField("Pressed", serializedObject, atlas, serializedObject.FindProperty("pressedSprite"), true);
                        NGUIEditorTools.DrawSpriteField("Disabled", serializedObject, atlas, serializedObject.FindProperty("disabledSprite"), true);
                    }
                    EditorGUI.EndDisabledGroup();

                    NGUIEditorTools.DrawProperty("Pixel Snap", serializedObject, "pixelSnap");
                    NGUIEditorTools.DrawProperty("IsPlaySoundp", serializedObject, "IsPlaySound"); ---
                    NGUIEditorTools.EndContents();
                }
            }
            else if (s2d != null)
            {
                if (NGUIEditorTools.DrawHeader("Sprites", "Sprites", false, true))
                {
                    NGUIEditorTools.BeginContents(true);
                    EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects);
                    {
                        SerializedObject obj = new SerializedObject(s2d);
                        obj.Update();
                        NGUIEditorTools.DrawProperty("Normal", obj, "mSprite");
                        obj.ApplyModifiedProperties();

                        NGUIEditorTools.DrawProperty("Hover", serializedObject, "hoverSprite2D");
                        NGUIEditorTools.DrawProperty("Pressed", serializedObject, "pressedSprite2D");
                        NGUIEditorTools.DrawProperty("Disabled", serializedObject, "disabledSprite2D");
                    }
                    EditorGUI.EndDisabledGroup();

                    NGUIEditorTools.DrawProperty("Pixel Snap", serializedObject, "pixelSnap");
                    NGUIEditorTools.EndContents();
                }
            }
        }

        UIButton button = target as UIButton;
        NGUIEditorTools.DrawEvents("On Click", button, button.onClick, false);
    }
}
           

上文中的NGUIEditorTools.DrawProperty(“IsPlaySound”, serializedObject, “IsPlaySound”); 就是控制在面闆中顯示變量IsPlaySound的方法。更多的定制就先不說了,後面再把這篇部落格寫的詳盡一些。