91 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-12-14 18:27:59 +08:00
using QFramework;
using System;
2024-12-14 18:27:59 +08:00
using System.Collections;
using System.Collections.Generic;
2025-06-16 17:48:00 +08:00
using System.Linq;
2024-12-14 18:27:59 +08:00
using UnityEngine;
public class VarController : MonoSingleton<VarController>
{
2025-06-16 17:48:00 +08:00
[SerializeField]
2024-12-14 18:27:59 +08:00
public Dictionary<string, float> varDict;
2025-06-16 17:48:00 +08:00
[ContextMenu("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>var")]
public void PrintvarDict()
{
var entries = varDict.ToList();
for (int i = 0; i < entries.Count; i++)
{
var entry = entries[i];
Debug.Log($"Key: {entry.Key}, Value: {entry.Value}");
}
}
2024-12-14 18:27:59 +08:00
public override void OnSingletonInit()
{
base.OnSingletonInit();
varDict = new Dictionary<string, float>();
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
2024-12-14 18:27:59 +08:00
}
private void OnQuit(OnModuleQuit quit)
{
varDict.Clear();
}
2024-12-14 18:27:59 +08:00
public void Set(string key, float value)
{
if (varDict.ContainsKey(key))
{
varDict[key] = value;
}
else
{
varDict.Add(key, value);
}
}
public float Get(string key)
{
if (varDict.ContainsKey(key))
{
return varDict[key];
}
else
{
return 0;
}
}
protected override void OnDestroy()
{
base.OnDestroy();
2025-02-07 17:06:19 +08:00
if (varDict!=null)
{
varDict.Clear();
varDict = null;
}
2024-12-14 18:27:59 +08:00
}
}