mirror of
https://github.com/HoonTB/Project-AS.git
synced 2025-12-26 11:51:21 +09:00
74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class VariableManager
|
|
{
|
|
private static VariableManager _instance;
|
|
public static VariableManager Instance => _instance ??= new VariableManager();
|
|
|
|
private Dictionary<string, object> _variables = new();
|
|
|
|
public void SetVariable(string name, string value)
|
|
{
|
|
// Try to parse as int or float, otherwise string
|
|
if (int.TryParse(value, out int intVal))
|
|
_variables[name] = intVal;
|
|
else if (float.TryParse(value, out float floatVal))
|
|
_variables[name] = floatVal;
|
|
else
|
|
_variables[name] = value;
|
|
|
|
|
|
Debug.Log($"VariableManager :: Set {name} = {_variables[name]} ({_variables[name].GetType()})");
|
|
}
|
|
|
|
public object GetVariable(string name)
|
|
{
|
|
return _variables.ContainsKey(name) ? _variables[name] : null;
|
|
}
|
|
|
|
public void AddVariable(string name, string valueStr)
|
|
{
|
|
if (!_variables.ContainsKey(name))
|
|
{
|
|
SetVariable(name, valueStr);
|
|
return;
|
|
}
|
|
|
|
object current = _variables[name];
|
|
|
|
|
|
if (current is int currentInt && int.TryParse(valueStr, out int addInt))
|
|
{
|
|
_variables[name] = currentInt + addInt;
|
|
}
|
|
else if (current is float currentFloat && float.TryParse(valueStr, out float addFloat))
|
|
{
|
|
_variables[name] = currentFloat + addFloat;
|
|
}
|
|
else if (current is string currentStr)
|
|
{
|
|
_variables[name] = currentStr + valueStr;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"VariableManager :: Cannot add {valueStr} to {name} (Type: {current.GetType()})");
|
|
}
|
|
|
|
|
|
Debug.Log($"VariableManager :: Add {name} += {valueStr} -> {_variables[name]}");
|
|
}
|
|
|
|
|
|
public string ReplaceVariables(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text) || !text.Contains("{")) return text;
|
|
|
|
foreach (var kvp in _variables)
|
|
{
|
|
text = text.Replace($"{{{kvp.Key}}}", kvp.Value.ToString());
|
|
}
|
|
return text;
|
|
}
|
|
}
|