public override void Get(int index, ref Variable variable) { if (variable is VariableBase <T> ) { VariableBase <T> vt = variable as VariableBase <T>; vt.Value = collection[index]; } else { Debug.LogError("Collection cannot get variable " + variable.Key + ". Is not matching type:" + typeof(T).Name); } }
// 获取,或添加变量 public VariableBase <T> GetOrAddVariable <T>(string variableKey, T defaultvalue, Type type) { Variable v = null; VariableBase <T> vAsT = null; // 返回:是否包含此变量 var res = variables.TryGetValue(variableKey, out v); // 有找到变量 if (res && v != null) { vAsT = v as VariableBase <T>; if (vAsT != null) { return(vAsT); } else { Debug.LogError("A fungus variable of name " + variableKey + " already exists, but of a different type"); } } else { // 没有找到变量 // 创建变量 // 添加到GlobalVariables的Flowchart中 //create the variable vAsT = holder.gameObject.AddComponent(type) as VariableBase <T>; vAsT.Value = defaultvalue; vAsT.Key = variableKey; vAsT.Scope = VariableScope.Public; variables[variableKey] = vAsT; // Q: 这里,为什么还要保存到一个FlowChart里? holder.Variables.Add(vAsT); } return(vAsT); }