Skip to content

Commit 49d5f3c

Browse files
Added Overridable flag to allow overriding the found value with any object that implements the expected interface even if it does not comply with the other attributes
1 parent b871efe commit 49d5f3c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

SceneRefAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public enum Flag
6262
/// Excludes components on current GameObject from search(only applies to Child and Parent).
6363
/// </summary>
6464
ExcludeSelf = 1 << 3,
65+
66+
/// <summary>
67+
/// Allows the user to manually set the reference and does not validate the location if manually set
68+
/// </summary>
69+
Overridable = 1 << 4 | Editable
6570
}
6671

6772
/// <summary>

SceneRefAttributeValidator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,13 @@ private static bool ValidateRef(SceneRefAttribute attr, Component c, FieldInfo f
425425
{
426426
Type fieldType = field.FieldType;
427427
bool isCollection = IsCollectionType(fieldType, out bool _, out bool _);
428+
bool isOverridable = attr.HasFlags(Flag.Overridable);
428429

429430
if (value is ISerializableRef ser)
430431
{
431432
value = ser.SerializedObject;
432433
}
433-
434+
434435
if (IsEmptyOrNull(value, isCollection))
435436
{
436437
if (attr.HasFlags(Flag.Optional))
@@ -461,6 +462,9 @@ private static bool ValidateRef(SceneRefAttribute attr, Component c, FieldInfo f
461462

462463
if (o != null)
463464
{
465+
if (isOverridable)
466+
continue;
467+
464468
if (attr.HasFlags(Flag.ExcludeSelf) && o is Component valueC &&
465469
valueC.gameObject == c.gameObject)
466470
Debug.LogError($"{c.GetType().Name} {elementType?.Name}[] ref '{field.Name}' cannot contain component from the same GameObject", c.gameObject);
@@ -477,6 +481,9 @@ private static bool ValidateRef(SceneRefAttribute attr, Component c, FieldInfo f
477481
}
478482
else
479483
{
484+
if (isOverridable)
485+
return true;
486+
480487
if (attr.HasFlags(Flag.ExcludeSelf) && value is Component valueC && valueC.gameObject == c.gameObject)
481488
Debug.LogError($"{c.GetType().Name} {fieldType.Name} ref '{field.Name}' cannot be on the same GameObject", c.gameObject);
482489

0 commit comments

Comments
 (0)