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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ShipPhysicsEditor : EditorWindow
{
private float m_GravityStrength = 9.8f;
private Vector3 m_Point;
private RotationController m_Ship;
private float m_radius = 0;
private float m_oldRadius = 0;
// Add menu named "My Window" to the Window menu
[MenuItem("Tools/Ship Physics")]
static void Init()
{
// Get existing open window or if none, make a new one:
ShipPhysicsEditor window = (ShipPhysicsEditor)EditorWindow.GetWindow(typeof(ShipPhysicsEditor));
window.m_Ship = FindObjectOfType<RotationController>();
window.m_Point = window?.m_Ship.Position ?? Vector3.zero;
window.RegisterSceneGUI(true);
window.Show();
}
private void OnDestroy()
{
RegisterSceneGUI(false);
}
public void OnGUI()
{
GUILayout.Label("Base Settings", EditorStyles.boldLabel);
m_Ship = EditorGUILayout.ObjectField(m_Ship, typeof(RotationController), true) as RotationController;
m_GravityStrength = EditorGUILayout.FloatField("Gravity", m_GravityStrength);
m_radius = EditorGUILayout.FloatField("Radius", m_radius);
if (m_oldRadius != m_radius)
{
Vector3 direction;
if (m_Point == m_Ship.Position)
direction = Vector3.Cross(m_Ship.RotationAxis, (m_Ship.RotationAxis != Vector3.right) ? Vector3.left : Vector3.up);
else
direction = m_Ship.getDownDirection(m_Point);
m_Point = m_Ship.Position + direction * m_radius;
m_oldRadius = m_radius;
SceneView.RepaintAll();
}
if (GUILayout.Button("Set Rotation"))
m_Ship.SetGravityAtRadius(m_GravityStrength, m_radius);
}
void OnSceneGUI(SceneView sceneView)
{
EditorGUI.BeginChangeCheck();
m_Point = Handles.PositionHandle(m_Point, m_Ship.getUpRotation(m_Point));
if (EditorGUI.EndChangeCheck())
{
m_Point = Vector3.ProjectOnPlane(m_Point, m_Ship.RotationAxis) + Vector3.Project(m_Ship.Position, m_Ship.RotationAxis);
m_radius = m_Ship.getDownDirection(m_Point, false).magnitude;
m_oldRadius = m_radius;
Repaint();
}
Handles.color = Color.red;
Handles.DrawLine(m_Point, m_Ship.Position);
Handles.BeginGUI();
// Do your drawing here using GUI.
Handles.EndGUI();
}
private void RegisterSceneGUI(bool value)
{
if (value)
SceneView.duringSceneGui += this.OnSceneGUI;
else
SceneView.duringSceneGui -= this.OnSceneGUI;
SceneView.RepaintAll();
}
}