框架上传
This commit is contained in:
59
Assets/00.StaryEvo/Samples/Runtime/IOC/Script/DIPExample.cs
Normal file
59
Assets/00.StaryEvo/Samples/Runtime/IOC/Script/DIPExample.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
#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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user