利用此方法也可以对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);
}
}
网友评论