Unity编辑器扩展之修改扩展脚本的成员变量

Unity技术QQ群:484275915,有需要可以加群相互探讨

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(XXX))]//XXX是我们要扩展Inspector面板的脚本名,以下我们称之为目标脚本
public class Edit : Editor
{
     private SerializedProperty XXX;
     private GUIStyle m_guiFoldoutStyle = null;
	 private GUIStyle m_guiLineStyle = null;
	 private bool m_bol;

	 void OnEnable()
	    {
	       XXX = serializedObject.FindProperty("xxx");//xxx是目标脚本的成员变量名
	    }
	 //这个方法是继承的Editor的重写Inspector面板的扩展方法
	 public override void OnInspectorGUI()
	    {
	        DrawDefaultInspector();//绘制原来脚本的成员变量,如果不调用这个方法,之前脚本默认的成员变量则不会被绘制
	        InitializeGUIStyles();
			DevelopmentOfRequired();
	    }
		public void DevelopmentOfRequired()
	    {
	        m_bol = DrawSectionHeader("这里填写题目", m_texIconTexture, m_bol);
	        if (m_bol)
	        {
	            EditorGUILayout.PropertyField(XXX);//在Inspector面板上绘制目标脚本的成员变量
		        serializedObject.ApplyModifiedProperties();//修改目标脚本的成员变量之后将值保存
	        }
	    }
	    //初始化生成Inspector面板的分割线
	    private void InitializeGUIStyles()
	    {
	        if (m_guiFoldoutStyle == null)
	        {
	            m_guiFoldoutStyle = new GUIStyle(EditorStyles.foldout);
	            m_guiFoldoutStyle.fontStyle = FontStyle.Bold;
	            m_guiFoldoutStyle.fixedWidth = 2000.0f;
	        }
	
	        if (m_guiLineStyle == null)
	        {
	            m_guiLineStyle = new GUIStyle(GUI.skin.box);
	            m_guiLineStyle.border.top = 1;
	            m_guiLineStyle.border.bottom = 1;
	            m_guiLineStyle.margin.top = 1;
	            m_guiLineStyle.margin.bottom = 1;
	            m_guiLineStyle.padding.top = 1;
	            m_guiLineStyle.padding.bottom = 1;
	        }
	    }
	    //绘制Inspector面板的表头
	    public bool DrawSectionHeader(string name, Texture2D icon, bool isExpanded)
	    {
	        GUILayout.Box(GUIContent.none, m_guiLineStyle, GUILayout.ExpandWidth(true), GUILayout.Height(1.0f));
	
	        Rect position = GUILayoutUtility.GetRect(40.0f, 2000.0f, 16.0f, 16.0f, m_guiFoldoutStyle);
	        isExpanded = EditorGUI.Foldout(position, isExpanded, new GUIContent(" " + name, icon), true, m_guiFoldoutStyle);
	
	        return isExpanded;
	    }

}

你可能感兴趣的:(unity)