ObjectDetailEditorWindow.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace MTE
  6. {
  7. internal class ObjectDetailEditorWindow : MTEWindow
  8. {
  9. /// <summary>
  10. /// Editing grass detail list
  11. /// </summary>
  12. public IList detailList;
  13. public int editingIndex;
  14. public bool IsAdding = false;
  15. public Action OnSave { private get; set; }
  16. #region Parameters
  17. private GameObject obj;
  18. private Vector3 minScale = Vector3.one;
  19. private Vector3 maxScale = Vector3.one;
  20. private bool useUnifiedScale = true;
  21. #endregion
  22. public override void OnEnable()
  23. {
  24. base.OnEnable();
  25. var rect = position;
  26. rect.width = 550;
  27. rect.height = 270;
  28. position = rect;
  29. objectDetail = null;
  30. }
  31. private ObjectDetail objectDetail;
  32. public override void OnGUI()
  33. {
  34. base.OnGUI();
  35. if(IsAdding)
  36. {
  37. obj = (GameObject) EditorGUILayout.ObjectField(StringTable.Get(C.Object), obj, typeof(GameObject), false);
  38. EditorGUILayout.PrefixLabel(StringTable.Get(C.Scale));
  39. useUnifiedScale =
  40. EditorGUILayout.Toggle(StringTable.Get(C.UnifiedScale), useUnifiedScale);
  41. EditorGUILayout.PrefixLabel(StringTable.Get(C.Scale));
  42. EditorGUI.indentLevel++;
  43. if (useUnifiedScale)
  44. {
  45. var newMin = EditorGUILayout.FloatField("min", minScale.x);
  46. var newMax = EditorGUILayout.FloatField("max", maxScale.x);
  47. EditorGUILayout.MinMaxSlider(ref newMin, ref newMax, 0.01f, 100.0f);
  48. if (newMin >= newMax)
  49. {
  50. newMin = newMax;
  51. }
  52. minScale.z = minScale.y = minScale.x = newMin;
  53. maxScale.z = maxScale.y = maxScale.x = newMax;
  54. }
  55. else
  56. {
  57. EditorGUILayoutEx.MinMaxSlider(StringTable.Get(C.Scale),
  58. ref minScale, ref maxScale, 0.01f, 100.0f);
  59. }
  60. EditorGUI.indentLevel--;
  61. GUILayout.FlexibleSpace();
  62. EditorGUILayout.BeginHorizontal();
  63. if (GUILayout.Button(StringTable.Get(C.Add)))
  64. {
  65. if (obj != null)
  66. {
  67. ObjectDetail detail = new ObjectDetail
  68. {
  69. Object = obj,
  70. MinScale = minScale,
  71. MaxScale = maxScale,
  72. UseUnifiedScale = useUnifiedScale,
  73. };
  74. this.detailList.Add(detail);
  75. OnSave();
  76. MTEEditorWindow.Instance.Repaint();
  77. this.Close();
  78. }
  79. }
  80. if (GUILayout.Button(StringTable.Get(C.Cancel)))
  81. {
  82. this.Close();
  83. }
  84. EditorGUILayout.EndHorizontal();
  85. }
  86. else//editing
  87. {
  88. if (objectDetail == null)
  89. {
  90. objectDetail = detailList[editingIndex] as ObjectDetail;
  91. if (objectDetail != null)
  92. {
  93. objectDetail = objectDetail.ShallowCopy();
  94. }
  95. }
  96. if (objectDetail == null)
  97. {
  98. EditorGUILayout.HelpBox(
  99. $"Ignored invalid Object detail detected at index {editingIndex}",
  100. MessageType.Warning);
  101. }
  102. else
  103. {
  104. objectDetail.Object = (GameObject) EditorGUILayout.ObjectField(StringTable.Get(C.Prefab),
  105. objectDetail.Object, typeof(GameObject), true);
  106. EditorGUILayout.PrefixLabel(StringTable.Get(C.Scale));
  107. EditorGUI.indentLevel++;
  108. objectDetail.UseUnifiedScale =
  109. EditorGUILayout.Toggle(StringTable.Get(C.UnifiedScale), objectDetail.UseUnifiedScale);
  110. if (objectDetail.UseUnifiedScale)
  111. {
  112. var newMin = EditorGUILayout.FloatField("min", objectDetail.MinScale.x);
  113. var newMax = EditorGUILayout.FloatField("max", objectDetail.MaxScale.x);
  114. EditorGUILayout.MinMaxSlider(ref newMin, ref newMax, 0.01f, 100.0f);
  115. if (newMin >= newMax)
  116. {
  117. newMin = newMax;
  118. }
  119. objectDetail.MinScale.z = objectDetail.MinScale.y = objectDetail.MinScale.x = newMin;
  120. objectDetail.MaxScale.z = objectDetail.MaxScale.y = objectDetail.MaxScale.x = newMax;
  121. }
  122. else
  123. {
  124. EditorGUILayoutEx.MinMaxSlider(StringTable.Get(C.Scale),
  125. ref objectDetail.MinScale, ref objectDetail.MaxScale, 0.01f, 100.0f);
  126. }
  127. EditorGUI.indentLevel--;
  128. GUILayout.FlexibleSpace();
  129. EditorGUILayout.BeginHorizontal();
  130. if (GUILayout.Button(StringTable.Get(C.Apply)))
  131. {
  132. detailList[editingIndex] = objectDetail;
  133. OnSave();
  134. MTEEditorWindow.Instance.Repaint();
  135. this.Close();
  136. }
  137. }
  138. if (GUILayout.Button(StringTable.Get(C.Cancel)))
  139. {
  140. this.Close();
  141. }
  142. EditorGUILayout.EndHorizontal();
  143. }
  144. }
  145. }
  146. }