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的方法。更多的定制就先不说了,后面再把这篇博客写的详尽一些。