Improving the Unity Editor's support for LUA files

Source: Internet
Author: User
Tags lua
The Textasset class in the current version of Unity (as of unity5.5.x) does not support files with the suffix Lua, and when the Lua file is imported into the project, it is recognized as a file of type Defaultasset, which is not supported by Unity native. In addition, LUA files cannot be created directly in the editor mode and need to be created manually in the folder. After exploring, it is simple to implement the ability to create LUA files and preview lua files in the editor. I. Creating LUA files under the editorOpen the Editor\data\resources\scripttemplates in the Unity installation directory to see the following text: It is possible to guess that unity will build the menu in the editor based on the files here (which can be verified by deserializing UnityEditor.dll): Add a 87-lua to the title format Script-newluascript.lua.txt files, the contents of the file can be arbitrary, where #scriptname# will be replaced with the name entered at the time of creation. To restart Unity, you can see that you already have this LUA Script in the Create menu:
Click Create to follow the same process as creating a script:
This makes it possible to create LUA files normally in the project. two. Previewing LUA files under the editor

Since LUA files are recognized as Defaultasset, we can implement previews by overriding the Defaultasset Inspector panel, Here's a direct copy of the Textassetinspector code (decompile UnityEditor.dll obtained):

Using Unityengine;
Using Unityeditor;

Using System.IO; [Caneditmultipleobjects, Customeditor (typeof (Defaultasset))] public class Luainspector:editor {private Guistyle m_t

    Extstyle;  public override void Oninspectorgui () {if (This.m_textstyle = = null) {This.m_textstyle =
        "ScriptText";
        } bool enabled = gui.enabled;
        Gui.enabled = true;
        String assetpath = Assetdatabase.getassetpath (target);
            if (Assetpath.endswith (". Lua")) {string luafile = File.readalltext (Assetpath);
            string text;
            if (Base.targets.Length > 1) {text = Path.getfilename (Assetpath);
                } else {text = Luafile; if (text. Length > 7000) {text = text. Substring (0, 7000) + "... \n\n<...etc ...
                > "; }} Rect rect = GuilayoututilitY.getrect (new Guicontent (text), this.m_textstyle);
            Rect.x = 0f;
            Rect.y-= 3f;
            Rect.width = editorguiutility.currentviewwidth + 1f; Gui.
        Box (rect, text, This.m_textstyle);
    } gui.enabled = enabled;
 }
}

The effect is as follows, more than 7000 words part or be omitted (see above code), actually here also can directly make textbox form, instant Edit and so on ...


Three. Implement the drag-and-drop function of the Inspector panel

In fact, when reading LUA files, we generally directly use the relevant IO operation API, if you want to implement the editor panel drag and drop, the code is ugly, here try to do a package, so drag support Defaultasset and Textasset:

Using Unityengine;
Using Unityeditor;
Using System.IO;

[System.serializable]
public class Sgtextasset
{public
    Object textasset;
    
    private string text = String. Empty;
    Private Textasset asset = null;
    public string Text
    {
        get
        {
            if (Textasset is Defaultasset)
            {
                if (string. IsNullOrEmpty (text))
                {
                    text = File.readalltext (Assetdatabase.getassetpath (Textasset));
                }
                return text;
            }
            else if (Textasset is Textasset)
            {
                if (asset = = null)
                {
                    asset = Textasset as Textasset;
                }
                return asset.text;
            }
            else
            {
                return null;}}}
}
The final effect is as follows:



In fact, in the real project, generally use Assetbundle to update, the Lua file suffix is generally changed to TXT to generate Textasset objects, and then packaged into Assetbundle. Some platforms do not support direct access to steamingassets (such as Android) directly using the IO-related APIs, only www, and www can only load unity native supported objects, which cannot be loaded correctly if the Lua suffix is not changed. The good news is that there are many developers on the Unity website who are requesting Textasset support for LUA files, and hope Unity will support it as soon as possible.



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.