改善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获    得):

 
  1. using UnityEngine;

  2. using UnityEditor;

  3. using System.IO;

  4.  
  5. [CanEditMultipleObjects, CustomEditor(typeof(DefaultAsset))]

  6. public class LuaInspector : Editor

  7. {

  8. private GUIStyle m_TextStyle;

  9.  
  10. public override void OnInspectorGUI()

  11. {

  12. if (this.m_TextStyle == null)

  13. {

  14. this.m_TextStyle = "ScriptText";

  15. }

  16. bool enabled = GUI.enabled;

  17. GUI.enabled = true;

  18. string assetPath = AssetDatabase.GetAssetPath(target);

  19. if (assetPath.EndsWith(".lua"))

  20. {

  21. string luaFile = File.ReadAllText(assetPath);

  22. string text;

  23. if (base.targets.Length > 1)

  24. {

  25. text = Path.GetFileName(assetPath);

  26. }

  27. else

  28. {

  29. text = luaFile;

  30. if (text.Length > 7000)

  31. {

  32. text = text.Substring(0, 7000) + "...\n\n<...etc...>";

  33. }

  34. }

  35. Rect rect = GUILayoutUtility.GetRect(new GUIContent(text), this.m_TextStyle);

  36. rect.x = 0f;

  37. rect.y -= 3f;

  38. rect.width = EditorGUIUtility.currentViewWidth + 1f;

  39. GUI.Box(rect, text, this.m_TextStyle);

  40. }

  41. GUI.enabled = enabled;

  42. }

  43. }


效果如下,超过7000字部分或被省略(见上述代码),其实这里也可以直接做成TextBox的形式,即时编辑等等......

  

三.实现Inspector面板的拖拽功能

其实读取lua文件时,我们一般直接使用相关的IO操作API,如果要实现编辑器面板上的拖拽,代码就比较丑陋,这里尝试进行了一次封装,使得拖拽支持DefaultAsset和TextAsset:

 
  1. using UnityEngine;

  2. using UnityEditor;

  3. using System.IO;

  4.  
  5. [System.Serializable]

  6. public class SGTextAsset

  7. {

  8. public Object textAsset;

  9.  
  10. private string text = string.Empty;

  11. private TextAsset asset = null;

  12. public string Text

  13. {

  14. get

  15. {

  16. if (textAsset is DefaultAsset)

  17. {

  18. if (string.IsNullOrEmpty(text))

  19. {

  20. text = File.ReadAllText(AssetDatabase.GetAssetPath(textAsset));

  21. }

  22. return text;

  23. }

  24. else if (textAsset is TextAsset)

  25. {

  26. if (asset == null)

  27. {

  28. asset = textAsset as TextAsset;

  29. }

  30. return asset.text;

  31. }

  32. else

  33. {

  34. return null;

  35. }

  36. }

  37. }

  38. }

最终效果如下:


 

其实在真实的项目中,一般使用Assetbundle进行更新,一般将lua文件后缀改为txt来生成TextAsset对象,进而被打包成AssetBundle。某些平台不支持直接使用IO相关的API直接访问SteamingAssets(如Android),只能使用www,而www只能加载Unity原生支持的对象,这时候如果不更改lua的后缀,就无法被正确的加载了。好消息是,Unity官网上有很多开发者都在请求TextAsset支持lua文件,希望Unity尽快支持吧~

猜你喜欢

转载自blog.csdn.net/qq_14914623/article/details/81084318