mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-21 23:58:49 -05:00
* Added the ability to drag and drop any atom reference to a `AtomReference` without the need to manually select the type through the 3 dot button on inspector - I also refactored the `AtomBaseReferenceDrawer` script so that it's easier to read/maintain. - The class `BaseAtomInstancer` is needed to be able to auto reference instancers. Because when we drag a `GameObject` we need to get it's `AtomInstancer` component to determine the reference type. Also, the class represents something similiar to `BaseAtom` for `ScriptableObjects` but for `MonoBehaviors`, so it makes sense to have it. * Addressed some of the key points @soraphis made and fixed the issues I mentioned on the #442 pull - Solved the probelms that arise when a `GameObject` has multiple different instancers in it (mentioned in the #442 pull) - Removed `UsageIndex` class as it is not needed * Fixed file name not matching the class name * Addressed @soraphis issue of `GuiData` not being a struct, so I changed it and modified the `AtomBaseReferenceDrawer` to handle it as a struct * Fixed `GuiData` isn't updated even though `position` and `label` parameters of the `OnGUI` are. * Reversed the for loops order because the order of the components inside the `GameObject` are more important than the order of the "usages" so it fixes that problem - Also cleaned the code a bit * Missed a line that could be simplified - Made `Set/GetUsageIndex` into static because we can * Fixed issue mentioned in #442 > "you only get the first component, when dragging in a game object, so there could be the case where selecting the type manually and dragging into it will swap the field" The issue is when there are multiple instancer components in a single `GameObject` then when you drag said `GameObject` then the atom reference will switch to the first instancer (via usage index) no matter the intent of the user. However, the intent of the user could be to pick the 2nd or 3rd reference, so he could manually select the usage type using the 3 dots button, but it won't work if he decides to drag and drop a `GameObject` that has multiple instancers that the reference could switch to automatically, which will always be the first instancer of the dragged `GameObject`, which bascially makes the experience frustrating to that particular scenario. Now, you can guess that the issue is hyper specific just because of how hard it is to me to explain it in text, so don't worry if you didn't get it on the first read. If you would like me to showcase it, I will gladly share a video example of what I mean. * Fixed index out of range exception (I forgot that I set the usage index at the end of this method and it could be -1 because of this line) * Redone the previous push because it was incorrect * Response to @AdamRamberg to try to make the code section clearer by refactoring it * * Minor cosmetic changes * Replace switch expression to be backwards compatible (for example to be able to run the unity-atoms-example project) * Always remove the last greater than symbol in GetPropertyTypeName to take into account generic types * Added comment to IAtomInstancer * Removed `GuiData` * testing being able to merge * Removed `GuiData` in the new merged push * Removed `GuiData` class once again * Integrated the #447 fix - Also the variable `initialPosition` was missing so I added it back - Swapped the ordering of the `position` and `property` inside of the methods to match the ordering of the `OnGUI` method (I think it just makes more sense visually to order it this way) * Revert removal of compiler flags when setting valueFieldHeight --------- Co-authored-by: Adam Ramberg <adam@mambojambostudios.com>
This commit is contained in:
parent
2e11570e74
commit
f2b9186eef
@ -24,12 +24,7 @@ namespace UnityAtoms.Editor
|
|||||||
|
|
||||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||||
{
|
{
|
||||||
GuiData guiData = new GuiData()
|
Rect initialPosition = position;
|
||||||
{
|
|
||||||
Position = position,
|
|
||||||
Property = property,
|
|
||||||
Label = label
|
|
||||||
};
|
|
||||||
|
|
||||||
if (_popupStyle == null)
|
if (_popupStyle == null)
|
||||||
{
|
{
|
||||||
@ -41,18 +36,17 @@ namespace UnityAtoms.Editor
|
|||||||
|
|
||||||
using (var scope = new EditorGUI.PropertyScope(position, label, property))
|
using (var scope = new EditorGUI.PropertyScope(position, label, property))
|
||||||
{
|
{
|
||||||
guiData.Label = scope.content;
|
label = scope.content;
|
||||||
guiData.Position = EditorGUI.PrefixLabel(position, label);
|
position = EditorGUI.PrefixLabel(position, label);
|
||||||
// Store old indent level and set it to 0, the PrefixLabel takes care of it
|
// Store old indent level and set it to 0, the PrefixLabel takes care of it
|
||||||
int indent = EditorGUI.indentLevel;
|
int indent = EditorGUI.indentLevel;
|
||||||
EditorGUI.indentLevel = 0;
|
EditorGUI.indentLevel = 0;
|
||||||
{
|
{
|
||||||
EditorGUI.BeginChangeCheck();
|
EditorGUI.BeginChangeCheck();
|
||||||
{
|
{
|
||||||
DetermineDragAndDropFieldReferenceType(guiData);
|
DetermineDragAndDropFieldReferenceType(position, property);
|
||||||
DrawConfigurationButton(ref guiData);
|
DrawConfigurationButton(ref position, property);
|
||||||
string currentUsageTypePropertyName = GetUsages(property)[GetUsageIndex(property)].PropertyName;
|
DrawField(position, property, label, initialPosition);
|
||||||
DrawField(currentUsageTypePropertyName, guiData, position);
|
|
||||||
}
|
}
|
||||||
if (EditorGUI.EndChangeCheck())
|
if (EditorGUI.EndChangeCheck())
|
||||||
{
|
{
|
||||||
@ -78,8 +72,8 @@ namespace UnityAtoms.Editor
|
|||||||
}
|
}
|
||||||
|
|
||||||
var innerProperty = property.FindPropertyRelative(usageData.PropertyName);
|
var innerProperty = property.FindPropertyRelative(usageData.PropertyName);
|
||||||
|
|
||||||
bool forceSingleLine = false;
|
bool forceSingleLine = false;
|
||||||
|
|
||||||
#if UNITY_2021_2_OR_NEWER
|
#if UNITY_2021_2_OR_NEWER
|
||||||
// This is needed for similar reasons as described in the comment in the DrawField method below.
|
// This is needed for similar reasons as described in the comment in the DrawField method below.
|
||||||
// This is basically a hack to fix a bug on Unity's side, which we need to revert when / if Unity fix it on their side.
|
// This is basically a hack to fix a bug on Unity's side, which we need to revert when / if Unity fix it on their side.
|
||||||
@ -91,26 +85,27 @@ namespace UnityAtoms.Editor
|
|||||||
EditorGUI.GetPropertyHeight(innerProperty, label);
|
EditorGUI.GetPropertyHeight(innerProperty, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawConfigurationButton(ref GuiData guiData)
|
private void DrawConfigurationButton(ref Rect position, SerializedProperty property)
|
||||||
{
|
{
|
||||||
Rect button = new Rect(guiData.Position);
|
Rect button = new Rect(position);
|
||||||
button.yMin += _popupStyle.margin.top;
|
button.yMin += _popupStyle.margin.top;
|
||||||
button.yMax = button.yMin + EditorGUIUtility.singleLineHeight;
|
button.yMax = button.yMin + EditorGUIUtility.singleLineHeight;
|
||||||
button.width = _popupStyle.fixedWidth + _popupStyle.margin.right;
|
button.width = _popupStyle.fixedWidth + _popupStyle.margin.right;
|
||||||
guiData.Position.xMin = button.xMax;
|
position.xMin = button.xMax;
|
||||||
|
|
||||||
var currentUsageIndex = GetUsageIndex(guiData.Property);
|
var currentUsageIndex = GetUsageIndex(property);
|
||||||
var newUsageValue = EditorGUI.Popup(button, currentUsageIndex, GetPopupOptions(guiData.Property), _popupStyle);
|
var newUsageValue = EditorGUI.Popup(button, currentUsageIndex, GetPopupOptions(property), _popupStyle);
|
||||||
SetUsageIndex(guiData.Property, newUsageValue);
|
SetUsageIndex(property, newUsageValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void DrawField(string usageTypePropertyName, in GuiData guiData, in Rect originalPosition)
|
private void DrawField(in Rect position, SerializedProperty property, GUIContent label, in Rect originalPosition)
|
||||||
{
|
{
|
||||||
var usageTypeProperty = guiData.Property.FindPropertyRelative(usageTypePropertyName);
|
string usageTypePropertyName = GetUsages(property)[GetUsageIndex(property)].PropertyName;
|
||||||
|
var usageTypeProperty = property.FindPropertyRelative(usageTypePropertyName);
|
||||||
|
|
||||||
if (usageTypeProperty == null)
|
if (usageTypeProperty == null)
|
||||||
{
|
{
|
||||||
EditorGUI.LabelField(guiData.Position, "[Non serialized value]");
|
EditorGUI.LabelField(position, "[Non serialized value]");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -121,10 +116,10 @@ namespace UnityAtoms.Editor
|
|||||||
// In versions prior to 2022.3 GetPropertyHeight returns the wrong value for "SerializedPropertyType.Quaternion"
|
// In versions prior to 2022.3 GetPropertyHeight returns the wrong value for "SerializedPropertyType.Quaternion"
|
||||||
// In later versions, the fix is introduced _but only_ when using the SerializedPropertyType parameter, not when using the SerializedProperty parameter version.
|
// In later versions, the fix is introduced _but only_ when using the SerializedPropertyType parameter, not when using the SerializedProperty parameter version.
|
||||||
// ALSO the SerializedPropertyType parameter version does not work with the isExpanded flag which we set to true exactly for this reason a (few) lines above.
|
// ALSO the SerializedPropertyType parameter version does not work with the isExpanded flag which we set to true exactly for this reason a (few) lines above.
|
||||||
EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector3, guiData.Label) :
|
EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector3, label) :
|
||||||
EditorGUI.GetPropertyHeight(usageTypeProperty, guiData.Label);
|
EditorGUI.GetPropertyHeight(usageTypeProperty, label);
|
||||||
#else
|
#else
|
||||||
var valueFieldHeight = EditorGUI.GetPropertyHeight(usageTypeProperty, guiData.Label);
|
var valueFieldHeight = EditorGUI.GetPropertyHeight(usageTypeProperty, label);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
usageTypeProperty.isExpanded = expanded;
|
usageTypeProperty.isExpanded = expanded;
|
||||||
@ -135,7 +130,7 @@ namespace UnityAtoms.Editor
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
EditorGUI.PropertyField(guiData.Position, usageTypeProperty, GUIContent.none);
|
EditorGUI.PropertyField(position, usageTypeProperty, GUIContent.none);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,7 +147,7 @@ namespace UnityAtoms.Editor
|
|||||||
|
|
||||||
|
|
||||||
#region Auto Drag And Drop Usage Type Detection
|
#region Auto Drag And Drop Usage Type Detection
|
||||||
private void DetermineDragAndDropFieldReferenceType(in GuiData guiData)
|
private void DetermineDragAndDropFieldReferenceType(in Rect position, SerializedProperty property)
|
||||||
{
|
{
|
||||||
EventType mouseEventType = Event.current.type;
|
EventType mouseEventType = Event.current.type;
|
||||||
|
|
||||||
@ -161,7 +156,7 @@ namespace UnityAtoms.Editor
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsMouseHoveringOverProperty(guiData.Position))
|
if (!IsMouseHoveringOverProperty(position))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -177,11 +172,11 @@ namespace UnityAtoms.Editor
|
|||||||
if (draggedObject is GameObject gameObject)
|
if (draggedObject is GameObject gameObject)
|
||||||
{
|
{
|
||||||
object[] instancers = gameObject.GetComponents<IAtomInstancer>();
|
object[] instancers = gameObject.GetComponents<IAtomInstancer>();
|
||||||
UpdateUsageConfigurationOption(guiData.Property, instancers);
|
UpdateUsageConfigurationOption(property, instancers);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
UpdateUsageConfigurationOption(guiData.Property, draggedObject);
|
UpdateUsageConfigurationOption(property, draggedObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,13 +202,13 @@ namespace UnityAtoms.Editor
|
|||||||
if (isDraggedTypeSameAsUsageType)
|
if (isDraggedTypeSameAsUsageType)
|
||||||
{
|
{
|
||||||
bool isUsageSetByUser = currentUsageIndex == index;
|
bool isUsageSetByUser = currentUsageIndex == index;
|
||||||
|
bool isNewUsageIndexSet = newUsageIndex > -1;
|
||||||
|
|
||||||
if (isUsageSetByUser)
|
if (isUsageSetByUser)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isNewUsageIndexSet = newUsageIndex > -1;
|
|
||||||
if (!isNewUsageIndexSet)
|
if (!isNewUsageIndexSet)
|
||||||
{
|
{
|
||||||
newUsageIndex = index;
|
newUsageIndex = index;
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace UnityAtoms.Editor
|
|
||||||
{
|
|
||||||
public struct GuiData
|
|
||||||
{
|
|
||||||
public Rect Position;
|
|
||||||
public SerializedProperty Property;
|
|
||||||
public GUIContent Label;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: a1c20882cb5b7ab4e8c3e8fb77d1ebf8
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Loading…
Reference in New Issue
Block a user