69 lines
2.5 KiB
C#
Raw Permalink Normal View History

2025-05-18 01:04:31 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Joystick), false)]
public class JoystickEditor : Editor
{
private SerializedProperty handleRange;
private SerializedProperty invalidRange;
private SerializedProperty axisOptions;
private SerializedProperty snapX;
private SerializedProperty snapY;
protected SerializedProperty background;
protected SerializedProperty handle;
protected SerializedProperty indicator;
protected Vector2 center = new Vector2(0.5f, 0.5f);
protected virtual void OnEnable()
{
handleRange = serializedObject.FindProperty("_handleRange");
invalidRange = serializedObject.FindProperty("_invalidRange");
axisOptions = serializedObject.FindProperty("_axisOptions");
snapX = serializedObject.FindProperty("_snapX");
snapY = serializedObject.FindProperty("_snapY");
background = serializedObject.FindProperty("background");
handle = serializedObject.FindProperty("handle");
indicator = serializedObject.FindProperty("indicator");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
DrawValues();
EditorGUILayout.Space();
DrawComponents();
serializedObject.ApplyModifiedProperties();
if (handle != null)
{
RectTransform handleRect = (RectTransform)handle.objectReferenceValue;
handleRect.anchorMax = center;
handleRect.anchorMin = center;
handleRect.pivot = center;
handleRect.anchoredPosition = Vector2.zero;
}
}
protected virtual void DrawValues()
{
EditorGUILayout.PropertyField(handleRange, new GUIContent("Handle Range", "The distance the visual handle can move from the center of the joystick."));
EditorGUILayout.PropertyField(invalidRange, new GUIContent("Invalid Range", "The distance away from the center input has to be before registering."));
EditorGUILayout.PropertyField(axisOptions, new GUIContent("Axis Options", "Which axes the joystick uses."));
EditorGUILayout.PropertyField(snapX, new GUIContent("Snap X", "Snap the horizontal input to a whole value."));
EditorGUILayout.PropertyField(snapY, new GUIContent("Snap Y", "Snap the vertical input to a whole value."));
}
protected virtual void DrawComponents()
{
EditorGUILayout.ObjectField(background, new GUIContent("Background", "The background's RectTransform component."));
EditorGUILayout.ObjectField(handle, new GUIContent("Handle", "The handle's RectTransform component."));
EditorGUILayout.ObjectField(indicator, new GUIContent("Indicator", "The indicator's RectTransform component."));
}
}