(轉 )Unity對Lua的編輯器拓展

來源:互聯網
上載者:User

標籤:相關   dea   textbox   檔案夾   teaming   開啟   def   south   span   

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

  • 目前的版本的Unity(截至Unity5.5.x)中TextAsset類不支援尾碼為lua的檔案,將lua檔案匯入到項目中後,其會被識別為類型為DefaultAsset的檔案,即不被Unity原生支援。此外在編輯器模式下也無法直接建立lua檔案,需要在檔案夾中手動進行建立。經過一番探索,簡單實現了在編輯器中建立lua檔案和預覽lua檔案的功能。
  一.在編輯器下建立Lua檔案
  • 開啟Unity安裝目錄下的Editor\Data\Resources\ScriptTemplates,可以看到如下文本:
  • 可以猜測Unity在啟動時會根據這裡的檔案構建編輯器裡的菜單(這一點可以反編譯UnityEditor.dll進行驗證):
  • 仿造標題格式添加一個87-Lua Script-NewLuaScript.lua.txt的檔案,檔案內容可隨意,其中#SCRIPTNAME#會被替換為建立時輸入的名字。重啟Unity,可以發現在建立菜單裡已經有了這個Lua Script:
  • 點擊建立後會走建立指令碼一樣的流程:
  • 這樣就能在項目中正常的建立Lua檔案了。
     二.在編輯器下預覽Lua檔案

       由於lua檔案會被識別為DefaultAsset,我們可以通過重寫DefaultAsset的Inspector面板來實現預覽,這裡直接抄了一下TextAssetInspector的代碼(反編譯UnityEditor.dll獲    得):

using UnityEngine;using UnityEditor;using System.IO;[CanEditMultipleObjects, CustomEditor(typeof(DefaultAsset))]public class LuaInspector : Editor{    private GUIStyle m_TextStyle;    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;    }}

效果如下,超過7000字部分或被省略(見上述代碼),其實這裡也可以直接做成TextBox的形式,即時編輯等等......

 

  三.實現Inspector面板的拖拽功能

其實讀取lua檔案時,我們一般直接使用相關的IO操作API,如果要實現編輯器面板上的拖拽,代碼就比較醜陋,這裡嘗試進行了一次封裝,使得拖拽支援DefaultAsset和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;            }        }    }}

最終效果如下:

 



其實在真實的項目中,一般使用Assetbundle進行更新,一般將lua檔案尾碼改為txt來產生TextAsset對象,進而被打包成AssetBundle。某些平台不支援直接使用IO相關的API直接存取SteamingAssets(如Android),只能使用www,而www只能載入Unity原生支援的對象,這時候如果不更改lua的尾碼,就無法被正確的載入了。好訊息是,Unity官網上有很多開發人員都在請求TextAsset支援lua檔案,希望Unity儘快支援吧~

(轉 )Unity對Lua的編輯器拓展

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.