Unity3d对象池
Singleton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13 using UnityEngine;
/// <summary>
/// 单例模版类
/// </summary>
public class Singleton<T> where T : new() {
private static readonly T instance = new T(); public static T Instance{
get{
return instance;
}
}
}
MonoSingleton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 using UnityEngine;
/// <summary>
/// 组建单例模版
/// </summary>
public class MonoSingleten<T> : MonoBehaviour where T : MonoBehaviour{
private static T instance;
public static T Instance{
get{
if (instance == null){
GameObject go = new GameObject(typeof(T).Name);
instance = go.AddComponent<T>();
}
return instance;
}
set {
instance = value;
}
} protected virtual void Awake(){
Instance = this as T;
}
}
IReusable.cs
1
2
3
4
5
6
7
8
9
10 using UnityEngine;
/// <summary>
/// 对象池接口
/// </summary>
public interface IReusable{
//对象从对象池实例化的回调
void OnSpawned();
//对象返回对象池后的回调
void OnUnSpawned();
}
PrefabType.cs
1
2
3
4
5public enum PrefabType{
None = 0,
Effects = 1,
Roles = 2,
}
ResourcesPath.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 using UnityEngine;
/// <summary>
/// 资源路径
/// </summary>
public class ResourcesPath {
public const string prefabRoles = "Prefabs/Roles/";
public const string prefabEffects = "Prefabs/Effects/"; public static string GetPath(PrefabType type, string name){
string path = string.Empty;
switch(type){
case PrefabType.Effects:
path = ResourcesPath.prefabEffects + name;
break;
case PrefabType.Roles:
path = ResourcesPath.prefabRoles + name;
break;
}
return path;
}
}
ResourceFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 using System.Collections;
using System.Collections.Generic;
using UnityEngine; /// <summary>
/// 资源工厂
/// </summary>
public class ResourceFactory : Singleton<ResourceFactory> {
/// <summary>
/// 加载资源
/// </summary>
/// <returns>The load.</returns>
/// <param name="path">Path.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public T Load<T>(string path) where T : Object{
T res = Resources.Load<T>(path);
return res;
}
}
ObjectPoolMananger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 using System.Collections;
using System.Collections.Generic;
using UnityEngine; /// <summary>
/// 对象池管理器
/// </summary>
public class ObjectPoolMananger : MonoSingleten<ObjectPoolMananger> {
private Dictionary<string, ObjectPool> mPools = new Dictionary<string, ObjectPool>(); //从对象池取出对象
public GameObject Spawn(PrefabType type, string name, Vector3 pos = default(Vector3), Quaternion rotation = default(Quaternion), Transform parent = null){
ObjectPool pool = null;
if (!mPools.ContainsKey(name)){
//创建对象池
RegisterPoll(type, name);
}
pool = mPools[name];
//从对象池中取出一个物体
GameObject obj = pool.Spawn(); obj.transform.SetParent(parent);
obj.transform.localPosition = pos;
obj.transform.localRotation = rotation;
return obj; } /// <summary>
/// 对象池回收物体
/// </summary>
/// <param name="obj">Object.</param>
public void UnSpawn(GameObject obj){
foreach(ObjectPool pool in mPools.Values){
if (pool.Contains(obj)){
pool.UnSpawn(obj);
return ;
}
}
Destroy(obj);
} /// <summary>
/// 回收所有物体
/// </summary>
public void UnSpwanAll(){
foreach(ObjectPool pool in mPools.Values){
pool.UnSpawnAll();
}
}
private void RegisterPoll(PrefabType type, string name){
string path = ResourcesPath.GetPath(type, name);
GameObject prefab = ResourceFactory.Instance.Load<GameObject>(path);
ObjectPool pool = new ObjectPool(prefab);
mPools.Add(name, pool);
}
}
ObjectPool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 对象池类
/// </summary>
public class ObjectPool{
//预制体
private GameObject mPrefab;
//对象池
private List<GameObject> objectlist = new List<GameObject>(); //构造方法
public ObjectPool(GameObject prefab){
this.mPrefab = prefab;
} /// <summary>
/// 取出物体
/// </summary>
/// <returns>The spawn.</returns>
public GameObject Spawn(){
GameObject obj = null;
for (int i = 0; i < objectlist.Count; i++){
if (!objectlist[i].activeSelf){//如果有物体隐藏
obj = objectlist[i];
break;
}
}
if (obj == null){
obj = GameObject.Instantiate(mPrefab);
objectlist.Add(obj);
}
obj.SetActive(true); //获取对象池接口
IReusable reusable = obj.GetComponent<IReusable>();
if (reusable != null){
reusable.OnSpawned();
}
return obj;
} /// <summary>
/// 回收物体
/// </summary>
/// <param name="obj">Object.</param>
public void UnSpawn(GameObject obj){
obj.SetActive(false);
IReusable reusable = obj.GetComponent<IReusable>();
if (reusable != null){
reusable.OnUnSpawned();
}
} public void UnSpawnAll() {
foreach (GameObject obj in objectlist){
obj.SetActive(false);
IReusable reusable = obj.GetComponent<IReusable>();
if (reusable != null){
reusable.OnUnSpawned();
}
}
} /// <summary>
/// 判断物体是否存在
/// </summary>
/// <returns>The contains.</returns>
/// <param name="obj">Object.</param>
public bool Contains(GameObject obj){
return objectlist.Contains(obj);
}
}
DestoryObjectPool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 对象池销毁
/// </summary>
public class DestoryObjectPool : MonoBehaviour, IReusable {
public float mDestoryTime = 0.1f; public void OnSpawned()
{
Invoke("UnSpawn", mDestoryTime);
} public void OnUnSpawned()
{
} private void UnSpawn(){
ObjectPoolMananger.Instance.UnSpawn(gameObject);
} }
Unity3d对象池的更多相关文章
- unity3d对象池的使用
说对象池之前首先来看看单例类和单例脚本的区别.这里有介绍 http://blog.csdn.net/lzhq1982/article/details/12649281 使用对象池的好处是不用每次都创建 ...
- Unity3D 对象池思想 在游戏开发中的运用
分类:U3D 1.在王者荣耀中,每30秒小兵会出现一波,出现之后会被敌方玩家或敌方小兵销毁,一局游戏下来,小兵会被创建多次,同时也会被销毁,在游戏中,这种频繁的创建和销毁游戏对象是很损耗性能的.在游戏 ...
- Unity3D 基于预设(Prefab)的泛型对象池实现
背景 在研究Inventory Pro插件的时候,发现老外实现的一个泛型对象池,觉得设计的小巧实用,不敢私藏,特此共享出来. 以前也看过很多博友关于对象池的总结分享,但是世界这么大,这么复杂到底什么样 ...
- 游戏开发设计模式之对象池模式(unity3d 示例实现)
前篇:游戏开发设计模式之命令模式(unity3d 示例实现) 博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 原理:从一个固定 ...
- [译]Unity3D内存管理——对象池(Object Pool)
原文地址:C# Memory Management for Unity Developers (part 3 of 3), 其实从原文标题可以看出,这是一系列文章中的第三篇,前两篇讲解了从C#语言本身 ...
- Unity3D 游戏开发构架篇 —— 动态大场景生成 = 区域加载+对象池管理
项目做一个类似无尽模式的场景,想了一想,其实方法很简单,做一个相关的总结. 主要先谈一谈构架,后期附上代码. 一.区域加载 其实无尽场景的实现很简单,因为屏幕限制,那么不论何时何地,我们只能看到自己的 ...
- 设计模式之美:Object Pool(对象池)
索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):实现 DatabaseConnectionPool 类. 实现方式(二):使用对象构造方法和预分配方式实现 ObjectPool ...
- Egret中的对象池ObjectPool
为了可以让对象复用,防止大量重复创建对象,导致资源浪费,使用对象池来管理. 对象池具体含义作用,自行百度. 一 对象池A 二 对象池B 三 字符串key和对象key的效率 一 对象池A /** * 对 ...
- 对象池与.net—从一个内存池实现说起
本来想写篇关于System.Collections.Immutable中提供的ImmutableList里一些实现细节来着,结果一时想不起来源码在哪里--为什么会变成这样呢--第一次有了想写分析的源码 ...
随机推荐
- 自身使用的springboot项目中比较全的pom.xml
在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...
- java 枚举使用教程
转载:https://blog.csdn.net/newbie_907486852/article/details/81027512 一.枚举型常量 首先枚举是一个特殊的class,这个class相当 ...
- 第 9 章 数据管理 - 074 - 如何安装和配置 Rex-Ray?
Rex-Ray 安装和配置方法 Rex-Ray 是一个优秀的 Docker volume driver 安装和配置方法 Rex-Ray 以 standalone 进程的方式运行在 Docker 主机上 ...
- 自动车牌识别(ALPR)---https://blog.csdn.net/ELEVEN_ZOU/article/details/80893579
1.基本功能:从一张或者一系列的图片中提取车牌信息,比如车牌号码.车牌颜色等信息. 2.功能扩展:车型.车品牌.车牌类型等. 3.应用方向:电子交易系统(停车自动收费.收费站自动支付等).交通执法.交 ...
- 【转】Appium如何定位安卓APP元素
转载原文:https://www.jianshu.com/p/efe9dcf8bbaf 一.定位工具 在安装appium环境的时候我们已经安装了SDK,里面就自带有元素定位的工具,位置在.../sdk ...
- 102. Binary Tree Level Order Traversal二叉树层序遍历
网址:https://leetcode.com/problems/binary-tree-level-order-traversal/ 参考:https://www.cnblogs.com/grand ...
- TP5.0 PHPExcel 数据表格导出导入(原)
今天看的是PHPExcel这个扩展库,Comporse 下载不下来,最后只能自己去github里面手动下载,但有一个问题就是下载下来的PHPExcel没有命名空间,所以框架里面的use根本引入不进去, ...
- 泛在电力物联网建设大纲ppt
“三型两网”,国家电网在2019年提出的新战略目标.其中,“两网”分别代表着,“坚强智能电网”和“泛在电力物联网”.“坚强智能电网”的概念已经随着特高压的持续建设而被大家渐渐熟知,那么“泛在电力物联网 ...
- 有价值的IPFS博客
前一阵子研究IPFS,回头找资料有些找不到,再次汇总一下有价值的IPFS资料 戴嘉乐-IPFS的本质架构及应用:https://blog.csdn.net/u012357002/article/d ...
- react native练习
import React, { Component } from 'react' import { Platform, StyleSheet, Text, View,Image ,FlatList} ...