美文网首页
5.18对象池

5.18对象池

作者: 胤醚貔貅 | 来源:发表于2017-05-18 18:19 被阅读15次

usingUnityEngine;

usingSystem.Collections;

usingSystem.Collections.Generic;

public classObjectPoolScript{

private GameObject  poolObject;

private Dictionary<string,List<GameObject>>poolList=newDictionary<string,List<GameObject>>();

privateObjectPoolScript(){

}

private static ObjectPoolScript  instance;

public static ObjectPoolScript  Instance{

get{

if(instance==null){

instance=newObjectPoolScript();

}

return  instance;

}

}

//对象池管理方法

//从对象池中取出物体

public  GameObject  GetGameObject(GameObject  prefabs,

Vector3  position,Quaternion  rotation){

string tag=prefabs.tag;

//判断对象池中是否存在当前预设体所对应的游戏物体的列表,并且该列表中是否存在物体,如果存在就取出,否则实例化该预设体的一个对象

if(poolList.ContainsKey(tag)&&poolList[tag].Count>0){

Debug.Log("sdf");

poolObject=poolList [tag] [0];

poolList[tag].Remove(poolObject);

poolObject.transform.position=position;

poolObject.transform.rotation=rotation;

poolObject.SetActive(true);

}else{

if(!poolList.ContainsKey(tag)){

poolList.Add(tag,newList<GameObject>( ));

}

poolObject=GameObject.Instantiate(prefabs,position,rotation)asGameObject;

}

return  poolObject;

}

public  void   RecycleObject(GameObject  obj){//对象池回收物体

if(poolList.ContainsKey(obj.tag)){

poolList [obj.tag].Add(obj);

obj.SetActive(false);

}

}

//销毁对象池中的物体

//public void RecycleObject(GameObject  obj,bool   isDestory,float  delayTime){

//

//if(poolList.ContainsKey(obj.tag)){

//poolList[obj.tag].Add(obj);

//obj.SetActive(false);

//}

//}

}


相关文章

  • 5.18对象池

    usingUnityEngine; usingSystem.Collections; usingSystem.Co...

  • Unity 类对象池资源池对象池

    类对象池 包含创建对象池,取对象池中的内容,回收。 对象管理类 因为使用加载AB包的时候可能会频繁创建类,但是ne...

  • Laya_2D示例项目使用对象池预制体的创建、位置设置和添加到父

    //使用对象池创建盒子 1.对象池的使用 对象池创建箱子 let box = Laya.Pool.getI...

  • Unity--简单的对象池

    简单的对象池分三步走: 建立对象池 拿到对象 回收对象 Test为对象池,Obj为自动回收的物体 Test.cs ...

  • 对象池

    一、对象池概述: 对于那些实例化开销比较大,并且生命周期比较短的对象,我们可以通过池就行管理。所谓池,就相当于一个...

  • 对象池

  • 对象池

    概要 看到现在,Netty之设计精妙,令人感叹。优化至极,令人发指。在高并发场景下,对象的分配的损耗是很大的,特别...

  • 对象池

    讲在前面 为什么要有对象池?我们需要一种类型的实例时候往往回去new一个这样会造成gc问题对象池根本奥义就在于一个...

  • 对象池

    UGUI 中使用的对象池 在 C:\Files\Unity\Projects\UGUITest\PackageSo...

  • 对象池

    java对象池化技术https://blog.csdn.net/tiane5hao/article/details...

网友评论

      本文标题:5.18对象池

      本文链接:https://www.haomeiwen.com/subject/mrzsxxtx.html