Unity Advanced--Object Pool Data Scene Manager Notes

Generic singleton class

using System.Collections;
using System.Collections.Generic;

public abstract class ManagersSingle<T> where T : new()
{
    
    
    private static T instance;


    // 获取单例实例
    public static T Instance
    {
    
    
        get
        {
    
    
            if (instance == null)
            {
    
    
                instance = new T();
            }
            return instance;
        }
    }
}

Generic singleton class (without component version)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyrSingletonBase<T> : MonoBehaviour where T : MonoBehaviour
{
    
    
    private static T instance;
    public static T Instance {
    
    
        get
        {
    
    
            return instance;
        }
    }

    protected virtual void Awake() {
    
    
        instance = this as T;
    }

    protected virtual void OnDestroy() {
    
    
        instance = null;
    }
}

object pool manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//对象池
public class PoolStack{
    
    
    //对象集合
    public Stack <UnityEngine.Object> stack = new Stack<Object>();
    //个数
    public int MaxCount = 100;
    
    //把游戏物体放入对象池
    public void Push(UnityEngine.Object go){
    
    
        if (stack.Count < MaxCount) stack.Push(go);
        else  GameObject.Destroy(go);
    }
    //从对象池取出对象
    public UnityEngine.Object Pop() {
    
    
        if (stack.Count > 0) return stack.Pop();
        return null;  
    }

    //清空池
    public void Clear(){
    
    
        foreach (UnityEngine.Object go in stack) GameObject.Destroy(go);
        stack.Clear();
    }

}

public class PoolManager :ManagersSingle<PoolManager>
{
    
    
    //管理多个池子
    Dictionary<string, PoolStack> poolDic = new Dictionary<string, PoolStack>();

    //从对象池取出对象,没有则创建一个
    public UnityEngine.Object Spawn(string poolName, UnityEngine.Object prefab){
    
    
        //如果没有对应的池子,创建池子
        if (!poolDic.ContainsKey(poolName)) poolDic.Add(poolName, new PoolStack());
        //从池子中拿出一个
        UnityEngine.Object go = poolDic[poolName].Pop();
        if (go == null) go = GameObject.Instantiate(prefab);
        return go;
    }
    //清空对象池
    public void UnSpawn(string poolName){
    
    
        if (poolDic.ContainsKey(poolName)){
    
    
            poolDic[poolName].Clear();
            poolDic.Remove(poolName);
        }
    }

}

data manager

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-uWsTk7ac-1690360831404)(QQ%E6%88%AA%E5%9B%BE20230726091635.png)]

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-s6XIAsND-1690360831406)(QQ%E6%88%AA%E5%9B%BE20230726093855.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Jzymn27z-1690360831407)(QQ%E6%88%AA%E5%9B%BE20230726094052.png)]

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-HeGkPdaw-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094141.png)]

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-8ed6LFKt-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094604.png)]

scene manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneManager : MyrSingletonBase<SceneManager>
{
    
    
    //场景名称
    public List<string> sceneList = new List<string>();
    //当前场景
    public int CurrentIndex = 0;
    //当前场景索引
    private System.Action<float> currentAction;
    //当前加载场景对象
    private AsyncOperation operation;
   
    public void LoadScene(string sceneName, System.Action<float> action){
    
    
        currentAction = action;
        if (sceneList.Contains(sceneName))
        {
    
    
            //更新场景索引
            CurrentIndex = sceneList.IndexOf(sceneName);
            //加载场景
            operation = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, UnityEngine.SceneManagement.LoadSceneMode.Single);
        }
    }

    void Update(){
    
    
        if (operation != null){
    
    
            
            currentAction(operation.progress);
            //场景加载完成
            if (operation.progress >= 1) operation = null;
        }
    }
    
    //加载上一个场景
    public void LoadPre(System.Action<float> action){
    
    
        CurrentIndex--;
        LoadScene(sceneList[CurrentIndex], action);
    }

    //加载上一个场景
    public void LoadNext(System.Action<float> action){
    
    
        CurrentIndex++;
        LoadScene(sceneList[CurrentIndex], action);
    }
}


Guess you like

Origin blog.csdn.net/abaidaye/article/details/131942659