编辑器扩展——模板

来源:互联网 发布:淘宝网原珍向天果官网 编辑:程序博客网 时间:2024/06/08 19:03
using UnityEngine;using System.Collections;using UnityEditor;using System.Text;using UnityEditor.ProjectWindowCallback;using System.IO;public class ExtensionScript {[MenuItem("Assets/Create/Lua Scripts",false,85)]public static void CreateLuaScripts(){ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,                ScriptableObject.CreateInstance<CreateAssetAction>(),                GetSelectedPath()+"/NewLuaScript.cs",                null,                "Assets/Editor/Template/85-Lua-NewLuaScript.lua.txt");}private static string GetSelectedPath(){string selectedPath = "Assets";Object[] selection = Selection.GetFiltered(typeof(Object),SelectionMode.Assets);foreach (Object obj in selection) {selectedPath = AssetDatabase.GetAssetPath(obj);if (!string.IsNullOrEmpty(selectedPath) && File.Exists(selectedPath)) {selectedPath = Path.GetDirectoryName(selectedPath);break;}}return selectedPath;}}public class CreateAssetAction : EndNameEditAction{public override void Action (int instanceId, string pathName, string resourceFile){Object obj = CreateAssetFromTemplate(pathName,resourceFile);ProjectWindowUtil.ShowCreatedAsset(obj);}internal static Object CreateAssetFromTemplate(string pathName, string resourceFile){string fullName = Path.GetFullPath(pathName);StreamReader sr = new StreamReader(resourceFile);string content = sr.ReadToEnd();sr.Close();string fileName = Path.GetFileNameWithoutExtension(fullName);content.Replace("#NAME", fileName);StreamWriter sw = new StreamWriter(fullName,false,Encoding.UTF8);sw.Write(content);sw.Close();AssetDatabase.ImportAsset(pathName);AssetDatabase.Refresh();return AssetDatabase.LoadAssetAtPath(pathName,typeof(Object));}}

0 0
原创粉丝点击