Files
Cielonos/Assets/Scripts/MainGame/Base/InfoTransistor.cs
SoulliesOfficial 7bc1e1722c 爆更
2026-06-05 04:21:00 -04:00

66 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using SLSUtilities.General;
using UnityEngine;
namespace Cielonos.MainGame
{
public partial class InfoTransistor : Singleton<InfoTransistor>
{
public HashSet<string> flags = new HashSet<string>();
public Dictionary<string, object> customInfo = new Dictionary<string, object>();
protected override void Awake()
{
Initialize(true);
}
}
public partial class InfoTransistor
{
public static void SetFlag(string key)
{
Instance.flags.Add(key);
}
public static void SetInfo<T>(string key, T value)
{
Instance.customInfo[key] = value;
}
public static T GetInfo<T>(string key)
{
if (typeof(T) == typeof(bool))
{
return (T)(object)Instance.flags.Contains(key);
}
if (Instance.customInfo.TryGetValue(key, out object value))
{
if (value is T typedValue)
{
return typedValue;
}
// 兼容可能存在的隐式/强制类型转换(如 int -> float 或数值类型转换)
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch
{
Debug.LogError($"[InfoTransistor] Cannot cast object with key '{key}' from {value.GetType().Name} to {typeof(T).Name}.");
}
}
return default;
}
public static void ClearAll()
{
Instance.flags.Clear();
Instance.customInfo.Clear();
}
}
}