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 SetSceneBehaviourUpgraded(UdonBehaviour behaviour) { if (!PrefabUtility.IsPartOfPrefabInstance(behaviour) && !PrefabUtility.IsPartOfPrefabAsset(behaviour)) { return; } if (!behaviour.publicVariables.TrySetVariableValue <bool>(UDONSHARP_SCENE_BEHAVIOUR_UPGRADE_MARKER, true)) { behaviour.publicVariables.RemoveVariable(UDONSHARP_SCENE_BEHAVIOUR_UPGRADE_MARKER); IUdonVariable newVar = new UdonVariable <bool>(UDONSHARP_SCENE_BEHAVIOUR_UPGRADE_MARKER, true); behaviour.publicVariables.TryAddVariable(newVar); } UdonSharpUtils.SetDirty(behaviour); }
private static void SetVariable <T>(UdonBehaviour behaviour, string variableKey, T value) { System.Type type = typeof(T); bool isNull = false; if ((value is UnityEngine.Object unityEngineObject && unityEngineObject == null) || value == null) { isNull = true; } if (isNull) { bool isRemoveType = (type == typeof(GameObject) || type == typeof(Transform) || type == typeof(UdonBehaviour)); if (isRemoveType) { behaviour.publicVariables.RemoveVariable(variableKey); } else { if (!behaviour.publicVariables.TrySetVariableValue <T>(variableKey, value)) { UdonVariable <T> varVal = new UdonVariable <T>(variableKey, value); if (!behaviour.publicVariables.TryAddVariable(varVal)) { Debug.LogError($"Could not write variable '{variableKey}' to public variables on UdonBehaviour"); } } } } else { if (!behaviour.publicVariables.TrySetVariableValue <T>(variableKey, value)) { UdonVariable <T> varVal = new UdonVariable <T>(variableKey, value); if (!behaviour.publicVariables.TryAddVariable(varVal)) { Debug.LogError($"Could not write variable '{variableKey}' to public variables on UdonBehaviour"); } } } }
private static void SetVarInternal <T>(UdonBehaviour behaviour, string variableKey, T value) { if (!behaviour.publicVariables.TrySetVariableValue <T>(variableKey, value)) { UdonVariable <T> varVal = new UdonVariable <T>(variableKey, value); if (!behaviour.publicVariables.TryAddVariable(varVal)) { if (!behaviour.publicVariables.RemoveVariable(variableKey) || !behaviour.publicVariables.TryAddVariable(varVal)) // Fallback in case the value already exists for some reason { Debug.LogError($"Could not write variable '{variableKey}' to public variables on UdonBehaviour"); } else { Debug.LogWarning($"Storage for variable '{variableKey}' of type '{typeof(T)}' did not match, updated storage type"); } } } }
public void SyncValues() { if (uB == null) { uB = GetComponent <UdonBehaviour>(); if (uB == null) { return; } } var fields = GetType().GetFields().Where(f => f.GetAttribute <UdonPublicAttribute>() != null); foreach (var field in fields) { var uAttr = field.GetAttribute <UdonPublicAttribute>(); var fieldType = field.GetReturnType(); var fieldValue = field.GetValue(this); var fieldName = uAttr.varName.IsNullOrWhitespace() ? field.Name : uAttr.varName; var hiddenTypes = new[] { typeof(UdonBehaviour), typeof(GameObject), typeof(Transform), typeof(UdonBehaviour[]) }; if (hiddenTypes.Contains(fieldType)) { if (fieldValue != null) { IUdonVariable var; if (fieldType == typeof(UdonBehaviour[])) { var converted = (fieldValue as UdonBehaviour[]).Select(i => i as Component).ToArray(); if (Application.isPlaying) { uB.SetProgramVariable(fieldName, converted); continue; } uB.publicVariables.TrySetVariableValue(fieldName, converted); continue; } if (fieldType == typeof(UdonBehaviour)) { var = new UdonVariable <UdonBehaviour>(fieldName, (UdonBehaviour)fieldValue); } else if (fieldType == typeof(GameObject)) { var = new UdonVariable <GameObject>(fieldName, (GameObject)fieldValue); } else if (fieldType == typeof(Transform)) { var = new UdonVariable <Transform>(fieldName, (Transform)fieldValue); } else { continue; } if (Application.isPlaying) { uB.SetProgramVariable(fieldName, var); continue; } uB.publicVariables.RemoveVariable(fieldName); uB.publicVariables.TryAddVariable(var); uB.publicVariables.TrySetVariableValue(fieldName, var); } continue; } if (Application.isPlaying) { uB.SetProgramVariable(fieldName, fieldValue); continue; } uB.publicVariables.TrySetVariableValue(fieldName, fieldValue); } }