修该json
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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85f49b30db919ac4c890c9af5f58778c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
51
Assets/00.StaryEvo/~Samples/Runtime/IOC/Script/IOCExample.cs
Normal file
51
Assets/00.StaryEvo/~Samples/Runtime/IOC/Script/IOCExample.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
using Stary.Evo;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class IOCExample : MonoBehaviour
|
||||
{
|
||||
void Start()
|
||||
{
|
||||
// 创建一个 IOC 容器
|
||||
var container = new IOCContainer();
|
||||
|
||||
// 注册一个蓝牙管理器的实例
|
||||
container.Register<IBluetoothManager>(new BluetoothManager());
|
||||
|
||||
// 根据类型获取蓝牙管理器的实例
|
||||
var bluetoothManager = container.Get<IBluetoothManager>();
|
||||
|
||||
//连接蓝牙
|
||||
bluetoothManager.Connect();
|
||||
|
||||
container.Register<IBluetoothManager>(new BluetoothManager1());
|
||||
|
||||
// 根据类型获取蓝牙管理器的实例
|
||||
var bluetoothManager1 = container.Get<IBluetoothManager>();
|
||||
|
||||
//连接蓝牙
|
||||
bluetoothManager1.Connect();
|
||||
}
|
||||
|
||||
public interface IBluetoothManager
|
||||
{
|
||||
void Connect();
|
||||
}
|
||||
|
||||
|
||||
public class BluetoothManager:IBluetoothManager
|
||||
{
|
||||
public void Connect()
|
||||
{
|
||||
Debug.Log("蓝牙连接成功");
|
||||
}
|
||||
}
|
||||
public class BluetoothManager1:IBluetoothManager
|
||||
{
|
||||
public void Connect()
|
||||
{
|
||||
Debug.Log("蓝牙连接成功");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5878fd1bc3520e4a81c1a8588916b3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user