Unity 在Project目录创建Lua脚本

3 篇文章 0 订阅
3 篇文章 0 订阅

选中Project目录中你需要创建脚本的文件夹,点击右键,然后选择创建Lua脚本。

创建Lua脚本后,将脚本文件改为rename mode,修改脚本名字。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;

public class CreateLuaScriptEditor : Editor {
    static double renameTime = 0;

    [MenuItem("Assets/Create/Lua Script")]
    public static void CreateLua()
    {
        UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
        string path = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[0]);
        DateTime dt = DateTime.Now;
        string context = string.Format("--Author : {0}\n--Date : {1}", GetUserName(), dt.ToShortDateString().ToString());

        string fileName = "LuaScript.lua";
        if (File.Exists(path + "/" + fileName))
        {
            fileName = "LuaScript 1.lua";
        }
        File.AppendAllText(path + "/" + fileName, context);
        AssetDatabase.Refresh();
        string s = AssetDatabase.GetAssetPath(arr[0]) + "/" + fileName;

        Selection.activeObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);

        renameTime = EditorApplication.timeSinceStartup + 0.1d;
        EditorApplication.update += EngageRenameMode;
    }

    // 重命名
    public static void EngageRenameMode()
    {
        if (EditorApplication.timeSinceStartup >= renameTime)
        {
            EditorApplication.update -= EngageRenameMode;
            EditorApplication.ExecuteMenuItem("Window/Project"); // focus on the project window
            EditorWindow.focusedWindow.SendEvent(new Event() { keyCode = KeyCode.F2, type = EventType.KeyDown });
        }

        
    }
    

    public static string GetUserName()
    {
        try
        {
            return Environment.UserName;
        }
        catch
        {
            return "unknown";
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值