The Inspector panel in Unity displays the following attributes:
(1) C # and basic types provided by unity;
(2) customize the type and use the [system. serializable] keyword for serialization, for example:
[System.Serializable]public class TestClass{ public Vector3 vec = Vector3.zero; public Color clr = Color.green;}
You can also use [system. nonserialized] to mark attributes that do not need to be displayed, for example:
public class TestClass{ [System.NonSerialized] public Vector3 vec = Vector3.zero;}
Sometimes, if the default inspector of U3D cannot meet the requirements, you can customize the Inspector panel of a specific type:Write a corresponding editor class and rewrite its oninspector method.For example, we have the following types:
using UnityEngine;using System.Collections;[System.Serializable]public class TestClass{ public Vector3 vec = Vector3.zero; [System.NonSerialized] public Color clr = Color.green;}[ExecuteInEditMode()][RequireComponent(typeof(TestComponent))]public class TestInspector : MonoBehaviour { public Vector3 lookAtPoint = Vector3.zero; public Vector3 pos = Vector3.zero; public TestClass testObj = new TestClass(); void Update() { transform.LookAt(lookAtPoint); transform.position = pos; }}
Then add the following script in the editor directory:
Using unityengine; using unityeditor; using system. collections; [customeditor (typeof (testinspector)] public class testinspectoreditor: Editor {private serializedobject OBJ; private serializedproperty lookatpoint; private serializedproperty Pos; private serializedproperty testobj; // when the gameobject of the testinspector component is selected, the void onenable () {OBJ = new serializedobject (target); lookatpoint = obj. findproperty ("lookatpoint"); Pos = obj. findproperty ("POS"); testobj = obj. findproperty ("testobj");} // rewrite Inspector view panel public override void oninspectorgui () {obj. update (); editorguilayout. propertyfield (lookatpoint); editorguilayout. propertyfield (POS); editorguilayout. propertyfield (testobj, true); // The second parameter indicates that a subnode needs to display obj. applymodifiedproperties ();}}
In the oninspector function, you can achieve the desired effect.
Note the following points in the above Code:
(1) [executeineditmode ()] This function allows the code to run in editing mode without running the game;
(2) The testinspectoreditor class must inherit from the editor. customeditor tells U3D which component type needs to be customized and oninspectorgui is called when displaying the Inspector Panel of the component;
(3) serializedobject serializes objects, serializedproperty, and editor. These attributes are used together to access and update component attributes. seriailzedproperty can access all basic types in a common way, including array control.
In addition, you can call the drawdefaultinspector function in oninspectorgui to display the default supervisor panel content.
This post is well written: http://blog.csdn.net/lilanfei/article/details/7680802