Development Ulua comrades must have met a big hole, that is, when the LUA files are packaged into Assetbundle, the unity engine does not recognize LUA files, and finally had to detour the Lua file into a TXT file before packaging. At the end of the development, it was developed directly with TXT. So many of the web-based LUA editor is basically useless, code hints, and code errors will give some people a lot of trouble, for just learning Lua is a very big blow, so that the threshold of development is high, for the whole project is unfavorable.
I chose a curve to save the way, the idea is to pack assetbundle, the. Lua file copies a copy, the same path is saved as a. txt file. Then call the package Assetbundle instructions, package, and then delete the TXT file after packaging, so that users can only in the. lua file development.
Directly on the Unityeditor code. (Not very elegant, but I can barely understand it.)
Using Unityengine;
Using Unityeditor;
Using System.IO; public class Exportassetbundles {//Add menu in Unity Editor [MenuItem ("Assets/build assetbundle from Selection")] St atic void ExportResourceRGB2 () {//Open the Save panel to get the path selected by the user string path = Editorutility.savefolderpanel ("Selec
T Lua Script Folder "," "," ");
DirectoryInfo Thefolder = new DirectoryInfo (path);
String Bundlepath = path + "/" + Thefolder.name + ". Assetbundle";
Changelua2txt (Thefolder, path);
Bundlepath = Editorutility.savefilepanel ("Save Resource", "" ", Thefolder.name," Assetbundle ");
Assetdatabase.refresh (); if (path. Length! = 0) {//Select the object to save object[] selection = selection.getfiltered (typeof (object), Selectionmo De.
Deepassets); Packaged Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Bundlepath, Buildassetbundleoptions.col lectdependencies | Buildassetbundleoptions.completeassets, Buildtarget.Standalonewindows);
Deletetxt (Thefolder);
Assetdatabase.refresh (); }} static private void Changelua2txt (DirectoryInfo thefolder, string path) {foreach (var nextfile in the
Folder.getfiles ("*.lua")) {var Utf8withoutbom = new System.Text.UTF8Encoding (false);
StreamReader sr = new StreamReader (Nextfile.fullname, Utf8withoutbom); String text = Sr.
ReadToEnd ();
FileStream fs = new FileStream (path + "/" + path.changeextension (nextfile.name, "txt"), filemode.create);
StreamWriter SW = new StreamWriter (FS, Utf8withoutbom); Sw.
Write (text); Sr.
Close (); Sw.
Flush (); Sw.
Close (); Fs.
Close ();
} foreach (DirectoryInfo nextfolder in Thefolder.getdirectories ()) {changelua2txt (nextfolder, path); }} static private void Deletetxt (DirectoryInfo thefolder) {foreach (var nextfile in Thefolder.
GetFiles ("*.txt")) {nextfile.delete ();
} foreach (DirectoryInfo nextfolder in Thefolder.getdirectories ()) {deletetxt (Nextfolder); }
}
}