专业的网站建设电话国贸行业 网站建设
web/
2025/10/1 21:23:30/
文章来源:
专业的网站建设电话,国贸行业 网站建设,3d动画制作流程,邯郸购物网站建设文章目录 泛型单例类泛型单例类#xff08;不带组件版#xff09;对象池管理器数据管理器场景管理器 泛型单例类
using System.Collections;
using System.Collections.Generic;public abstract class ManagersSingleT where T : new()
{private static T instance;… 文章目录 泛型单例类泛型单例类不带组件版对象池管理器数据管理器场景管理器 泛型单例类
using System.Collections;
using System.Collections.Generic;public abstract class ManagersSingleT 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 MyrSingletonBaseT : 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 StackObject();//个数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 :ManagersSinglePoolManager
{//管理多个池子Dictionarystring, PoolStack poolDic new Dictionarystring, 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);}}}
数据管理器 场景管理器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class SceneManager : MyrSingletonBaseSceneManager
{//场景名称public Liststring sceneList new Liststring();//当前场景public int CurrentIndex 0;//当前场景索引private System.Actionfloat currentAction;//当前加载场景对象private AsyncOperation operation;public void LoadScene(string sceneName, System.Actionfloat 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.Actionfloat action){CurrentIndex--;LoadScene(sceneList[CurrentIndex], action);}//加载上一个场景public void LoadNext(System.Actionfloat action){CurrentIndex;LoadScene(sceneList[CurrentIndex], action);}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/85260.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!