Skip to content

Commit

Permalink
Add checking for if proxies are in a valid state to copy back
Browse files Browse the repository at this point in the history
- Add check for if proxies are in a valid state to copy back to UdonBehaviours, this prevents potential cases where UdonBehaviour data could be cleared when using custom inspectors
  • Loading branch information
MerlinVR committed Sep 23, 2020
1 parent 686b7bc commit ae1ab6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Assets/UdonSharp/Editor/Editors/UdonSharpBehaviourEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,13 @@ void OnUndoRedo()

if (customEditorType != null) // Only do the undo copying on things with a custom inspector
{
UdonSharpEditorUtility.CopyProxyToUdon(inspectorTarget, ProxySerializationPolicy.All);

if (PrefabUtility.IsPartOfPrefabInstance(behaviour))
PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour);
if ((bool)typeof(UdonSharpBehaviour).GetField("_isValidForAutoCopy", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(inspectorTarget))
{
UdonSharpEditorUtility.CopyProxyToUdon(inspectorTarget, ProxySerializationPolicy.All);

if (PrefabUtility.IsPartOfPrefabInstance(behaviour))
PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour);
}
}
}
}
Expand All @@ -406,6 +409,8 @@ void CleanupProxy()
}
}
}

static FieldInfo _autoCopyValidField = null;

public override void OnInspectorGUI()
{
Expand Down Expand Up @@ -445,6 +450,11 @@ public override void OnInspectorGUI()
UdonSharpBehaviour inspectorTarget = UdonSharpEditorUtility.GetProxyBehaviour(behaviour, ProxySerializationPolicy.All);
inspectorTarget.enabled = false;

if (_autoCopyValidField == null)
_autoCopyValidField = typeof(UdonSharpBehaviour).GetField("_isValidForAutoCopy", BindingFlags.NonPublic | BindingFlags.Instance);

_autoCopyValidField.SetValue(inspectorTarget, true);

Editor.CreateCachedEditorWithContext(inspectorTarget, this, customEditorType, ref baseEditor);
currentProxyBehaviour = inspectorTarget;

Expand Down
3 changes: 3 additions & 0 deletions Assets/UdonSharp/Scripts/UdonSharpBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()

[OdinSerialize]
private IUdonBehaviour _backingUdonBehaviour;

[SerializeField]
private bool _isValidForAutoCopy = false;
#endif
}
}

0 comments on commit ae1ab6d

Please sign in to comment.