改善Unity編輯器對Lua文件的支持

  • 當前版本的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儘快支持吧~



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章