Files
plugin-library/Assets/00.StaryEvo/~Samples/Runtime/IOC/Script/IOCExample.cs

51 lines
1.2 KiB
C#
Raw Normal View History

2025-03-31 11:16:52 +08:00

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("蓝牙连接成功");
}
}
}