/// <summary> /// Finds an existing proxy behaviour, if none exists returns null /// </summary> /// <param name="udonBehaviour"></param> /// <returns></returns> private static UdonSharpBehaviour FindProxyBehaviour_Internal(UdonBehaviour udonBehaviour) { if (_proxyBehaviourLookup.TryGetValue(udonBehaviour, out UdonSharpBehaviour proxyBehaviour)) { if (proxyBehaviour != null) { return(proxyBehaviour); } _proxyBehaviourLookup.Remove(udonBehaviour); } UdonSharpBehaviour[] behaviours = udonBehaviour.GetComponents <UdonSharpBehaviour>(); foreach (UdonSharpBehaviour udonSharpBehaviour in behaviours) { IUdonBehaviour backingBehaviour = GetBackingUdonBehaviour(udonSharpBehaviour); if (backingBehaviour != null && ReferenceEquals(backingBehaviour, udonBehaviour)) { _proxyBehaviourLookup.Add(udonBehaviour, udonSharpBehaviour); return(udonSharpBehaviour); } } return(null); }
public static UdonSharpBehaviour FindProxyBehaviour(UdonBehaviour udonBehaviour, ProxySerializationPolicy proxySerializationPolicy) { if (_proxyBehaviourLookup.TryGetValue(udonBehaviour, out UdonSharpBehaviour proxyBehaviour)) { if (proxyBehaviour != null) { CopyUdonToProxy(proxyBehaviour, proxySerializationPolicy); SetIgnoreEvents(true); try { proxyBehaviour.enabled = false; } finally { SetIgnoreEvents(false); } return(proxyBehaviour); } else { _proxyBehaviourLookup.Remove(udonBehaviour); } } UdonSharpBehaviour[] behaviours = udonBehaviour.GetComponents <UdonSharpBehaviour>(); foreach (UdonSharpBehaviour udonSharpBehaviour in behaviours) { IUdonBehaviour backingBehaviour = GetBackingUdonBehaviour(udonSharpBehaviour); if (backingBehaviour != null && ReferenceEquals(backingBehaviour, udonBehaviour)) { _proxyBehaviourLookup.Add(udonBehaviour, udonSharpBehaviour); CopyUdonToProxy(udonSharpBehaviour, proxySerializationPolicy); SetIgnoreEvents(true); try { udonSharpBehaviour.enabled = false; } finally { SetIgnoreEvents(false); } return(udonSharpBehaviour); } } return(null); }
private static void RunBehaviourSetup(UdonSharpBehaviour behaviour, bool withUndo) { UdonBehaviour backingBehaviour = GetBackingUdonBehaviour(behaviour); // Handle components pasted across different behaviours if (backingBehaviour && backingBehaviour.gameObject != behaviour.gameObject) { backingBehaviour = null; } // Handle pasting components on the same behaviour, assumes pasted components are always the last in the list. if (backingBehaviour) { int refCount = 0; UdonSharpBehaviour[] behaviours = backingBehaviour.GetComponents <UdonSharpBehaviour>(); foreach (UdonSharpBehaviour udonSharpBehaviour in behaviours) { if (GetBackingUdonBehaviour(udonSharpBehaviour) == backingBehaviour) { refCount++; } } if (refCount > 1 && behaviour == behaviours.Last()) { backingBehaviour = null; } } bool isPartOfPrefabInstance = PrefabUtility.IsPartOfPrefabInstance(behaviour) && PrefabUtility.GetCorrespondingObjectFromSource(behaviour) != behaviour; if (backingBehaviour == null) { if (isPartOfPrefabInstance) { UdonSharpUtils.LogWarning("Cannot setup behaviour on prefab instance, original prefab asset needs setup"); return; } SetIgnoreEvents(true); try { backingBehaviour = withUndo ? Undo.AddComponent <UdonBehaviour>(behaviour.gameObject) : behaviour.gameObject.AddComponent <UdonBehaviour>(); #pragma warning disable CS0618 // Type or member is obsolete backingBehaviour.SynchronizePosition = false; backingBehaviour.AllowCollisionOwnershipTransfer = false; #pragma warning restore CS0618 // Type or member is obsolete MoveComponentRelativeToComponent(backingBehaviour, behaviour, false); SetBackingUdonBehaviour(behaviour, backingBehaviour); SetBehaviourVersion(backingBehaviour, UdonSharpBehaviourVersion.CurrentVersion); SetSceneBehaviourUpgraded(backingBehaviour); // UdonSharpUtils.Log($"Created behaviour {backingBehaviour}", behaviour); } finally { SetIgnoreEvents(false); } _proxyBehaviourLookup.Add(backingBehaviour, behaviour); UdonSharpUtils.SetDirty(behaviour); UdonSharpUtils.SetDirty(backingBehaviour); } // Handle U# behaviours that have been added to a prefab via Added Component > Apply To Prefab, but have not had their backing behaviour added // if (isPartOfPrefabInstance && // backingBehaviour != null && // !PrefabUtility.IsPartOfPrefabInstance(backingBehaviour)) // { // PropertyModification[] modifications = PrefabUtility.GetPropertyModifications(behaviour); // // if (modifications != null) // { // // } // } UdonSharpProgramAsset programAsset = GetUdonSharpProgramAsset(behaviour); if (backingBehaviour.programSource == null) { backingBehaviour.programSource = programAsset; if (backingBehaviour.programSource == null) { UdonSharpUtils.LogError($"Unable to find valid U# program asset associated with script '{behaviour}'", behaviour); } UdonSharpUtils.SetDirty(backingBehaviour); } if (_serializedProgramAssetField.GetValue(backingBehaviour) == null) { SerializedObject componentAsset = new SerializedObject(backingBehaviour); SerializedProperty serializedProgramAssetProperty = componentAsset.FindProperty("serializedProgramAsset"); serializedProgramAssetProperty.objectReferenceValue = programAsset.SerializedProgramAsset; if (withUndo) { componentAsset.ApplyModifiedProperties(); } else { componentAsset.ApplyModifiedPropertiesWithoutUndo(); } } if (backingBehaviour.enabled != behaviour.enabled) { if (withUndo) { Undo.RecordObject(backingBehaviour, "Enabled change"); } backingBehaviour.enabled = behaviour.enabled; if (!withUndo) { UdonSharpUtils.SetDirty(backingBehaviour); } } #if UDONSHARP_DEBUG backingBehaviour.hideFlags &= ~HideFlags.HideInInspector; #else backingBehaviour.hideFlags |= HideFlags.HideInInspector; #endif ((UdonSharpProgramAsset)backingBehaviour.programSource)?.UpdateProgram(); }