Skip to content

Commit 487bba2

Browse files
committed
Add AllowManualAssignment parameter to Autofill and AutofillOptional
1 parent 1d77b13 commit 487bba2

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

Packages/com.jonagill.autofill/Editor/AutofillEditorUpdater.cs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public static AutofillUpdateResult UpdateProperty(
220220
if (targetGameObject != null && autofillAttribute != null)
221221
{
222222
Component propertyComponent = property.objectReferenceValue as Component;
223-
if (!VerifyPropertyFilled(targetGameObject, autofillAttribute, propertyComponent) || force)
223+
224+
if (ShouldRunUpdate(targetGameObject, propertyComponent, autofillAttribute, force))
224225
{
225226
// Collect all of the possible components that could fill this field
226227
Component[] allPossibleComponents = null;
@@ -289,6 +290,56 @@ public static AutofillUpdateResult UpdateProperty(
289290
return result;
290291
}
291292

293+
internal static bool PropertyHasManualOverride(
294+
SerializedProperty property,
295+
AutofillAttribute autofillAttribute)
296+
{
297+
var targetObject = property.serializedObject.targetObject;
298+
var targetComponent = targetObject as Component;
299+
if (targetComponent != null)
300+
{
301+
var targetGameObject = targetComponent.gameObject;
302+
if (targetGameObject != null && autofillAttribute != null)
303+
{
304+
Component propertyComponent = property.objectReferenceValue as Component;
305+
306+
// We have a value, but it doesn't match our expected location
307+
return propertyComponent != null &&
308+
!VerifyAutofillSatisfied(targetGameObject, autofillAttribute, propertyComponent);
309+
}
310+
}
311+
312+
return false;
313+
}
314+
315+
private static bool ShouldRunUpdate(
316+
GameObject targetGameObject,
317+
Component propertyComponent,
318+
AutofillAttribute autofillAttribute,
319+
bool force)
320+
{
321+
if (propertyComponent == null)
322+
{
323+
// We have no value -- try to find one
324+
return true;
325+
}
326+
327+
if (autofillAttribute.AllowManualAssignment)
328+
{
329+
// We have manually assigned a value
330+
return false;
331+
}
332+
333+
if (force)
334+
{
335+
// Update all fields that aren't manually assigned
336+
return true;
337+
}
338+
339+
// Update only if we don't already have a valid target
340+
return !VerifyAutofillSatisfied(targetGameObject, autofillAttribute, propertyComponent);
341+
}
342+
292343
private static string GenerateErrorText(
293344
SerializedProperty property,
294345
FieldInfo fieldInfo,
@@ -326,15 +377,17 @@ void CollectHierarchyPathRecursive(Transform transform, bool first)
326377
return sb.ToString();
327378
}
328379

329-
private static bool VerifyPropertyFilled(GameObject targetGameObject, AutofillAttribute autofillAttribute,
330-
Component propertyComponent)
380+
private static bool VerifyAutofillSatisfied(
381+
GameObject targetGameObject,
382+
AutofillAttribute autofillAttribute,
383+
Component propertyValue)
331384
{
332-
if (propertyComponent == null)
385+
if (propertyValue == null)
333386
{
334387
return false;
335388
}
336389

337-
if (autofillAttribute.IncludesSelf && propertyComponent.gameObject == targetGameObject)
390+
if (autofillAttribute.IncludesSelf && propertyValue.gameObject == targetGameObject)
338391
{
339392
return true;
340393
}
@@ -343,9 +396,9 @@ private static bool VerifyPropertyFilled(GameObject targetGameObject, AutofillAt
343396
{
344397
case AutofillType.Parent:
345398
case AutofillType.SelfAndParent:
346-
return targetGameObject.transform.IsChildOf(propertyComponent.transform);
399+
return targetGameObject.transform.IsChildOf(propertyValue.transform);
347400
case AutofillType.SelfAndChildren:
348-
return propertyComponent.transform.IsChildOf(targetGameObject.transform);
401+
return propertyValue.transform.IsChildOf(targetGameObject.transform);
349402
}
350403

351404
return false;

Packages/com.jonagill.autofill/Editor/AutofillPropertyDrawer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,18 @@ void UpdateFields()
6666
}
6767

6868
var showError = errorText != null;
69-
var showField = showError || autofillAttribute.AlwaysShowInInspector;
69+
70+
var isNull = property.objectReferenceValue == null;
71+
var isManualOverride = AutofillEditorUpdater.PropertyHasManualOverride(property, autofillAttribute);
72+
73+
var showField = showError ||
74+
autofillAttribute.AlwaysShowInInspector ||
75+
(autofillAttribute.AllowManualAssignment && (isNull || isManualOverride));
7076

7177
helpBox.text = errorText;
7278
helpBox.style.display = showError ? DisplayStyle.Flex : DisplayStyle.None;
7379
propertyField.style.display = showField ? DisplayStyle.Flex : DisplayStyle.None;
80+
propertyField.SetEnabled(autofillAttribute.AllowManualAssignment);
7481
}
7582

7683
propertyField.TrackSerializedObjectValue(property.serializedObject, obj => UpdateFields());

Packages/com.jonagill.autofill/Runtime/AutofillAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ public class AutofillAttribute : PropertyAttribute
2121

2222
public readonly bool AcceptFirstValidResult;
2323
public readonly bool AlwaysShowInInspector;
24+
public readonly bool AllowManualAssignment;
2425

2526
public AutofillAttribute(
2627
AutofillType type = AutofillType.Self,
2728
bool acceptFirstValidResult = false,
28-
bool alwaysShowInInspector = false)
29+
bool alwaysShowInInspector = false,
30+
bool allowManualAssignment = false)
2931
{
3032
Type = type;
3133
AcceptFirstValidResult = acceptFirstValidResult;
3234
AlwaysShowInInspector = alwaysShowInInspector;
35+
AllowManualAssignment = allowManualAssignment;
3336
}
3437
}
3538
}

Packages/com.jonagill.autofill/Runtime/AutofillOptionalAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class AutofillOptionalAttribute : AutofillAttribute
1515
public AutofillOptionalAttribute(
1616
AutofillType type = AutofillType.Self,
1717
bool acceptFirstValidResult = false,
18-
bool alwaysShowInInspector = false) : base(type, acceptFirstValidResult, alwaysShowInInspector)
18+
bool alwaysShowInInspector = false,
19+
bool allowManualAssignment = false) : base(type, acceptFirstValidResult, alwaysShowInInspector, allowManualAssignment)
1920
{
2021
}
2122

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.jonagill.autofill",
33
"description": "A utility to automatically retrieve serializable component references on prefabs at edit time.",
4-
"version": "1.0.6",
4+
"version": "1.0.7",
55
"unity": "2019.2",
66
"displayName": "Autofill"
77
}

0 commit comments

Comments
 (0)