HKMBFZ/Assets/SKFramework/Tools/Variables/Variables.cs

68 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;
namespace SK.Framework
{
[Serializable]
public class Variables
{
[SerializeField] private List<Variable> variables;
public ReadOnlyCollection<Variable> VariableCollection
{
get { return variables.AsReadOnly(); }
}
public Variable this[int index]
{
get { return variables[index]; }
}
public object Get(string name)
{
if(null == variables || variables.Count <= 0)
{
return null;
}
var variable = variables.Find(v => v.Name.Equals(name));
return variable?.GetValue();
}
public void Set(string name, object value)
{
if(null == variables || variables.Count <= 0)
{
return;
}
var variable = variables.Find(v => v.Name.Equals(name));
variable?.SetValue(value);
}
public T Get<T>(string name)
{
if (null == variables || variables.Count <= 0)
{
return default;
}
var variable = variables.Find(v => v.Name.Equals(name));
return variable == null ? default : variable.GetValue<T>();
}
public void Set<T>(string name, T value)
{
if(null == variables || variables.Count <= 0)
{
return;
}
var variable = variables.Find(v => v.Name.Equals(name));
variable?.SetValue<T>(value);
}
public static implicit operator List<Variable>(Variables array)
{
return array.variables;
}
public static implicit operator Variables(List<Variable> variables)
{
return new Variables() { variables = variables };
}
}
}