96 lines
3.7 KiB
C#
96 lines
3.7 KiB
C#
|
|
using UnityEditor;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UI.DragAndDrop.Core;
|
|||
|
|
|
|||
|
|
namespace UI.DragAndDrop.Editor
|
|||
|
|
{
|
|||
|
|
[CustomEditor(typeof(DraggableObject), true)]
|
|||
|
|
public class DraggableObjectEditor : UnityEditor.Editor
|
|||
|
|
{
|
|||
|
|
public override void OnInspectorGUI()
|
|||
|
|
{
|
|||
|
|
DrawDefaultInspector();
|
|||
|
|
|
|||
|
|
DraggableObject draggable = (DraggableObject)target;
|
|||
|
|
|
|||
|
|
// Only show button in edit mode
|
|||
|
|
if (!Application.isPlaying)
|
|||
|
|
{
|
|||
|
|
EditorGUILayout.Space();
|
|||
|
|
EditorGUILayout.LabelField("Editor Tools", EditorStyles.boldLabel);
|
|||
|
|
|
|||
|
|
if (GUILayout.Button("Snap to Parent Slot"))
|
|||
|
|
{
|
|||
|
|
SnapToParentSlot(draggable);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void SnapToParentSlot(DraggableObject draggable)
|
|||
|
|
{
|
|||
|
|
// Find parent slot
|
|||
|
|
DraggableSlot parentSlot = draggable.GetComponentInParent<DraggableSlot>();
|
|||
|
|
|
|||
|
|
if (parentSlot == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning("No parent DraggableSlot found!");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Undo.RecordObject(draggable.transform, "Snap to Parent Slot");
|
|||
|
|
|
|||
|
|
// Reset position and rotation
|
|||
|
|
draggable.transform.localPosition = Vector3.zero;
|
|||
|
|
draggable.transform.localRotation = Quaternion.identity;
|
|||
|
|
|
|||
|
|
// Apply slot's size mode
|
|||
|
|
RectTransform draggableRect = draggable.GetComponent<RectTransform>();
|
|||
|
|
RectTransform slotRect = parentSlot.GetComponent<RectTransform>();
|
|||
|
|
|
|||
|
|
if (draggableRect != null && slotRect != null)
|
|||
|
|
{
|
|||
|
|
// Use reflection to access private fields
|
|||
|
|
System.Reflection.FieldInfo modeField = typeof(DraggableSlot).GetField("occupantSizeMode",
|
|||
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|||
|
|
System.Reflection.FieldInfo scaleField = typeof(DraggableSlot).GetField("occupantScale",
|
|||
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|||
|
|
|
|||
|
|
if (modeField != null && scaleField != null)
|
|||
|
|
{
|
|||
|
|
var sizeMode = modeField.GetValue(parentSlot);
|
|||
|
|
var occupantScale = (Vector3)scaleField.GetValue(parentSlot);
|
|||
|
|
|
|||
|
|
// Get enum type
|
|||
|
|
System.Type enumType = sizeMode.GetType();
|
|||
|
|
string modeName = System.Enum.GetName(enumType, sizeMode);
|
|||
|
|
|
|||
|
|
Undo.RecordObject(draggableRect, "Apply Slot Size Mode");
|
|||
|
|
|
|||
|
|
switch (modeName)
|
|||
|
|
{
|
|||
|
|
case "MatchSlotSize":
|
|||
|
|
draggableRect.sizeDelta = slotRect.sizeDelta;
|
|||
|
|
draggableRect.localScale = Vector3.one;
|
|||
|
|
Debug.Log($"Matched slot size: {slotRect.sizeDelta}");
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case "Scale":
|
|||
|
|
draggableRect.localScale = occupantScale;
|
|||
|
|
Debug.Log($"Applied scale: {occupantScale}");
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case "None":
|
|||
|
|
default:
|
|||
|
|
// Keep current size
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
EditorUtility.SetDirty(draggable.gameObject);
|
|||
|
|
Debug.Log($"Snapped {draggable.name} to parent slot {parentSlot.name}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|