59 lines
1.4 KiB
C#
59 lines
1.4 KiB
C#
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
using UnityEngine;
|
|
|
|
namespace Stary.Evo.Example.ClickClick
|
|
{
|
|
public class DIPExample : MonoBehaviour
|
|
{
|
|
// 1. 设计模块接口
|
|
public interface IStorage
|
|
{
|
|
void SaveString(string key, string value);
|
|
string LoadString(string key, string defaultValue = "");
|
|
}
|
|
|
|
// 2. 实现接口
|
|
// 运行时存储
|
|
public class PlayerPrefsStorage : IStorage
|
|
{
|
|
public void SaveString(string key, string value)
|
|
{
|
|
PlayerPrefs.SetString(key, value);
|
|
}
|
|
|
|
public string LoadString(string key, string defaultValue = "")
|
|
{
|
|
return PlayerPrefs.GetString(key, defaultValue);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 使用
|
|
private void Start()
|
|
{
|
|
// 创建一个 IOC 容器
|
|
var container = new IOCContainer();
|
|
|
|
// 注册运行时模块
|
|
container.Register<IStorage>(new PlayerPrefsStorage());
|
|
|
|
var storage = container.Get<IStorage>();
|
|
|
|
storage.SaveString("name", "运行时存储");
|
|
|
|
Debug.Log(storage.LoadString("name"));
|
|
|
|
// 切换实现
|
|
//container.Register<IStorage>(new EditorPrefsStorage());
|
|
|
|
storage = container.Get<IStorage>();
|
|
|
|
Debug.Log(storage.LoadString("name"));
|
|
}
|
|
}
|
|
} |