unity3D 编辑器扩展,自己写控件(BeginContents)(DrawHeader)

写编辑器时,不为别的,就为排版漂亮点。

参考NGUI的编辑器工具,修改了下,实现如下效果:

unity3D 编辑器扩展,自己写控件(BeginContents)(DrawHeader)_第1张图片

控件代码如下:

YZSettings.cs

using UnityEngine;
using UnityEditor;

public class YZSettings
{
    #region Generic Get and Set methods
    /// 
    /// Save the specified boolean value in settings.
    /// 
    static public void SetBool(string name, bool val) { EditorPrefs.SetBool(name, val); }

    /// 
    /// Save the specified integer value in settings.
    /// 
    static public void SetInt(string name, int val) { EditorPrefs.SetInt(name, val); }

    /// 
    /// Save the specified float value in settings.
    /// 
    static public void SetFloat(string name, float val) { EditorPrefs.SetFloat(name, val); }

    /// 
    /// Save the specified string value in settings.
    /// 
    static public void SetString(string name, string val) { EditorPrefs.SetString(name, val); }

    /// 
    /// Save the specified color value in settings.
    /// 
    static public void SetColor(string name, Color c) { SetString(name, c.r + " " + c.g + " " + c.b + " " + c.a); }

    /// 
    /// Save the specified enum value to settings.
    /// 
    static public void SetEnum(string name, System.Enum val) { SetString(name, val.ToString()); }

    /// 
    /// Save the specified object in settings.
    /// 
    static public void Set(string name, Object obj)
    {
        if (obj == null)
        {
            EditorPrefs.DeleteKey(name);
        }
        else
        {
            if (obj != null)
            {
                string path = AssetDatabase.GetAssetPath(obj);

                if (!string.IsNullOrEmpty(path))
                {
                    EditorPrefs.SetString(name, path);
                }
                else
                {
                    EditorPrefs.SetString(name, obj.GetInstanceID().ToString());
                }
            }
            else EditorPrefs.DeleteKey(name);
        }
    }

    /// 
    /// Get the previously saved boolean value.
    /// 
    static public bool GetBool(string name, bool defaultValue) { return EditorPrefs.GetBool(name, defaultValue); }

    /// 
    /// Get the previously saved integer value.
    /// 
    static public int GetInt(string name, int defaultValue) { return EditorPrefs.GetInt(name, defaultValue); }

    /// 
    /// Get the previously saved float value.
    /// 
    static public float GetFloat(string name, float defaultValue) { return EditorPrefs.GetFloat(name, defaultValue); }

    /// 
    /// Get the previously saved string value.
    /// 
    static public string GetString(string name, string defaultValue) { return EditorPrefs.GetString(name, defaultValue); }

    /// 
    /// Get a previously saved color value.
    /// 
    static public Color GetColor(string name, Color c)
    {
        string strVal = GetString(name, c.r + " " + c.g + " " + c.b + " " + c.a);
        string[] parts = strVal.Split(' ');

        if (parts.Length == 4)
        {
            float.TryParse(parts[0], out c.r);
            float.TryParse(parts[1], out c.g);
            float.TryParse(parts[2], out c.b);
            float.TryParse(parts[3], out c.a);
        }
        return c;
    }

    /// 
    /// Get a previously saved enum from settings.
    /// 
    static public T GetEnum(string name, T defaultValue)
    {
        string val = GetString(name, defaultValue.ToString());
        string[] names = System.Enum.GetNames(typeof(T));
        System.Array values = System.Enum.GetValues(typeof(T));

        for (int i = 0; i < names.Length; ++i)
        {
            if (names[i] == val)
                return (T)values.GetValue(i);
        }
        return defaultValue;
    }

    /// 
    /// Get a previously saved object from settings.
    /// 
    static public T Get(string name, T defaultValue) where T : Object
    {
        string path = EditorPrefs.GetString(name);
        if (string.IsNullOrEmpty(path)) return null;

        T retVal = YZEditorTools.LoadAsset(path);

        if (retVal == null)
        {
            int id;
            if (int.TryParse(path, out id))
                return EditorUtility.InstanceIDToObject(id) as T;
        }
        return retVal;
    }
    #endregion

    #region Convenience accessor properties

    static public bool minimalisticLook
    {
        get { return GetBool("NGUI Minimalistic", false); }
        set { SetBool("NGUI Minimalistic", value); }
    }

    #endregion
}

 YZEditorTools.cs

using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;

/// 
/// Tools for the editor
/// 
static public class YZEditorTools
{
    /// 
    /// Draw a distinctly different looking header label
    /// 
    static public bool DrawMinimalisticHeader(string text) { return DrawHeader(text, text, false, true); }

    /// 
    /// Draw a distinctly different looking header label
    /// 
    static public bool DrawHeader(string text) { return DrawHeader(text, text, false, YZSettings.minimalisticLook); }

    /// 
    /// Draw a distinctly different looking header label
    /// 
    static public bool DrawHeader(string text, string key) { return DrawHeader(text, key, false, YZSettings.minimalisticLook); }

    /// 
    /// Draw a distinctly different looking header label
    /// 
    static public bool DrawHeader(string text, bool detailed) { return DrawHeader(text, text, detailed, !detailed); }

    /// 
    /// Draw a distinctly different looking header label
    /// 
   static public bool DrawHeader(string text, string key, bool forceOn, bool minimalistic)
    {
        bool state = EditorPrefs.GetBool(key, true);

        if (!minimalistic) GUILayout.Space(3f);
        if (!forceOn && !state) GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
        GUILayout.BeginHorizontal();
        GUI.changed = false;
       if (minimalistic)
        {
            if (state) text = "\u25BC" + (char)0x200a + text;
            else text = "\u25BA" + (char)0x200a + text;

            GUILayout.BeginHorizontal();
            GUI.contentColor = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.7f) : new Color(0f, 0f, 0f, 0.7f);
            if (!GUILayout.Toggle(true, text, "PreToolbar2", GUILayout.MinWidth(20f))) state = !state;
            GUI.contentColor = Color.white;
            GUILayout.EndHorizontal();
        }
        else
        {
            text = "" + text + "";
            if (state) text = "\u25BC " + text;
            else text = "\u25BA " + text;
            if (!GUILayout.Toggle(true, text, "dragtab", GUILayout.MinWidth(20f))) state = !state;
        }
        if (GUI.changed) EditorPrefs.SetBool(key, state);

        if (!minimalistic) GUILayout.Space(2f);
        GUILayout.EndHorizontal();
        GUI.backgroundColor = Color.white;
        if (!forceOn && !state) GUILayout.Space(3f);
        return state;
    }

    /// 
    /// Begin drawing the content area.
    /// 
    static public void BeginContents() { BeginContents(YZSettings.minimalisticLook); }

    static bool mEndHorizontal = false;

#if UNITY_4_7 || UNITY_5_5 || UNITY_5_6
	static public string textArea = "AS TextArea";
#else
    static public string textArea = "TextArea";
#endif

    /// 
    /// Begin drawing the content area.
    /// 
    static public void BeginContents(bool minimalistic)
    {
        if (!minimalistic)
        {
            mEndHorizontal = true;
            GUILayout.BeginHorizontal();
            EditorGUILayout.BeginHorizontal(textArea, GUILayout.MinHeight(10f));
        }
        else
        {
            mEndHorizontal = false;
            EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(10f));
            GUILayout.Space(10f);
        }
        GUILayout.BeginVertical();
        GUILayout.Space(2f);
    }
    /// 
    /// End drawing the content area.
    /// 
    static public void EndContents()
    {
        GUILayout.Space(3f);
        GUILayout.EndVertical();
        EditorGUILayout.EndHorizontal();

        if (mEndHorizontal)
        {
            GUILayout.Space(3f);
            GUILayout.EndHorizontal();
        }

        GUILayout.Space(3f);
    }
}

调用代码:

    /// 
    /// 在Unity编辑器 窗口类中调用(EditorWindow)
    /// 
    private void OnGUI()
    {
        if (YZEditorTools.DrawHeader("TestHeader"))
        {
            YZEditorTools.BeginContents();
            if (GUILayout.Button("Show Notification"))
            {
                this.ShowNotification(new GUIContent("This is a Notification!"));
            }
            GUILayout.Space(20);
            if (GUILayout.Button("Remove Notification"))
            {
                this.RemoveNotification();
            }
            YZEditorTools.EndContents();
        }
    }

更加详细的控件代码链接如下:

上面的代码已经实现了BeginContents控件与DrawHeader控件。

详细的代码参考链接: 

链接: https://pan.baidu.com/s/1IgYy6rev9yEghMKls0vCfQ

提取码: smbd

你可能感兴趣的:(unity3D,编辑器,Unity,Unity编辑器,BeginContents,DrawHeader)