MegaShapeWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEditor;
  2. using UnityEngine;
  3. // TODO: Select axis for shapes
  4. // TODO: Add new spline to shape
  5. // TODO: Button to recalc lengths
  6. // TEST: Build a simple scene in max then have a road, barrier, fence etc
  7. // Import of simple text file for path
  8. public class MegaShapeWindow : EditorWindow
  9. {
  10. static bool showcommon;
  11. //string name = "Shape";
  12. static MegaAxis axis = MegaAxis.Y;
  13. static bool drawknots = true;
  14. static bool drawhandles = false;
  15. static float stepdist = 0.5f;
  16. static float knotsize = 2.0f;
  17. static Color col1 = Color.white;
  18. static Color col2 = Color.black;
  19. static bool makemesh = false;
  20. // Add menu named "My Window" to the Window menu
  21. [MenuItem("GameObject/Mega Shapes")]
  22. static void Init()
  23. {
  24. // Get existing open window or if none, make a new one:
  25. //MegaShapeWindow window = (MegaShapeWindow)EditorWindow.GetWindow(typeof(MegaShapeWindow));
  26. EditorWindow.GetWindow(typeof(MegaShapeWindow), false, "MegaShapes");
  27. }
  28. [MenuItem("GameObject/Create Other/MegaShape/Star Shape")] static void CreateStarShape() { CreateShape("Star", typeof(MegaShapeStar)); }
  29. [MenuItem("GameObject/Create Other/MegaShape/Circle Shape")] static void CreateCircleShape() { CreateShape("Circle", typeof(MegaShapeCircle)); }
  30. [MenuItem("GameObject/Create Other/MegaShape/NGon Shape")] static void CreateNGonShape() { CreateShape("NGon", typeof(MegaShapeNGon)); }
  31. [MenuItem("GameObject/Create Other/MegaShape/Arc Shape")] static void CreateArcShape() { CreateShape("Arc", typeof(MegaShapeArc)); }
  32. [MenuItem("GameObject/Create Other/MegaShape/Ellipse Shape")] static void CreateEllipseShape() { CreateShape("Ellipse", typeof(MegaShapeEllipse)); }
  33. [MenuItem("GameObject/Create Other/MegaShape/Rectangle Shape")] static void CreateRectangleShape() { CreateShape("Rectangle", typeof(MegaShapeRectangle)); }
  34. [MenuItem("GameObject/Create Other/MegaShape/Helix Shape")] static void CreateHelixShape() { CreateShape("Helix", typeof(MegaShapeHelix)); }
  35. [MenuItem("GameObject/Create Other/MegaShape/Line Shape")] static void CreateLineShape() { CreateShape("Line", typeof(MegaShapeLine)); }
  36. static Color butcol = new Color(0.75f, 0.75f, 1.0f);
  37. static MegaModBut[] mods = new MegaModBut[] {
  38. new MegaModBut("Arc", "Create a Arc Shape", typeof(MegaShapeArc), butcol),
  39. new MegaModBut("Circle", "Create a Circle Shape", typeof(MegaShapeCircle), butcol),
  40. new MegaModBut("Ellipse", "Create a Ellipse Shape", typeof(MegaShapeEllipse), butcol),
  41. new MegaModBut("Helix", "Create a Helix Shape", typeof(MegaShapeHelix), butcol),
  42. new MegaModBut("Line", "Create a Line Shape", typeof(MegaShapeLine), butcol),
  43. new MegaModBut("NGon", "Create a NGon Shape", typeof(MegaShapeNGon), butcol),
  44. new MegaModBut("Rectangle", "Create a Rectangle Shape", typeof(MegaShapeRectangle), butcol),
  45. new MegaModBut("Star", "Create a Star Shape", typeof(MegaShapeStar), butcol),
  46. };
  47. void DoButtons(MegaModBut[] buttons, float width, int bstep, bool modobj)
  48. {
  49. Color c = GUI.backgroundColor;
  50. int off = 0;
  51. GUI.backgroundColor = Color.blue;
  52. Color guicol = GUI.color;
  53. GUI.color = new Color(1, 1, 1, 1);
  54. GUI.backgroundColor = new Color(0, 0, 0, 0);
  55. GUI.contentColor = Color.white;
  56. for ( int i = 0; i < buttons.Length; i++ )
  57. {
  58. //GUI.backgroundColor = buttons[i].color; //Color.blue;
  59. GUI.contentColor = buttons[i].color; //Color.blue;
  60. GUI.backgroundColor = buttons[i].color * 0.08f;
  61. if ( off == 0 )
  62. EditorGUILayout.BeginHorizontal();
  63. if ( GUILayout.Button(buttons[i].content, GUILayout.Width(width)) )
  64. {
  65. CreateShape(buttons[i].name, buttons[i].classname);
  66. }
  67. off++;
  68. if ( off == bstep )
  69. {
  70. off = 0;
  71. EditorGUILayout.EndHorizontal();
  72. }
  73. }
  74. if ( off != 0 )
  75. EditorGUILayout.EndHorizontal();
  76. GUI.backgroundColor = c;
  77. GUI.color = guicol;
  78. }
  79. Vector2 scroll = Vector2.zero;
  80. int toolbarInt = 0;
  81. string[] toolbarStrings = { "Shapes", "Params" };
  82. // Put common params in, and each shape has its sections
  83. void OnGUI()
  84. {
  85. scroll = EditorGUILayout.BeginScrollView(scroll);
  86. //name = EditorGUILayout.TextField("Name", name);
  87. toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings, GUILayout.MaxWidth(150.0f));
  88. float butwidth = 80.0f;
  89. float width = this.position.width; // / 2.0f;
  90. int bstep = (int)(width / butwidth);
  91. if ( bstep == 0 )
  92. bstep = 1;
  93. if ( toolbarInt == 0 )
  94. {
  95. DoButtons(mods, (width / bstep) - 6.0f, bstep, true);
  96. }
  97. else
  98. {
  99. //showcommon = EditorGUILayout.Foldout(showcommon, "Common");
  100. //if ( showcommon )
  101. {
  102. axis = (MegaAxis)EditorGUILayout.EnumPopup("Axis", axis);
  103. stepdist = EditorGUILayout.FloatField("Step Dist", stepdist);
  104. knotsize = EditorGUILayout.FloatField("Knot Size", knotsize);
  105. drawknots = EditorGUILayout.Toggle("Draw Knots", drawknots);
  106. drawhandles = EditorGUILayout.Toggle("Draw Handles", drawhandles);
  107. col1 = EditorGUILayout.ColorField("Color 1", col1);
  108. col2 = EditorGUILayout.ColorField("Color 2", col2);
  109. makemesh = EditorGUILayout.Toggle("Make Mesh", makemesh);
  110. }
  111. }
  112. EditorGUILayout.EndScrollView();
  113. }
  114. #if false
  115. static void CreateShape(string type)
  116. {
  117. Vector3 pos = UnityEditor.SceneView.lastActiveSceneView.pivot;
  118. MegaShape ms = null;
  119. GameObject go = new GameObject(type + " Shape");
  120. switch ( type )
  121. {
  122. case "Circle": ms = go.AddComponent<MegaShapeCircle>(); break;
  123. case "Star": ms = go.AddComponent<MegaShapeStar>(); break;
  124. case "NGon": ms = go.AddComponent<MegaShapeNGon>(); break;
  125. case "Arc": ms = go.AddComponent<MegaShapeArc>(); break;
  126. case "Ellipse": ms = go.AddComponent<MegaShapeEllipse>(); break;
  127. case "Rectangle": ms = go.AddComponent<MegaShapeRectangle>(); break;
  128. case "Helix": ms = go.AddComponent<MegaShapeHelix>(); break;
  129. }
  130. go.transform.position = pos;
  131. Selection.activeObject = go;
  132. if ( ms != null )
  133. {
  134. ms.axis = axis;
  135. ms.drawHandles = drawhandles;
  136. ms.drawKnots = drawknots;
  137. ms.col1 = col1;
  138. ms.col2 = col2;
  139. ms.KnotSize = knotsize;
  140. ms.stepdist = stepdist;
  141. }
  142. }
  143. #endif
  144. static void CreateShape(string type, System.Type classtype)
  145. {
  146. Vector3 pos = Vector3.zero;
  147. if ( UnityEditor.SceneView.lastActiveSceneView != null )
  148. pos = UnityEditor.SceneView.lastActiveSceneView.pivot;
  149. GameObject go = new GameObject(type + " Shape");
  150. MegaShape ms = (MegaShape)go.AddComponent(classtype);
  151. go.transform.position = pos;
  152. Selection.activeObject = go;
  153. if ( ms != null )
  154. {
  155. ms.axis = axis;
  156. ms.drawHandles = drawhandles;
  157. ms.drawKnots = drawknots;
  158. ms.col1 = col1;
  159. ms.col2 = col2;
  160. ms.KnotSize = knotsize;
  161. ms.stepdist = stepdist;
  162. ms.makeMesh = makemesh;
  163. ms.handleType = MegaHandleType.Free;
  164. }
  165. }
  166. }