Unity进阶--对象池数据场景管理器笔记

泛型单例类

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;
        }
    }
}

泛型单例类(不带组件版)

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;
    }
}

对象池管理器

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);
        }
    }

}

数据管理器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uWsTk7ac-1690360831404)(QQ%E6%88%AA%E5%9B%BE20230726091635.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s6XIAsND-1690360831406)(QQ%E6%88%AA%E5%9B%BE20230726093855.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jzymn27z-1690360831407)(QQ%E6%88%AA%E5%9B%BE20230726094052.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HeGkPdaw-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094141.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ed6LFKt-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094604.png)]

场景管理器

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);
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值