Unity3D编辑器:自定义Inspector属性面板显示效果

今天同事让我,帮他写一个自己定义编辑器Inspector属性面板显示效果,我想到的是Unity的编辑器类,它里面有好多也可以自定义效果的方法,其实主要用到的就是EditorGUI的东西,下面我将一些常见的方法贴出来,大家做为参考吧。

这个是普通的类:

using UnityEngine;
using UnityEngine.UI;
using System;

public class CustomClass : MonoBehaviour
{
    [HideInInspector]
    public GameObject go;

    [HideInInspector]
    public Point point;

    [HideInInspector]
    public bool on;

    [HideInInspector]
    public bool off;

    [HideInInspector]
    public Transform trans;

    [HideInInspector]
    public Vector3 position;

    [HideInInspector]
    public int index;

    [HideInInspector]
    public float flo;

    [HideInInspector]
    public Image image;
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

[Serializable]
public struct Point
{
    public int x;
    public int y;
    public override string ToString()
    {
        return "(" + x + "," + y + ")";
    }
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

这个是Editor类:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(CustomClass))]
public class CustomClassEditor : Editor
{
    CustomClass _target;
    // Use this for initialization
    void OnEnable()
    {
        _target = (CustomClass)target;
    }

    // Update is called once per frame
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        //GUI.backgroundColor = Color.red;//背景颜色
        GUI.color = Color.green;//整体颜色
        //GUI.contentColor = Color.yellow;//字体颜色
        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.PrefixLabel("Point");
        GUILayout.Label("X");
        _target.point.x = EditorGUILayout.IntField(_target.point.x);
        GUILayout.Label("Y");
        _target.point.y = EditorGUILayout.IntField(_target.point.y);

        EditorGUILayout.EndHorizontal();


        EditorGUILayout.BeginHorizontal();
        
        _target.on = EditorGUILayout.Toggle("On",_target.on);

        EditorGUILayout.EndHorizontal();

        
        EditorGUILayout.BeginHorizontal();
        
        _target.off = EditorGUILayout.Toggle("Off",_target.off);

        EditorGUILayout.EndHorizontal();

        if (_target.on)
        {
            EditorGUILayout.BeginHorizontal();

            _target.go = (GameObject)EditorGUILayout.ObjectField("Go", _target.go, typeof(GameObject), true);

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            
            _target.trans = (Transform)EditorGUILayout.ObjectField("Trans", _target.trans, typeof(GameObject), true);

            EditorGUILayout.EndHorizontal();
        }


        EditorGUILayout.BeginHorizontal();

        _target.position = EditorGUILayout.Vector3Field("Position", _target.position);

        EditorGUILayout.EndHorizontal();


        EditorGUILayout.BeginHorizontal();

        _target.index = EditorGUILayout.IntField("Index", _target.index);

        EditorGUILayout.EndHorizontal();


        EditorGUILayout.BeginHorizontal();

        _target.flo = EditorGUILayout.FloatField("Flo", _target.flo);

        EditorGUILayout.EndHorizontal();


        EditorGUILayout.BeginHorizontal();
        
        _target.image = (Image)EditorGUILayout.ObjectField("Go", _target.image, typeof(Image), true);

        EditorGUILayout.EndHorizontal();

        if (GUI.changed)
        {
            EditorUtility.SetDirty(_target);
        }
    }
}
图1

只要将Editor类放到任意目录下的Editor文件夹下,这个类就会生效。如下图:

图2

最终在Inspector属性面板显示的效果如下图:

大家有什么不懂的,就留言吧。希望呢给大家带来帮助。

You May Also Like

About the Author: 大腿Plus

1 Comment

发表评论