CustomEditor 自定义预览窗

using UnityEngine;
using System.Collections;

public class MyTextureView : MonoBehaviour
{
    public Texture texture;
}
using UnityEngine;
using System.Collections;
using UnityEditor;
//CustomEditor 自定义编辑器
//描述了用于编辑器实时运行类型的一个编辑器类。
//注意:这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。
//编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用。
[CustomEditor(typeof(MyTextureView))]
public class MyTextureViewEditor : Editor
{
    MyTextureView myView;

    void OnEnable()
    {
        myView = target as MyTextureView;
    }
    //重载是否有预览窗口
    public override bool HasPreviewGUI()
    {
        return true;
    }
    //自定义预览窗
    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        if (myView.texture != null)
        {
            GUI.Box(r, myView.texture);
        }
    }
    //重写预览窗标题
    public override GUIContent GetPreviewTitle()
    {
        return new GUIContent("Texture");
    }
}

 编辑MyTextureView 继承至ScriptableObject

using UnityEngine;
using System.Collections;

public class MyTextureView : ScriptableObject
{
    public Texture texture;
}

在MyTextureViewEditor中添加以下代码,可在Project中创建资源

    [MenuItem("Tools/CreateMyViewObj %2")]
    static void CreateMyViewObj()
    {
        Debug.Log("CreateMyViewObj");
        var sc = ScriptableObject.CreateInstance<MyTextureView>();

        //CreateAsset 在指定的路径新建资源。
        //你必须保证使用的路径是一个被支持的扩展
        //('.mat' 代表 materials, '.cubemap' 代表 cubemaps, '.GUISkin' 代表 skins, '.anim' 代表 animations and '.asset' 代表任意其他的资源文件。)
        AssetDatabase.CreateAsset(sc, "Assets/Default.asset");
        //将所有未保存的资源更改写入磁盘。
        AssetDatabase.SaveAssets();
    }

原文地址:https://www.cnblogs.com/martianzone/p/4868196.html