Custom Inspector view panel

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.