using System; using System.Collections.Generic; using UnityEngine; [DisallowMultipleComponent] public sealed class VariableBehavior : NodeBehavior { private static readonly Dictionary SharedVariables = new Dictionary(StringComparer.Ordinal); private void Start() { string key = GetKey(); if (!SharedVariables.ContainsKey(key)) { SharedVariables[key] = GetInputValue("Value").Normalize(); } SetLocalOutputValueIfChanged("Value", SharedVariables[key]); } public override void OnRun(string inputPortName) { if (string.Equals(inputPortName, "Set", StringComparison.Ordinal)) { string key = GetKey(); SharedVariables[key] = GetInputValue("Value").Normalize(); SetLocalOutputValueIfChanged("Value", SharedVariables[key]); TriggerOutput("Set"); } } public override void OnGraphUpdate() { string key = GetKey(); if (SharedVariables.TryGetValue(key, out var value)) { SetLocalOutputValueIfChanged("Value", value); } } private string GetKey() { string text = GetInputValue("Name").AsText(); if (string.IsNullOrWhiteSpace(text)) { return "Variable"; } return text.Trim(); } }