public override void OnImportAsset(AssetImportContext ctx) { var prefax = Path.GetExtension(ctx.assetPath).Substring(1); var asset = LuaAsset.CreateInstance <LuaAsset>(); byte[] data; if (compile) { // compile to lua byte code var outDir = $"obj/{Path.GetDirectoryName(ctx.assetPath)}"; if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } var outPath = $"obj/{ctx.assetPath}c"; #if UNITY_EDITOR_OSX var luac = "build/luac/build_unix/luac"; #elif UNITY_EDITOR_WIN var luac = "build/luac/build32/luac.exe"; #elif UNITY_EDITOR_WIN64 var luac = "build/luac/build64/luac.exe"; #endif var prmt = $"{(strip ? "-s" : "")} -o {outPath} -- {ctx.assetPath}"; Compile(luac, prmt); data = File.ReadAllBytes(outPath); } else { data = File.ReadAllBytes(ctx.assetPath); } // TODO: your encode function, like xxtea if (encode) { data = Security.XXTEA.Encrypt(data, LuaAsset.LuaDecodeKey); } asset.data = data; asset.encode = encode; ctx.AddObjectToAsset("main obj", asset, LoadIconTexture(prefax)); ctx.SetMainObject(asset); }
/// <summary> /// 自定义lua加载方法 /// </summary> /// <param name="filepath"></param> /// <returns></returns> private static byte[] CustomLoader(ref string filepath) { if (m_bytesDict.ContainsKey(filepath)) { return(m_bytesDict[filepath]); } string newpath = $"Assets/AssetBundles/Luas/{filepath}" + _luaSuffix; LuaAsset luaAsset = null; #if UNITY_EDITOR luaAsset = UnityEditor.AssetDatabase.LoadAssetAtPath <LuaAsset>(newpath); #endif if (luaAsset != null) { var data = luaAsset.GetDecodeBytes(); m_bytesDict.Add(filepath, data); return(data); } return(null); }
private void OnEnable() { mTarget = target as LuaAsset; }