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程序研究院 發表


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM