Unity——lua文件(.lua后缀的文件)无法被Unity识别问题

官方手册:ScriptedImporter官方手册说明

解决方法:

将如下文件放入Editor文件夹下,等Unity自动刷新或重新打开Unity即可识别。

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);        
        //转成TextAsset(Unity可识别类型)
        var assetsText = new TextAsset(luaTxt);
        //将对象assetText添加到导入操作(AssetImportContext)的结果中。
        ctx.AddObjectToAsset("main obj", assetsText);
        //将对象assetText作为导入操作的主要对象。
        ctx.SetMainObject(assetsText);
    }
}

这样子就可以被正确识别为TextAsset文件一样的东西了,可以在Project窗口搜索栏写入t:TextAsset进行搜索出所有.lua文件啦,打AB包时能正常打包了。

猜你喜欢

转载自blog.csdn.net/qq_39574690/article/details/109696376