2
0

SteamVR_RenderModelEditor.cs 3.8 KB

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