Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.0.53 #56

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 112 additions & 28 deletions Assets/BetterCommons/Editor/Drawers/SerializeReferenceField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public long UpdateInterval
}
}

public SerializedPropertyType PropertyType { get; private set; }

public SerializeReferenceField(SerializedProperty property) : this(property, string.Empty)
{
}
Expand All @@ -56,10 +58,12 @@ public SerializeReferenceField(SerializedProperty property, string label)
_serializedObject = property.serializedObject;
_referenceType = property.managedReferenceFullTypename;

PropertyType = property.propertyType;
PropertyField = CreatePropertyField(property, label);

#if !UNITY_2022_2_OR_NEWER
if (IsLastSerializeField() && TryCreateBufferLabel(property, out var bufferLabel))
var hasLabel = HasLabel(property);
if (!hasLabel && TryCreateBufferLabel(property, out var bufferLabel))
{
_bufferLabel = bufferLabel;
Insert(0, _bufferLabel);
Expand Down Expand Up @@ -99,8 +103,8 @@ private void Update()
}

#if !UNITY_2022_2_OR_NEWER
var isLast = IsLastSerializeField();
if (!isLast && _bufferLabel != null)
var hasLabel = HasLabel(property);
if (hasLabel && _bufferLabel != null)
{
_bufferLabel.RemoveFromHierarchy();
_bufferLabel = null;
Expand All @@ -123,7 +127,7 @@ private void Update()
finally
{
#if !UNITY_2022_2_OR_NEWER
if (isLast && TryCreateBufferLabel(property, out var label))
if (!hasLabel && TryCreateBufferLabel(property, out var label))
{
_bufferLabel = label;
Insert(0, _bufferLabel);
Expand All @@ -150,13 +154,114 @@ private void Update()
property.Dispose();
}

private void ReactToEditorChange()
{
UpdateSerializedObjectIfNeeded();
Update();
}

#if !UNITY_2022_2_OR_NEWER
private void UpdateSerializedObjectIfNeeded()
{
if (_recentSerializedObjects.Contains(_serializedObject))
return;

_serializedObject.Update();

var isTheFirstAddition = _recentSerializedObjects.Count == 0;
_recentSerializedObjects.Add(_serializedObject);

if (isTheFirstAddition)
EditorApplication.delayCall += ClearRecentObjects;
}

private bool IsLastSerializeField()
private bool HasLabel(SerializedProperty property)
{
var query = this.Query<SerializeReferenceField>().Last();
return query == this;
return query == this && ValidateBaseField(property);
}

private bool ValidateBaseField(SerializedProperty property)
{
var propertyType = property.propertyType;
switch (propertyType)
{
case SerializedPropertyType.Generic:
return property.isArray && HasValidLabel<IntegerField>();
case SerializedPropertyType.Integer:
return HasValidLabel<IntegerField>();
case SerializedPropertyType.Boolean:
return HasValidLabel<Toggle>();
case SerializedPropertyType.Float:
return HasValidLabel<FloatField>();
case SerializedPropertyType.String:
return HasValidLabel<TextField>();
case SerializedPropertyType.Color:
return HasValidLabel<ColorField>();
case SerializedPropertyType.ObjectReference:
return HasValidLabel<ObjectField>();
case SerializedPropertyType.LayerMask:
return HasValidLabel<LayerMaskField>();
case SerializedPropertyType.Enum:
return HasValidLabel<PopupField<string>>();
case SerializedPropertyType.Vector2:
return HasValidLabel<Vector2Field>();
case SerializedPropertyType.Vector3:
return HasValidLabel<Vector3Field>();
case SerializedPropertyType.Vector4:
return HasValidLabel<Vector4Field>();
case SerializedPropertyType.Rect:
return HasValidLabel<RectField>();
case SerializedPropertyType.ArraySize:
return HasValidLabel<IntegerField>();
case SerializedPropertyType.Character:
return HasValidLabel<TextField>();
case SerializedPropertyType.AnimationCurve:
return HasValidLabel<CurveField>();
case SerializedPropertyType.Bounds:
return HasValidLabel<BoundsField>();
case SerializedPropertyType.Gradient:
return HasValidLabel<GradientField>();
case SerializedPropertyType.Quaternion:
return HasValidLabel<Vector4Field>();
case SerializedPropertyType.ExposedReference:
return false;
case SerializedPropertyType.FixedBufferSize:
return false;
case SerializedPropertyType.Vector2Int:
return HasValidLabel<Vector2IntField>();
case SerializedPropertyType.Vector3Int:
return HasValidLabel<Vector3IntField>();
case SerializedPropertyType.RectInt:
return HasValidLabel<RectIntField>();
case SerializedPropertyType.BoundsInt:
return HasValidLabel<BoundsIntField>();
case SerializedPropertyType.Hash128:
return HasValidLabel<Hash128Field>();
default:
return false;
}
}

private bool HasValidLabel<TValueType>()
{
if (TryGetBaseField<TValueType>(out var baseField))
{
return baseField.labelElement != null;
}

return false;
}

private bool TryGetBaseField<TValueType>(out BaseField<TValueType> baseField)
{
baseField = PropertyField.Query().Children<BaseField<TValueType>>().First();
var validParent = baseField.parent == PropertyField;
if (!validParent)
{
baseField = null;
}

return validParent;
}

private bool TryCreateBufferLabel(SerializedProperty property, out Label label)
Expand All @@ -173,27 +278,6 @@ private bool TryCreateBufferLabel(SerializedProperty property, out Label label)
label = null;
return false;
}
#endif

private void ReactToEditorChange()
{
UpdateSerializedObjectIfNeeded();
Update();
}

private void UpdateSerializedObjectIfNeeded()
{
if (_recentSerializedObjects.Contains(_serializedObject))
return;

_serializedObject.Update();

var isTheFirstAddition = _recentSerializedObjects.Count == 0;
_recentSerializedObjects.Add(_serializedObject);

if (isTheFirstAddition)
EditorApplication.delayCall += ClearRecentObjects;
}

private static void ClearRecentObjects()
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/BetterCommons/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.commons",
"displayName": "Better Commons",
"version": "0.0.52",
"version": "0.0.53",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down
Loading