Unity 实现最简单的脚本编辑器

作者: 2b75747cf703 | 来源:发表于2016-02-21 11:56 被阅读739次

利用此方法也可以对DefaultAsset类型资源自定义编辑器,比如.lua文件。

using UnityEngine;
using UnityEditor;
using System.IO;

[CustomEditor(typeof(MonoScript))]

public class MonoScriptEditor : Editor
{
    private string assetPath;

    private string originalText;
    private string modifiedText;

    void Awake()
    {
        GUI.enabled = true;

        assetPath = AssetDatabase.GetAssetPath(target);

        originalText = File.ReadAllText(assetPath);
        modifiedText = originalText.Clone() as string;
    }

    void OnDestroy()
    {
        if(modifiedText != originalText)
        {
            if (EditorUtility.DisplayDialog("", "是否保存 " + assetPath + " ?", "确定", "取消"))
            {
                File.WriteAllText(assetPath, modifiedText);
                AssetDatabase.Refresh();
            }
        }
    }

    public override void OnInspectorGUI()
    {
        modifiedText = EditorGUILayout.TextArea(modifiedText);
    }
}

相关文章

网友评论

    本文标题:Unity 实现最简单的脚本编辑器

    本文链接:https://www.haomeiwen.com/subject/nvnvkttx.html