分享

unity3d拓展编辑器Editor的使用

 kiki的号 2017-04-26

Editor可以拓展Inspector窗口


可以通过代码自己绘制监测面板


先来看一个效果:



复制代码

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEditor;
 4 
 5 //Editor:使用这个为你的对象来创建自己的自定义检视面板和编辑器。
 6 public class myScript : MonoBehaviour
 7 {
 8     /*
 9        [HideInInspector]
10      * [SerializeField]
11      * 意思是:隐藏数据在面板,并且序列化。它可以帮我们永久保存数据,
12      * 如果不加的话数值就会重置。就好比我先添加了一个贴图,一运行游戏刚刚添加的贴图就变成null了。
13      * 
14      * 比如一个属性希望被外部访问(必须是public)。但不希望显示在检视面板中
15      * 
16      * 那么就:
17      * [HideInInspector]
18      * public int s;
19      * 
20      */
21 
22     [HideInInspector]
23     [SerializeField]
24     Rect pRectValue;
25 
26     public Rect PRectValue
27     {
28         get { return pRectValue; }
29         set { pRectValue = value; }
30     }
31 
32     [HideInInspector]
33     [SerializeField]
34     Texture pTexture;
35 
36     public Texture PTexture
37     {
38         get { return pTexture; }
39         set { pTexture = value; }
40     }
41 
42     [HideInInspector]
43     [SerializeField]
44     private int a;
45 
46     public int A
47     {
48         get { return a; }
49         set { a = value; }
50     }
51 }
52 
53 [CustomEditor(typeof(myScript))]
54 class show : Editor
55 {
56     //在这里方法中就可以绘制面板。
57     public override void OnInspectorGUI()
58     {
59         //得到myScript对象
60         myScript test = (myScript)target;
61         //绘制一个窗口
62         test.PRectValue = EditorGUILayout.RectField("窗口坐标", test.PRectValue);
63         //绘制一个贴图槽
64         test.PTexture = EditorGUILayout.ObjectField("增加一个贴图", test.PTexture, typeof(Texture), true) as Texture;
65 
66         test.A = EditorGUILayout.IntField("最大值", test.A);
67     }
68 }

复制代码

这个类并没有放进Editor文件夹中。


其实我们完全可以把需要序列化的类和编辑类分开;


把myScript类修改:


复制代码

using UnityEngine;
using System.Collections;
using UnityEditor;

//Editor:使用这个为你的对象来创建自己的自定义检视面板和编辑器。
public class myScript : MonoBehaviour
{
    /*
       [HideInInspector]
     * [SerializeField]
     * 意思是:隐藏数据在面板,并且序列化。它可以帮我们永久保存数据,
     * 如果不加的话数值就会重置。就好比我先添加了一个贴图,一运行游戏刚刚添加的贴图就变成null了。
     * 
     * 比如一个属性希望被外部访问(必须是public)。但不希望显示在检视面板中
     * 
     * 那么就:
     * [HideInInspector]
     * public int s;
     * 
     */

    [HideInInspector]
    [SerializeField]
    Rect pRectValue;

    public Rect PRectValue
    {
        get { return pRectValue; }
        set { pRectValue = value; }
    }

    [HideInInspector]
    [SerializeField]
    Texture pTexture;

    public Texture PTexture
    {
        get { return pTexture; }
        set { pTexture = value; }
    }

    [HideInInspector]
    [SerializeField]
    private int a;

    public int A
    {
        get { return a; }
        set { a = value; }
    }
}

复制代码




创建一个类:editors.cs,放进Editor文件夹中。


复制代码

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(myScript))]
public class editors : Editor
{
    //在这里方法中就可以绘制面板。
    public override void OnInspectorGUI()
    {
        //得到myScript对象
        myScript test = (myScript)target;
        //绘制一个窗口
        test.PRectValue = EditorGUILayout.RectField("窗口坐标1", test.PRectValue);
        //绘制一个贴图槽
        test.PTexture = EditorGUILayout.ObjectField("增加一个贴图1", test.PTexture, typeof(Texture), true) as Texture;

        test.A = EditorGUILayout.IntField("最大值1", test.A);
    }
}

复制代码

看效果:



 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多