SteamVR_RenderModelEditor.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Custom inspector display for SteamVR_RenderModel
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System.Text;
  9. using System.Collections.Generic;
  10. [CustomEditor(typeof(SteamVR_RenderModel)), CanEditMultipleObjects]
  11. public class SteamVR_RenderModelEditor : Editor
  12. {
  13. SerializedProperty script, index, modelOverride, shader, verbose, createComponents, updateDynamically;
  14. static string[] renderModelNames;
  15. int renderModelIndex;
  16. void OnEnable()
  17. {
  18. script = serializedObject.FindProperty("m_Script");
  19. index = serializedObject.FindProperty("index");
  20. modelOverride = serializedObject.FindProperty("modelOverride");
  21. shader = serializedObject.FindProperty("shader");
  22. verbose = serializedObject.FindProperty("verbose");
  23. createComponents = serializedObject.FindProperty("createComponents");
  24. updateDynamically = serializedObject.FindProperty("updateDynamically");
  25. // Load render model names if necessary.
  26. if (renderModelNames == null)
  27. {
  28. renderModelNames = LoadRenderModelNames();
  29. }
  30. // Update renderModelIndex based on current modelOverride value.
  31. if (modelOverride.stringValue != "")
  32. {
  33. for (int i = 0; i < renderModelNames.Length; i++)
  34. {
  35. if (modelOverride.stringValue == renderModelNames[i])
  36. {
  37. renderModelIndex = i;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. static string[] LoadRenderModelNames()
  44. {
  45. var results = new List<string>();
  46. results.Add("None");
  47. using (var holder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
  48. {
  49. var renderModels = holder.instance;
  50. if (renderModels != null)
  51. {
  52. uint count = renderModels.GetRenderModelCount();
  53. for (uint i = 0; i < count; i++)
  54. {
  55. var buffer = new StringBuilder();
  56. var requiredSize = renderModels.GetRenderModelName(i, buffer, 0);
  57. if (requiredSize == 0)
  58. continue;
  59. buffer.EnsureCapacity((int)requiredSize);
  60. renderModels.GetRenderModelName(i, buffer, requiredSize);
  61. results.Add(buffer.ToString());
  62. }
  63. }
  64. }
  65. return results.ToArray();
  66. }
  67. public override void OnInspectorGUI()
  68. {
  69. serializedObject.Update();
  70. EditorGUILayout.PropertyField(script);
  71. EditorGUILayout.PropertyField(index);
  72. //EditorGUILayout.PropertyField(modelOverride);
  73. GUILayout.BeginHorizontal();
  74. GUILayout.Label(new GUIContent("Model Override", SteamVR_RenderModel.modelOverrideWarning));
  75. var selected = EditorGUILayout.Popup(renderModelIndex, renderModelNames);
  76. if (selected != renderModelIndex)
  77. {
  78. renderModelIndex = selected;
  79. modelOverride.stringValue = (selected > 0) ? renderModelNames[selected] : "";
  80. }
  81. GUILayout.EndHorizontal();
  82. EditorGUILayout.PropertyField(shader);
  83. EditorGUILayout.PropertyField(verbose);
  84. EditorGUILayout.PropertyField(createComponents);
  85. EditorGUILayout.PropertyField(updateDynamically);
  86. serializedObject.ApplyModifiedProperties();
  87. }
  88. }