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