SubMeshSettingsDrawer.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace ShinySSRR {
  5. [CustomPropertyDrawer(typeof(SubMeshSettingsData))]
  6. public class SubMeshSettingsDrawer : PropertyDrawer {
  7. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) {
  8. GUIStyle style = GUI.skin.GetStyle("label");
  9. float lineHeight = style.CalcHeight(label, EditorGUIUtility.currentViewWidth);
  10. return lineHeight;
  11. }
  12. public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) {
  13. Rect firstColumn = position;
  14. firstColumn.width = EditorGUIUtility.labelWidth - firstColumn.x;
  15. Rect secondColumn = position;
  16. secondColumn.x = EditorGUIUtility.labelWidth + 5;
  17. secondColumn.width = position.width - secondColumn.x;
  18. int propIndex = GetArrayIndex(prop);
  19. Reflections refl = (Reflections)prop.serializedObject.targetObject;
  20. if (refl.ssrRenderers != null && refl.ssrRenderers.Count == 1 && refl.ssrRenderers[0].originalMaterials != null) {
  21. List<Material> materials = refl.ssrRenderers[0].originalMaterials;
  22. int matIndex = propIndex;
  23. if (matIndex >= materials.Count) {
  24. matIndex = materials.Count - 1;
  25. }
  26. EditorGUI.LabelField(firstColumn, materials[matIndex].name);
  27. } else {
  28. EditorGUI.LabelField(firstColumn, "SubMesh " + propIndex);
  29. }
  30. EditorGUI.PropertyField(secondColumn, prop.FindPropertyRelative("smoothness"), GUIContent.none);
  31. }
  32. /// <summary>
  33. /// Returns the index of this property in the array
  34. /// </summary>
  35. int GetArrayIndex(SerializedProperty property) {
  36. string s = property.propertyPath;
  37. int bracket = s.LastIndexOf("[");
  38. if (bracket >= 0) {
  39. string indexStr = s.Substring(bracket + 1, s.Length - bracket - 2);
  40. int index;
  41. if (int.TryParse(indexStr, out index)) {
  42. return index;
  43. }
  44. }
  45. return 0;
  46. }
  47. }
  48. }