using System.Collections;
|
using System.Collections.Generic;
|
using UnityEditor.ProjectWindowCallback;
|
using UnityEngine;
|
using UnityEditor;
|
using System;
|
using System.IO;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
|
public class CreateLuaClassFile
|
{
|
static string templatePath = "Assets/Editor/ScriptTemplate/LuaWindowTemplate.txt";
|
|
[MenuItem("Assets/Create/Lua/LuaWindow", false, 4)]
|
public static void CreateLuaClass()
|
{
|
var path = GetSelectedPathOrFallback() + "/NewLuaWindow.lua";
|
AssetDatabase.DeleteAsset(path);
|
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<LuaWindowTemplate>(), path, null, templatePath);
|
}
|
|
public static string GetSelectedPathOrFallback()
|
{
|
string path = "Assets";
|
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
|
{
|
path = AssetDatabase.GetAssetPath(obj);
|
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
{
|
path = Path.GetDirectoryName(path);
|
break;
|
}
|
}
|
return path;
|
}
|
|
}
|
|
class LuaWindowTemplate : EndNameEditAction
|
{
|
|
public override void Action(int instanceId, string pathName, string resourceFile)
|
{
|
UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
|
ProjectWindowUtil.ShowCreatedAsset(o);
|
}
|
|
internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
|
{
|
string fullPath = Path.GetFullPath(pathName);
|
|
StreamReader streamReader = new StreamReader(resourceFile);
|
string text = streamReader.ReadToEnd();
|
streamReader.Close();
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
|
text = Regex.Replace(text, "#DateTime#", System.DateTime.Now.ToLongDateString());
|
|
bool encoderShouldEmitUTF8Identifier = true;
|
bool throwOnInvalidBytes = false;
|
UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
|
bool append = false;
|
StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
|
streamWriter.Write(text);
|
streamWriter.Close();
|
AssetDatabase.ImportAsset(pathName);
|
return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
|
}
|
|
}
|