Unity编辑器扩展Texture显示选择框

学习NGUI插件的时候,突然间有一个问题为什么它这些属性可以通过弹出窗口来选中呢? 而我自己写的组件只能使用手动拖放的方式=.=.

Unity开发了组件Inspector视图扩展API,如果我们要写插件方便别人来使用,使用编辑器扩展API让我们的组件显示的更华丽,使用方便

 

Texture弹出选择框选中图片赋值:

1个组件对应1个Edit扩展器,继承Editor必须让入Editor文件夹下

image

MyComponent:

using UnityEngine;
using System.Collections;

public class MyComponent : MonoBehaviour {

    //不让字段显示在Inspector视图上面
    [SerializeField]
    private Rect rectValue;
    [SerializeField]
    private Texture texture;

    public Texture Texture
    {
        get { return texture; }
        set { texture = value; }
    }
    public Rect RectValue
    {
        get { return rectValue; }
        set { rectValue = value; }
    }
}

MyComponentEdit编辑器:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(MyComponent))]
public class MyComponentEdit : Editor 
{
    public override void OnInspectorGUI() 
    {
        MyComponent edit = (MyComponent)target;
        edit.RectValue = EditorGUILayout.RectField("窗口坐标", edit.RectValue);     

        //将贴图属性,以选择框形式显示在Inspector视图上面,方便我们选择贴图
        edit.Texture = EditorGUILayout.ObjectField("增加一个贴图", edit.Texture, typeof(Texture), true) as Texture;
    }
}

image

 

 

本文固定链接: http://www.xuanyusong.com/archives/2202

转载请注明: 雨松MOMO 2013年04月11日 于 雨松MOMO程序研究院 发表

posted @ 2015-02-09 23:16  盘子脸  阅读(4386)  评论(0编辑  收藏  举报