编辑器扩展——模板

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));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值