Keep the. Lua suffix name and package lua files into Assetbundle

Source: Internet
Author: User
Tags foreach lua

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); }
    }
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.