internal static void SetBehaviourVersion(UdonBehaviour behaviour, UdonSharpBehaviourVersion version) { UdonSharpBehaviourVersion lastVer = GetBehaviourVersion(behaviour); if (lastVer == version && lastVer != UdonSharpBehaviourVersion.V0) { return; } bool setVer = behaviour.publicVariables.TrySetVariableValue <int>(UDONSHARP_BEHAVIOUR_VERSION_KEY, (int)version); if (!setVer) { behaviour.publicVariables.RemoveVariable(UDONSHARP_BEHAVIOUR_VERSION_KEY); IUdonVariable newVar = new UdonVariable <int>(UDONSHARP_BEHAVIOUR_VERSION_KEY, (int)version); setVer = behaviour.publicVariables.TryAddVariable(newVar); } if (setVer) { UdonSharpUtils.SetDirty(behaviour); return; } UdonSharpUtils.LogError("Could not set version variable"); }
internal static void UpgradeSceneBehaviours(IEnumerable <UdonBehaviour> behaviours) { if (EditorApplication.isPlaying) { return; } // Create proxies if they do not exist foreach (UdonBehaviour udonBehaviour in behaviours) { if (!IsUdonSharpBehaviour(udonBehaviour)) { continue; } if (PrefabUtility.IsPartOfPrefabInstance(udonBehaviour) && PrefabUtility.IsAddedComponentOverride(udonBehaviour)) { continue; } if (GetProxyBehaviour(udonBehaviour) == null) { if (PrefabUtility.IsPartOfPrefabInstance(udonBehaviour) && PrefabUtility.GetCorrespondingObjectFromSource(udonBehaviour) != udonBehaviour) { UdonSharpUtils.LogError($"Cannot upgrade scene behaviour '{udonBehaviour}' since its prefab must be upgraded.", udonBehaviour); continue; } Type udonSharpBehaviourType = GetUdonSharpBehaviourType(udonBehaviour); if (!udonSharpBehaviourType.IsSubclassOf(typeof(UdonSharpBehaviour))) { UdonSharpUtils.LogError($"Class script referenced by program asset '{udonBehaviour.programSource}' is not an UdonSharpBehaviour", udonBehaviour.programSource); continue; } UdonSharpBehaviour newProxy = (UdonSharpBehaviour)udonBehaviour.gameObject.AddComponent(udonSharpBehaviourType); newProxy.enabled = udonBehaviour.enabled; SetBackingUdonBehaviour(newProxy, udonBehaviour); if (!PrefabUtility.IsAddedComponentOverride(udonBehaviour)) { MoveComponentRelativeToComponent(newProxy, udonBehaviour, true); } else { UdonSharpUtils.LogWarning($"Cannot reorder internal UdonBehaviour for '{udonBehaviour}' during upgrade because it is on a prefab instance.", udonBehaviour.gameObject); } UdonSharpUtils.SetDirty(newProxy); } if (GetBehaviourVersion(udonBehaviour) == UdonSharpBehaviourVersion.V0) { SetBehaviourVersion(udonBehaviour, UdonSharpBehaviourVersion.V0DataUpgradeNeeded); } } // Copy data over from UdonBehaviour to UdonSharpBehaviour foreach (UdonBehaviour udonBehaviour in behaviours) { if (!IsUdonSharpBehaviour(udonBehaviour)) { continue; } bool needsPrefabInstanceUpgrade = false; // Checks if the version is below V1 or if it needs the prefab instance upgrade UdonSharpBehaviourVersion behaviourVersion = GetBehaviourVersion(udonBehaviour); if (behaviourVersion >= UdonSharpBehaviourVersion.V1) { // Check if the prefab instance has a prefab that was upgraded causing the string data to be copied, but has a delta'd UnityEngine.Object storage array if (PrefabUtility.IsPartOfPrefabInstance(udonBehaviour) && !HasSceneBehaviourUpgradeFlag(udonBehaviour)) { UdonBehaviour prefabSource = PrefabUtility.GetCorrespondingObjectFromSource(udonBehaviour); if (prefabSource && BehaviourRequiresBackwardsCompatibilityPersistence(prefabSource)) { PropertyModification[] modifications = PrefabUtility.GetPropertyModifications(udonBehaviour); if (modifications != null && modifications.Any(e => e.propertyPath.StartsWith("publicVariablesUnityEngineObjects", StringComparison.Ordinal))) { needsPrefabInstanceUpgrade = true; } } } if (!needsPrefabInstanceUpgrade) { continue; } } UdonSharpBehaviour proxy = GetProxyBehaviour(udonBehaviour); if (proxy == null) { UdonSharpUtils.LogWarning($"UdonSharpBehaviour '{udonBehaviour}' could not be upgraded since it is missing a proxy", udonBehaviour); continue; } CopyUdonToProxy(proxy, ProxySerializationPolicy.RootOnly); // Nuke out old data now because we want only the C# side to own the data from this point on ClearBehaviourVariables(udonBehaviour, true); SetBehaviourVersion(udonBehaviour, UdonSharpBehaviourVersion.V1); SetSceneBehaviourUpgraded(udonBehaviour); if (needsPrefabInstanceUpgrade) { UdonSharpUtils.Log($"Scene behaviour '{udonBehaviour.name}' needed UnityEngine.Object upgrade pass", udonBehaviour); } UdonSharpUtils.SetDirty(proxy); UdonSharpUtils.Log($"Upgraded scene behaviour '{udonBehaviour.name}'", udonBehaviour); } }