工具.md 4.1 KB

# 1、SceneIOCContainer

在项目中很多游戏对象不止会被一个脚本使用,可能脚本1、2、3...都会使用到同一object【组件、物体、对象】,将需要被多出引用的object存储到IOC容器,使用时从容器中拿

SceneIOCContainer代码

using System.Collections.Generic;
using Blue;

/// <summary>
/// 场景IOC容器,用于存储对象、物体
/// </summary>
public class SceneIOCContainer : Singleton<SceneIOCContainer>
{
    private Dictionary<string, object> objContainer = new Dictionary<string, object>();

    public void Push(string objName,object obj)
    {
        if(objContainer.ContainsKey(objName))
        {
            objContainer[objName] = obj;
        }
        else
        {
            objContainer.Add(objName,obj);
        }
    }

    public object Pull(string objName)
    {
        if(objContainer.ContainsKey(objName))
        {
            return objContainer[objName];
        }
        return null;
    }
}

GetObjectSystem示例

代码

// 创建GetObjectSystem
using UnityEngine;

public class GetObjectSystem : SingletonMonobehaviour<GetObjectSystem>
{
    [SerializeField] private GameObject mesh_test;
    [SerializeField] private GameObject ARSpaceForAll;
    private void Awake()
    {
        GetObj();
        PushIOC();
    }

    private void GetObj()
    {
        mesh_test = GameObject.Find("ARSpaceForAll/mesh_test");
        ARSpaceForAll = GameObject.Find("goRefrence");
    }

    private void PushIOC()
    {
        SceneIOCContainer.Instance.Push("mesh_test",mesh_test);
        SceneIOCContainer.Instance.Push("goRefrence",ARSpaceForAll);
    }
}

调用

示例 代码

// 使用
using UnityEngine;
using UnityEngine.UI;

public class MoveChange : MonoBehaviour
{
    private GameObject test => SceneIOCContainer.Instance.Pull("test") as GameObject;
    public GameObject goRefrence // 参照物
    {
        get =>SceneIOCContainer.Instance.Pull("goRefrence")as GameObject;
    }
}

2、CommandSystem

在其他人写的脚本中调用Command,由于对方并没有执行框架规则,所以通过CommandSystem执行命令

代码

namespace Blue
{
    public class CommandSystem : SingletonMonobehaviour<CommandSystem>
    {
        /// <summary>
        /// 发送命令
        /// </summary>
        public void Send(ICommand command)
        {
            this.SendCommand(command);
        }
    }
}

调用

// 其他人代码
public class Other: MonoBehaviour
{
    public void Test()
    {
        TestCommand Command = new TestCommand();
        CommandSystem.Instance.Send(Command);
    }
}

3、CoroutineSystem

在普通的C#类执行协程

代码

using System.Collections;
using UnityEngine;
using Blue;

public class CoroutineSystem : SingletonMonobehaviour<CoroutineSystem>
{
    /// <summary>
    /// 启动一个协程
    /// </summary>
    public Coroutine Start_Coroutine(IEnumerator coroutine)
    {
        return StartCoroutine(coroutine);
    }

    /// <summary>
    /// 停止一个协程序
    /// </summary>
    public void Stop_Coroutine(Coroutine routine)
    {
        StopCoroutine(routine);
    }
}

调用

public class PointService 
{
    public void GetRefrencePos(int sceneID)
    {
        CoroutineSystem.Instance.Start_Coroutine(GetRefrence(sceneID));
    }

    private IEnumerator GetRefrence(int sceneID)
    {
        yield return null;
    }
}

4、BlueObject

将一些

代码

using UnityEngine;

[CreateAssetMenu(fileName ="PrefabsAsset",menuName ="ScriptableObject/BluePrefabs",order = 1 )]
public class BlueObject : ScriptableObject
{
    //可以包含更多的数据,信息
    public GameObject WarningPopUp;
    public string NetErrorText;
    public string SuccessText;
}

调用

public class PointService 
{
    public void GetRefrencePos(int sceneID)
    {
        CoroutineSystem.Instance.Start_Coroutine(GetRefrence(sceneID));
    }

    private IEnumerator GetRefrence(int sceneID)
    {
        yield return null;
    }
}