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.

101 lines
2.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class ShipPhysicsEditor : EditorWindow
  6. {
  7. private float m_GravityStrength = 9.8f;
  8. private Vector3 m_Point;
  9. private RotationController m_Ship;
  10. private float m_radius = 0;
  11. private float m_oldRadius = 0;
  12. // Add menu named "My Window" to the Window menu
  13. [MenuItem("Tools/Ship Physics")]
  14. static void Init()
  15. {
  16. // Get existing open window or if none, make a new one:
  17. ShipPhysicsEditor window = (ShipPhysicsEditor)EditorWindow.GetWindow(typeof(ShipPhysicsEditor));
  18. window.m_Ship = FindObjectOfType<RotationController>();
  19. window.m_Point = window?.m_Ship.Position ?? Vector3.zero;
  20. window.RegisterSceneGUI(true);
  21. window.Show();
  22. }
  23. private void OnDestroy()
  24. {
  25. RegisterSceneGUI(false);
  26. }
  27. public void OnGUI()
  28. {
  29. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  30. m_Ship = EditorGUILayout.ObjectField(m_Ship, typeof(RotationController), true) as RotationController;
  31. m_GravityStrength = EditorGUILayout.FloatField("Gravity", m_GravityStrength);
  32. m_radius = EditorGUILayout.FloatField("Radius", m_radius);
  33. if (m_oldRadius != m_radius)
  34. {
  35. Vector3 direction;
  36. if (m_Point == m_Ship.Position)
  37. direction = Vector3.Cross(m_Ship.RotationAxis, (m_Ship.RotationAxis != Vector3.right) ? Vector3.left : Vector3.up);
  38. else
  39. direction = m_Ship.getDownDirection(m_Point);
  40. m_Point = m_Ship.Position + direction * m_radius;
  41. m_oldRadius = m_radius;
  42. SceneView.RepaintAll();
  43. }
  44. if (GUILayout.Button("Set Rotation"))
  45. m_Ship.SetGravityAtRadius(m_GravityStrength, m_radius);
  46. }
  47. void OnSceneGUI(SceneView sceneView)
  48. {
  49. EditorGUI.BeginChangeCheck();
  50. m_Point = Handles.PositionHandle(m_Point, m_Ship.getUpRotation(m_Point));
  51. if (EditorGUI.EndChangeCheck())
  52. {
  53. m_Point = Vector3.ProjectOnPlane(m_Point, m_Ship.RotationAxis) + Vector3.Project(m_Ship.Position, m_Ship.RotationAxis);
  54. m_radius = m_Ship.getDownDirection(m_Point, false).magnitude;
  55. m_oldRadius = m_radius;
  56. Repaint();
  57. }
  58. Handles.color = Color.red;
  59. Handles.DrawLine(m_Point, m_Ship.Position);
  60. Handles.BeginGUI();
  61. // Do your drawing here using GUI.
  62. Handles.EndGUI();
  63. }
  64. private void RegisterSceneGUI(bool value)
  65. {
  66. if (value)
  67. SceneView.duringSceneGui += this.OnSceneGUI;
  68. else
  69. SceneView.duringSceneGui -= this.OnSceneGUI;
  70. SceneView.RepaintAll();
  71. }
  72. }