Go Unity-to-Lua editor expansion

Source: Internet
Author: User
Tags lua

Turn http://blog.csdn.net/ZhangDi2017/article/details/61203505

    • 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 editor
    • Open 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):
    • Copy the title format add a 87-lua script-newluascript.lua.txt file, 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):

usingUnityengine;usingUnityeditor;usingSystem.IO; [Caneditmultipleobjects, Customeditor (typeof(Defaultasset))] Public classluainspector:editor{PrivateGuistyle M_textstyle;  Public Override voidOninspectorgui () {if( This. M_textstyle = =NULL)        {             This. M_textstyle ="ScriptText"; }        BOOLEnabled =gui.enabled; Gui.enabled=true; stringAssetpath =Assetdatabase.getassetpath (target); if(Assetpath.endswith (". Lua"))        {            stringLuafile =File.readalltext (Assetpath); stringtext; 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 (NewGuicontent (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:

usingUnityengine;usingUnityeditor;usingSystem.IO; [System.serializable] Public classsgtextasset{ PublicObject Textasset; Private stringText =string.    Empty; PrivateTextasset Asset =NULL;  Public stringText {Get        {            if(Textasset isDefaultasset) {                if(string. IsNullOrEmpty (text)) {text=File.readalltext (Assetdatabase.getassetpath (Textasset)); }                returntext; }            Else if(Textasset isTextasset) {                if(Asset = =NULL) {Asset= Textasset asTextasset; }                returnAsset.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.

Go Unity-to-Lua editor expansion

Related Article

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.