|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
-
- public class RingSpaceEditor : EditorWindow
- {
- private bool m_ringSpaceEnabled;
- private Tool m_lastTool;
- private RotationController m_ring;
- private Vector3 handlePosition;
-
- private int m_subdivions = 4;
- private bool m_CurveEachUpdate;
-
- private int m_DuplicateAmount;
- private float m_DuplicateAngle;
-
-
- // Add menu named "My Window" to the Window menu
- [MenuItem("Tools/Ring Space")]
- static void Init()
- {
- // Get existing open window or if none, make a new one:
- RingSpaceEditor window = (RingSpaceEditor)EditorWindow.GetWindow(typeof(RingSpaceEditor));
- window.Show();
- }
-
- void OnGUI()
- {
- GUILayout.Label("Base Settings", EditorStyles.boldLabel);
-
- EditorGUILayout.BeginHorizontal();
- m_ring = EditorGUILayout.ObjectField(m_ring, typeof(RotationController),true) as RotationController;
- if (m_ring == null && m_ringSpaceEnabled)
- SetRingSpace(false);
-
- GUI.enabled = m_ring != null;
- if (GUILayout.Button((m_ringSpaceEnabled ? "Disable" : "Enable") + " Ring Space"))
- SetRingSpace(!m_ringSpaceEnabled);
- GUI.enabled = true;
- EditorGUILayout.EndHorizontal();
-
- MeshGUI();
- DuplicationGUI();
- }
-
-
-
- private void OnDisable() {
-
- SetRingSpace(false);
- }
-
- private void SetRingSpace(bool value)
- {
- m_ringSpaceEnabled = value;
- Tools.hidden = m_ringSpaceEnabled;
-
- if(m_ringSpaceEnabled)
- SceneView.duringSceneGui += this.OnSceneGUI;
-
- else
- SceneView.duringSceneGui -= this.OnSceneGUI;
-
- SceneView.RepaintAll();
- }
-
- public void OnSelectionChange()
- {
- Debug.Log("Selection changed");
- if (Selection.activeGameObject != null)
- handlePosition = Selection.activeGameObject.transform.position;
- }
-
- void OnSceneGUI(SceneView sceneView)
- {
- // Do your drawing here using Handles.
- GameObject selectedObject = Selection.activeGameObject;
-
- if (selectedObject == null)
- return;
-
- EditorGUI.BeginChangeCheck();
- handlePosition = Handles.PositionHandle(selectedObject.transform.position, selectedObject.transform.rotation);
- if (EditorGUI.EndChangeCheck())
- {
- Debug.Log("Finished moving");
- Undo.RecordObject(selectedObject.transform, "Changed Position");
- selectedObject.transform.position = handlePosition;
- selectedObject.transform.rotation = m_ring.getUpRotation(selectedObject.transform.position);
-
- if (m_CurveEachUpdate)
- CurveMesh(selectedObject);
-
- }
-
-
-
-
-
-
- Handles.BeginGUI();
- // Do your drawing here using GUI.
- Handles.EndGUI();
- }
-
- void MeshGUI()
- {
- GUILayout.Space(20);
- GUILayout.Label("Mesh Settings", EditorStyles.boldLabel);
-
- GUILayout.BeginHorizontal();
-
- m_subdivions = EditorGUILayout.IntField(m_subdivions,GUILayout.Width(50));
-
- if(GUILayout.Button("Subdivide Mesh") && Selection.activeGameObject != null)
- {
- SubdividMesh(Selection.activeGameObject);
- }
-
- GUILayout.EndHorizontal();
-
- GUILayout.BeginHorizontal();
-
- m_CurveEachUpdate = EditorGUILayout.Toggle(m_CurveEachUpdate, GUILayout.Width(50));
-
- if (GUILayout.Button("Curve Mesh") && Selection.activeGameObject != null)
- {
- CurveMesh(Selection.activeGameObject);
- }
-
- GUILayout.EndHorizontal();
-
- if (GUILayout.Button("Reset Mesh")&& Selection.activeGameObject != null)
- {
- MeshData _meshdata = Selection.activeGameObject.GetComponent<MeshData>();
- if (_meshdata != null)
- _meshdata.Remove();
-
- }
-
- }
-
- void DuplicationGUI()
- {
- GUILayout.Space(20);
- GUILayout.Label("Duplication Settings", EditorStyles.boldLabel);
- GUILayout.BeginHorizontal();
-
-
- EditorGUILayout.PrefixLabel("Amount");
- m_DuplicateAmount = EditorGUILayout.IntField(m_DuplicateAmount, GUILayout.Width(100));
- GUILayout.FlexibleSpace();
- EditorGUILayout.PrefixLabel("Angle");
- m_DuplicateAngle = EditorGUILayout.FloatField(m_DuplicateAngle, GUILayout.Width(100));
- GUILayout.EndHorizontal();
-
- if (GUILayout.Button("Duplicate") && Selection.activeGameObject != null)
- {
- Duplicate(Selection.activeGameObject, m_DuplicateAmount, m_DuplicateAngle);
- }
-
-
- }
-
- private void SubdividMesh(GameObject selection)
- {
- MeshData _meshData = selection.GetComponent<MeshData>();
- if (_meshData == null)
- _meshData = MeshData.Add(selection);
-
- _meshData.SubdivideMesh(m_subdivions);
- }
-
- private void CurveMesh(GameObject selection)
- {
- MeshData _meshData = selection.GetComponent<MeshData>();
- if (_meshData == null)
- _meshData = MeshData.Add(selection);
-
- _meshData.CurveMesh(m_ring.Position,m_ring.RotationAxis);
- }
-
- private void Duplicate(GameObject selection, int amount, float angle)
- {
-
- for (int i = 0; i < amount; i++)
- {
-
- GameObject clone = GameObject.Instantiate(selection, selection.transform.parent);
-
- clone.transform.RotateAround(m_ring.Position, m_ring.RotationAxis, angle * (i + 1));
- Undo.RegisterCreatedObjectUndo(clone, $"Duplicated {selection.name}");
- }
-
-
- }
-
-
-
-
-
-
-
- }
|