Editor/SnapRotateEditor.cs

mail@pastecode.io avatar
unknown
csharp
a year ago
2.4 kB
10
Indexable
Never
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(SnapRotate))]
public class SnapRotateEditor : Editor
{
	public override void OnInspectorGUI()
	{
		SnapRotate snapRotate = (SnapRotate)target;

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Pitch", GUILayout.MaxWidth(50));
		if (GUILayout.Button("<", GUILayout.MaxWidth(25)))
		{
			snapRotate.Pitch.CurrentDirection--;
			if ((int)snapRotate.Pitch.CurrentDirection < 0)
			{
				snapRotate.Pitch.CurrentDirection = (Discrete2DRotation.RotationDirection)7;
			}
		}
		EditorGUILayout.EnumPopup(snapRotate.Pitch.CurrentDirection, GUILayout.MaxWidth(100));
		if (GUILayout.Button(">", GUILayout.MaxWidth(25)))
		{
			snapRotate.Pitch.CurrentDirection++;
			if ((int)snapRotate.Pitch.CurrentDirection > 7)
			{
				snapRotate.Pitch.CurrentDirection = 0;
			}
		}
		EditorGUILayout.EndHorizontal();

		// Repeat the same process for Yaw and Roll

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Yaw", GUILayout.MaxWidth(50));
		if (GUILayout.Button("<", GUILayout.MaxWidth(25)))
		{
			snapRotate.Yaw.CurrentDirection--;
			if ((int)snapRotate.Yaw.CurrentDirection < 0)
			{
				snapRotate.Yaw.CurrentDirection = (Discrete2DRotation.RotationDirection)7;
			}
		}
		EditorGUILayout.EnumPopup(snapRotate.Yaw.CurrentDirection, GUILayout.MaxWidth(100));
		if (GUILayout.Button(">", GUILayout.MaxWidth(25)))
		{
			snapRotate.Yaw.CurrentDirection++;
			if ((int)snapRotate.Yaw.CurrentDirection > 7)
			{
				snapRotate.Yaw.CurrentDirection = 0;
			}
		}
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Roll", GUILayout.MaxWidth(50));
		if (GUILayout.Button("<", GUILayout.MaxWidth(25)))
		{
			snapRotate.Roll.CurrentDirection--;
			if ((int)snapRotate.Roll.CurrentDirection < 0)
			{
				snapRotate.Roll.CurrentDirection = (Discrete2DRotation.RotationDirection)7;
			}
		}
		EditorGUILayout.EnumPopup(snapRotate.Roll.CurrentDirection, GUILayout.MaxWidth(100));
		if (GUILayout.Button(">", GUILayout.MaxWidth(25)))
		{
			snapRotate.Roll.CurrentDirection++;
			if ((int)snapRotate.Roll.CurrentDirection > 7)
			{
				snapRotate.Roll.CurrentDirection = 0;
			}
		}
		EditorGUILayout.EndHorizontal();

		// Apply changes to the target object
		if (GUI.changed)
		{
			EditorUtility.SetDirty(snapRotate);
			serializedObject.ApplyModifiedProperties();
		}
	}
}