Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class RingSpaceEditor : EditorWindow
  6. {
  7. private bool m_ringSpaceEnabled;
  8. private Tool m_lastTool;
  9. private RotationController m_ring;
  10. private Vector3 handlePosition;
  11. private int m_subdivions = 4;
  12. private bool m_CurveEachUpdate;
  13. private int m_DuplicateAmount;
  14. private float m_DuplicateAngle;
  15. // Add menu named "My Window" to the Window menu
  16. [MenuItem("Tools/Ring Space")]
  17. static void Init()
  18. {
  19. // Get existing open window or if none, make a new one:
  20. RingSpaceEditor window = (RingSpaceEditor)EditorWindow.GetWindow(typeof(RingSpaceEditor));
  21. window.Show();
  22. }
  23. void OnGUI()
  24. {
  25. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  26. EditorGUILayout.BeginHorizontal();
  27. m_ring = EditorGUILayout.ObjectField(m_ring, typeof(RotationController),true) as RotationController;
  28. if (m_ring == null && m_ringSpaceEnabled)
  29. SetRingSpace(false);
  30. GUI.enabled = m_ring != null;
  31. if (GUILayout.Button((m_ringSpaceEnabled ? "Disable" : "Enable") + " Ring Space"))
  32. SetRingSpace(!m_ringSpaceEnabled);
  33. GUI.enabled = true;
  34. EditorGUILayout.EndHorizontal();
  35. MeshGUI();
  36. DuplicationGUI();
  37. }
  38. private void OnDisable() {
  39. SetRingSpace(false);
  40. }
  41. private void SetRingSpace(bool value)
  42. {
  43. m_ringSpaceEnabled = value;
  44. Tools.hidden = m_ringSpaceEnabled;
  45. if(m_ringSpaceEnabled)
  46. SceneView.duringSceneGui += this.OnSceneGUI;
  47. else
  48. SceneView.duringSceneGui -= this.OnSceneGUI;
  49. SceneView.RepaintAll();
  50. }
  51. public void OnSelectionChange()
  52. {
  53. Debug.Log("Selection changed");
  54. if (Selection.activeGameObject != null)
  55. handlePosition = Selection.activeGameObject.transform.position;
  56. }
  57. void OnSceneGUI(SceneView sceneView)
  58. {
  59. // Do your drawing here using Handles.
  60. GameObject selectedObject = Selection.activeGameObject;
  61. if (selectedObject == null)
  62. return;
  63. EditorGUI.BeginChangeCheck();
  64. handlePosition = Handles.PositionHandle(selectedObject.transform.position, selectedObject.transform.rotation);
  65. if (EditorGUI.EndChangeCheck())
  66. {
  67. Debug.Log("Finished moving");
  68. Undo.RecordObject(selectedObject.transform, "Changed Position");
  69. selectedObject.transform.position = handlePosition;
  70. selectedObject.transform.rotation = m_ring.getUpRotation(selectedObject.transform.position);
  71. if (m_CurveEachUpdate)
  72. CurveMesh(selectedObject);
  73. }
  74. Handles.BeginGUI();
  75. // Do your drawing here using GUI.
  76. Handles.EndGUI();
  77. }
  78. void MeshGUI()
  79. {
  80. GUILayout.Space(20);
  81. GUILayout.Label("Mesh Settings", EditorStyles.boldLabel);
  82. GUILayout.BeginHorizontal();
  83. m_subdivions = EditorGUILayout.IntField(m_subdivions,GUILayout.Width(50));
  84. if(GUILayout.Button("Subdivide Mesh") && Selection.activeGameObject != null)
  85. {
  86. SubdividMesh(Selection.activeGameObject);
  87. }
  88. GUILayout.EndHorizontal();
  89. GUILayout.BeginHorizontal();
  90. m_CurveEachUpdate = EditorGUILayout.Toggle(m_CurveEachUpdate, GUILayout.Width(50));
  91. if (GUILayout.Button("Curve Mesh") && Selection.activeGameObject != null)
  92. {
  93. CurveMesh(Selection.activeGameObject);
  94. }
  95. GUILayout.EndHorizontal();
  96. if (GUILayout.Button("Reset Mesh")&& Selection.activeGameObject != null)
  97. {
  98. MeshData _meshdata = Selection.activeGameObject.GetComponent<MeshData>();
  99. if (_meshdata != null)
  100. _meshdata.Remove();
  101. }
  102. }
  103. void DuplicationGUI()
  104. {
  105. GUILayout.Space(20);
  106. GUILayout.Label("Duplication Settings", EditorStyles.boldLabel);
  107. GUILayout.BeginHorizontal();
  108. EditorGUILayout.PrefixLabel("Amount");
  109. m_DuplicateAmount = EditorGUILayout.IntField(m_DuplicateAmount, GUILayout.Width(100));
  110. GUILayout.FlexibleSpace();
  111. EditorGUILayout.PrefixLabel("Angle");
  112. m_DuplicateAngle = EditorGUILayout.FloatField(m_DuplicateAngle, GUILayout.Width(100));
  113. GUILayout.EndHorizontal();
  114. if (GUILayout.Button("Duplicate") && Selection.activeGameObject != null)
  115. {
  116. Duplicate(Selection.activeGameObject, m_DuplicateAmount, m_DuplicateAngle);
  117. }
  118. }
  119. private void SubdividMesh(GameObject selection)
  120. {
  121. MeshData _meshData = selection.GetComponent<MeshData>();
  122. if (_meshData == null)
  123. _meshData = MeshData.Add(selection);
  124. _meshData.SubdivideMesh(m_subdivions);
  125. }
  126. private void CurveMesh(GameObject selection)
  127. {
  128. MeshData _meshData = selection.GetComponent<MeshData>();
  129. if (_meshData == null)
  130. _meshData = MeshData.Add(selection);
  131. _meshData.CurveMesh(m_ring.Position,m_ring.RotationAxis);
  132. }
  133. private void Duplicate(GameObject selection, int amount, float angle)
  134. {
  135. for (int i = 0; i < amount; i++)
  136. {
  137. GameObject clone = GameObject.Instantiate(selection, selection.transform.parent);
  138. clone.transform.RotateAround(m_ring.Position, m_ring.RotationAxis, angle * (i + 1));
  139. Undo.RegisterCreatedObjectUndo(clone, $"Duplicated {selection.name}");
  140. }
  141. }
  142. }