大家都知道.lua文件是unity不支持的文件类型,那怎么在unity读取lua脚本呢,有的人采用lua加密后后缀名为.bytes来读取,有的人直接修改为.txt,偶然发现其实unity官方是有相关接口的 https://docs.unity3d.com/ScriptReference/Experimental.AssetImporters.ScriptedImporter.html   

using System.IO;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;
 
[ScriptedImporter(1, ".lua")]
public class LuaImporter : ScriptedImporter
{
    public override void OnImportAsset(AssetImportContext ctx)
    {
        var luaTxt = File.ReadAllText(ctx.assetPath);
        var assetText = new TextAsset(luaTxt);
        ctx.AddObjectToAsset("main obj", assetText);
        //将对象assetText作为导入操作的主要对象。
        ctx.SetMainObject(assetText);
    }
}

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐