首页 >Unity3D频道 >【Unity3D拓展编辑器】 > Unity3D研究院编辑器之创建Lua脚本模板(十六)
2015
11-04

Unity3D研究院编辑器之创建Lua脚本模板(十六)

Unity里能创建 c#脚本模板,但是如果我想创建Lua脚本模板怎么办呢?拓展一下编辑器吧。

设置一下Lua脚本的模板地址 :  Assets/Editor/Lua/Template/lua.lua

using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Text;
using UnityEditor.ProjectWindowCallback;
using System.Text.RegularExpressions;

public class Test
{
    [MenuItem("Assets/Create/Lua Script", false, 80)]
    public static void CreatNewLua()
    {
        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
        ScriptableObject.CreateInstance<MyDoCreateScriptAsset>(),
        GetSelectedPathOrFallback() + "/New Lua.lua",
        null,
       "Assets/Editor/Lua/Template/lua.lua");
    }



    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 MyDoCreateScriptAsset : 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, "#NAME#", fileNameWithoutExtension);
        //string text2 = Regex.Replace(fileNameWithoutExtension, " ", string.Empty);
        //text = Regex.Replace(text, "#SCRIPTNAME#", text2);
        //if (char.IsUpper(text2, 0))
        //{
        //    text2 = char.ToLower(text2[0]) + text2.Substring(1);
        //    text = Regex.Replace(text, "#SCRIPTNAME_LOWER#", text2);
        //}
        //else
        //{
        //    text2 = "my" + char.ToUpper(text2[0]) + text2.Substring(1);
        //    text = Regex.Replace(text, "#SCRIPTNAME_LOWER#", text2);
        //}
        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));
    }

}    

 

因为是模板就可以添加一些预制代码在上面, 当填写完类名后可以来替换操作,  例如unity自带的 创建C#文件, 外面写了类名里面也跟这变。Creat -> Lua Script

QQ20151104221646

 

然后可以输入Lua脚本的类名。

QQ20151104221704

 

然后就根据自己创建Lua的文件名 正则替换模板里的东西了。

QQ20151104225139

下载地址:https://bitbucket.org/xuanyusong/project-createlua/

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity3D研究院编辑器之创建Lua脚本模板(十六)》有 16 条评论

  1. 海盗说:

    有个简单粗暴的办法, 直接在EditorDataResourcesScriptTemplates文件夹下建一个文件,文件名类似即可…如89-LuaScript-NewLuaScript.lua.txt 文件名的意义=>优先级为:89;右键Create名为:Lua Sprite;创建初始名为:NewLuaScript.lua 文件里的#SCRIPTNAME# 也会自动替换

  2. 刘剑寒说:

    [MenuItem(“Assets/Create/Lua Script”, false, 0)]这里0改成81,就会在脚本那一栏出现了

  3. 刘剑寒说:

    前两天刚写了一个,看来为了热更新,都要转LUA了

  4. 秦元培说:

    这样就可以直接在项目中创建Lua脚本了,新技能Get!

  5. 白昼说:

    请问下雨凇大大现在使用的是哪个lua框架?

留下一个回复

你的email不会被公开。 必填项已用 * 标注。