Unity Editor 编辑器扩展一 编辑器特殊文件夹及内置资源读取

目录

  • 目录
  • 编辑器特殊文件夹及内置资源读取
  • 编辑器相关文件夹
  • 读取内置资源包
    • 代码 Test1_1cs
    • 代码Editor1_1cs 这个要放到Editor文件夹内

编辑器特殊文件夹及内置资源读取

使用Unity引擎时 ,为了更快速任性开发,编辑器扩展是必不可少的,子曰:“工欲善其事,必先利其器“说的正是这个道理,做好编辑器开发,在整个项目中就会达到事半功倍的效果,废话不多说,在此纪录下学习过程。

编辑器相关文件夹

1,Editor
此文件夹可以放在Assets根目录下,也可以放在自文件夹里, 编辑器开发脚本要放到这个文件夹内,此文件夹内的脚本只在Unity编辑器内起作用,项目发布时不会被打包。
2,Editor Default Resources
此文件夹必须放在Assets根目录下,注意单词中间有空格,此文件夹用来存储编辑器所需资源,图片和文本,此文件夹里的内容也是不会被打包的,文件的访问方法是:EditorGUIUtility.Load (“文件名”) 。
3,Gizmos
此文件夹也是编辑器绘制Gizmos用的,相当于Resouce文件夹,只不过它是编辑器开发用的,这里不做赘述,以后的文章在说它。
这个系列只写和编辑器开发相关的东西,至于其他特殊文件夹就不在这里说了,最后挂一张测试工程的图片:
这里写图片描述

读取内置资源包

我们也可以用反射拿到编辑器内置的资源,这样我们就可以制作和Unity编译器毫无违和感的扩展了,为了确认里面的内容我们可以看到它打印出来的名称,先截一张图:
Unity Editor 编辑器扩展一 编辑器特殊文件夹及内置资源读取_第1张图片
在通过EditorGUILayout.ObjectField()方法在Inspector面板创建一个texture将它们显示出来,是这样:

Unity Editor 编辑器扩展一 编辑器特殊文件夹及内置资源读取_第2张图片
最后贴上代码

代码 Test1_1.cs

using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class Test1_1 : MonoBehaviour
{
    [SerializeField] 
    public static List<string> texName = new List<string> ();
    [SerializeField] 
    public List texure=new List();
    public List<string> name = new List<string> ();
    void OnEnable ()
    {
        #if UNITY_EDITOR
        name=texName;
        for (int i = 0; i < texName.Count; i++) {
            texure.Add(EditorGUIUtility.Load (texName[i]) as Texture);
        }
        #endif
    }


    [InitializeOnLoadMethod]
    static void GetBultinAssetNames ()
    {
        var flags = BindingFlags.Static | BindingFlags.NonPublic;
        var info = typeof(EditorGUIUtility).GetMethod ("GetEditorAssetBundle", flags);
        var bundle = info.Invoke (null, new object[0]) as AssetBundle;
        foreach (var n in bundle.GetAllAssetNames()) {
            if (n.IndexOf(".png")>=0) {
                texName.Add (n);
            }
            Debug.Log (n);
        }
    }

}

代码Editor1_1.cs 这个要放到Editor文件夹内

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Test1_1))] 
public class Editor1_1 : Editor 
{
    public override void OnInspectorGUI() 
    {
        Test1_1 test = (Test1_1) target;
        string a;
        for (int i = 0; i < test.texure.Count; i++) {
            if (test.texure [i]) {
                a  = test.texure [i].name;
            } else
                a = "";
            test.texure[i]=EditorGUILayout.ObjectField(i+":"+a,test.texure[i],typeof(Texture),true) as Texture;
        }
    }
}

相关资源:http://download.csdn.net/detail/warrenmondeville/9694654
本文链接:http://blog.csdn.net/warrenmondeville/article/details/53247181

你可能感兴趣的:(unity,Editor,unity,Editor,编辑器,扩展,Unity3D)