Unity Editor 基础篇(十三):更改资源的Inspector显示

代码参自:http://anchan828.github.io/editor-manual/web/customeditor.html

之前已经讨论过场景中物体在Inspector面板的显示的更改,今天讨论一下Project面板中资源在Inspector面板的显示。例如,点击Project里一个C#脚本,在Inspector面板就会出现脚本内容的预览,但是项目中的lua文件点击了却不会这样显示。

 

所以,我们点击资源时,更具资源的后缀名来选择显示在Inspector面板的内容,例如,lua文件点击了可以直接把文本内容显示在Inspector面板。

效果:(点击的是lua文件,为了保密,内容替换为一组任意文字)

 

代码:

因为资源文件可能有很多种后缀名,不同的可能需求也不一样,所以我们写个属性类,不同需求的使用不同的属性:

 

 
  1. public class CustomAssetAttribute : Attribute {

  2.  
  3. public string[] extensions; //不同属性对应不同需求,同一需求可能有几种类型文件

  4.  
  5. public CustomAssetAttribute(params string[] extention)

  6. {

  7. this.extensions = extention;

  8. }

  9. }

 

 

然后以lua文件这种文本资源为例,面板绘制:

 

 
  1. /// <summary>

  2. /// lua文件绘制方式

  3. /// </summary>

  4. [CustomAsset(".lua")] //绘制方式绑定以.lua为后缀的资源文件

  5. public class LuaInspector : Editor

  6. {

  7. private string content;

  8. private bool show = false;

  9. private string showKey = "LuaInspectorShown";

  10.  
  11. void OnEnable()

  12. {

  13. string path = Application.dataPath + "/" + AssetDatabase.GetAssetPath(Selection.activeObject).Substring(7);

  14.  
  15. try

  16. {

  17. TextReader tr = new StreamReader(path);

  18. content = tr.ReadToEnd(); //读取文件中所有文本

  19. tr.Close();

  20. }

  21. catch (Exception e)

  22. {

  23. Debug.Log(e);

  24. }

  25. }

  26.  
  27. public override void OnInspectorGUI()

  28. {

  29. show = EditorPrefs.GetBool("showKey");

  30. show = GUILayout.Toggle(show, "Show Lua Content");

  31. EditorPrefs.SetBool("showKey", show);

  32. if (show) //这里为了性能,加了开关

  33. {

  34. GUILayout.Label(content);

  35. }

  36. }

  37. }


 

 


目前可见这两个文件关联性不大,第二个脚本设置了绘制方式,但是还没法使以.lua后缀的文件就以这种方式绘制了,于是有了:

 

 

 
  1. [CustomEditor(typeof(DefaultAsset))] //自定义--资源的Inspector绘制方式

  2. public class DefaultAssetInspector : Editor

  3. {

  4. private Editor editor; //资源文件预览绘制方式

  5. private static Type[] customAssetTypes;

  6.  
  7. [InitializeOnLoadMethod] //Unity软件启动事件

  8. static void Init()

  9. {

  10. customAssetTypes = GetCustomAssetTypes();

  11. }

  12.  
  13. /// <summary>

  14. /// 获取所有带自定义属性CustomAssetAttribute的类

  15. /// </summary>

  16. private static Type[] GetCustomAssetTypes()

  17. {

  18. var assemblyPaths = Directory.GetFiles("Library/ScriptAssemblies", "*.dll");

  19. var types = new List<Type>();

  20. var customAssetTypes = new List<Type>();

  21.  
  22. foreach (var assembly in assemblyPaths

  23. .Select(assemblyPath => Assembly.LoadFile(assemblyPath)))

  24. {

  25. types.AddRange(assembly.GetTypes());

  26. }

  27. //到这一步,所有的类都在types这个列表里

  28. foreach (var type in types)

  29. {

  30. //在每个类中尝试寻找使用自定义属性CustomAssetAttribute

  31. var customAttributes =

  32. type.GetCustomAttributes(typeof(CustomAssetAttribute), false) //获取自定义属性CustomAssetAttribute

  33. as CustomAssetAttribute[];

  34.  
  35. if (0 < customAttributes.Length)

  36. customAssetTypes.Add(type); //找到了就把这个类存到customAssetTypes列表中

  37. }

  38.  
  39. //so返回的是所有程序集中找到的带CustomAssetAttribute属性的类的数组(如前面的LuaInspector类)

  40. return customAssetTypes.ToArray();

  41. }

  42.  
  43. /// <summary>

  44. /// 根据扩展名去含指定属性的类数组中获取对应类(对比属性的extensions值)

  45. /// </summary>

  46. private Type GetCustomAssetEditorType(string extension)

  47. {

  48. foreach (var type in customAssetTypes)

  49. {

  50. var customAttributes =

  51. type.GetCustomAttributes(typeof(CustomAssetAttribute), false)

  52. as CustomAssetAttribute[];

  53.  
  54. foreach (var customAttribute in customAttributes)

  55. {

  56. if (customAttribute.extensions.Contains(extension))

  57. return type;

  58. }

  59. }

  60. return typeof(DefaultAsset);

  61. }

  62.  
  63. private void OnEnable()

  64. {

  65. var assetPath = AssetDatabase.GetAssetPath(target);

  66.  
  67. var extension = Path.GetExtension(assetPath); //获取扩展名

  68. //根据扩展名去得对应的继承自Editor的绘制方式

  69. var customAssetEditorType = GetCustomAssetEditorType(extension);

  70. //使目标以指定绘制方式绘制

  71. editor = CreateEditor(target, customAssetEditorType);

  72. }

  73.  
  74. //先去程序集中遍历寻找带有CustomAssetAttribute这个自定义的属性的类,找到这些个类后,就得到了后缀名和对应绘制方式

  75. //(因为这个属性关联了后缀名和绘制方式)

  76. //所以我们去用现在点击的资源的后缀名去获取对应的绘制方式,用这种方式绘制Inspector面板

  77.  
  78. public override void OnInspectorGUI()

  79. {

  80. if (editor != null)

  81. {

  82. GUI.enabled = true;

  83. editor.OnInspectorGUI();

  84. }

  85. }

  86.  
  87. public override bool HasPreviewGUI() //是否有Inspector面板下面的预览小界面

  88. {

  89. return editor != null ? editor.HasPreviewGUI() : base.HasPreviewGUI();

  90. }

  91.  
  92. public override void OnPreviewGUI(Rect r, GUIStyle background) //小界面内容

  93. {

  94. if (editor != null)

  95. editor.OnPreviewGUI(r, background);

  96. }

  97.  
  98. public override void OnPreviewSettings() //小界面横条设置内容

  99. {

  100. if (editor != null)

  101. editor.OnPreviewSettings();

  102. }

  103.  
  104. public override string GetInfoString()

  105. {

  106. return editor != null ? editor.GetInfoString() : base.GetInfoString();

  107. }

  108.  
  109. }


代码很详细,要说的都在代码里,然后OK了。

 

但是,lua脚本过长,Unity里会报错没法绘制了,所以脚本大小要控制,进一步方法待思考开发。

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

简单说一下Inspector面板下面的预览小面板,一般模型什么的会用到这个。

如,上述改为:

 

 
  1. //是否有Inspector面板下面的预览小界面

  2. public override bool HasPreviewGUI()

  3. {

  4. return true;

  5. //return editor != null ? editor.HasPreviewGUI() : base.HasPreviewGUI();

  6. }

  7.  
  8. public override void OnPreviewGUI(Rect r, GUIStyle background)

  9. {

  10. //if (editor != null)

  11. // editor.OnPreviewGUI(r, background);

  12. GUI.Label(r, "Preview \n This is preview content!");

  13. }

  14.  
  15. public override void OnPreviewSettings() //小界面横条设置内容

  16. {

  17. //if (editor != null)

  18. // editor.OnPreviewSettings();

  19. GUIStyle preLabel = new GUIStyle("preLabel");

  20. GUIStyle preButton = new GUIStyle("preButton");

  21.  
  22. GUILayout.Label("EanLabel", preLabel);

  23. GUILayout.Button("EamBtn", preButton);

  24. }

  25.  
  26. public override string GetInfoString()

  27. {

  28. return "Here Info";// editor != null ? editor.GetInfoString() : base.GetInfoString();

  29. }

  30.  
  31. public override GUIContent GetPreviewTitle()

  32. {

  33. return new GUIContent("Here Title");

  34. }

结合图片说明一下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack Yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值