zoukankan      html  css  js  c++  java
  • 对象池----unity中应用

    对象池应用在unity中能减少资源消耗,节省内存空间具体原理不再赘述.

    以下是他的操作步骤:(注意:对象池中应用到了栈或对队列!)

    1).先建立一个(怪物)物体   mMonster;

    2).再建立一个对象池  private Stack<GameObject> monsterPool

    :先进后出,后进先出,水杯结构   常用方法:  .peek()---->获取栈顶元素

                                                                          .pop()------->出栈,弹栈

                                                                          .push()------>压栈,进栈

    队列:先进先出,后进后出,水管结构 常用方法: .peek()------------->获取队头元素

                                                                          .Dequeue()-------->移除队头

                                                                          .Enqueue()--------->移除队头

    3).再建立一个池子,用来存放激活过的(怪物)物体,作用是用来判断场景中有多少个激活的物体,再让最近激活(栈)(或最开始激活(队列))的对象给失活;

    4).在Start/Awake函数中将两个对象池赋初值(new一下);

    5).创建一个返回值为(怪物)物体的方法,里面进行判断对象池中是否存在所需物体,如果对象池为空,就实例化一个(先不将其加入对象池),否则就直接拿出对象池中所需的物体

    6).创建一个方法,当该(怪物)物体失活(SetActive(Fasle)),将其加入到对象池所在物体的旗下( monster.transform.SetParent(transform);),并将其加入到对象池中,以便下次再用!

    代码参上:

     

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class MySimpleFactory : MonoBehaviour {
     6     public GameObject mMonster;
     7     private Stack<GameObject> monsterPool;
     8     private Stack<GameObject> SetActiveMonsterPool;
     9     
    10     void Start () 
    11     {
    12         monsterPool = new Stack<GameObject>();
    13         SetActiveMonsterPool = new Stack<GameObject>();     
    14     }        
    15     void Update ()
    16     {
    17         if (Input.GetMouseButtonDown(0))
    18         {
    19             GameObject tempMonster = TakeMonster();
    20             tempMonster.transform.position = Vector3.zero;
    21             SetActiveMonsterPool.Push(tempMonster);         
    22         }
    23         if (Input.GetMouseButtonDown(1))
    24         {
    25             PushThisGameObjectToMonsterPool(SetActiveMonsterPool.Pop());
    26         }
    27     }
    28 
    29     private GameObject TakeMonster() 
    30     {
    31         GameObject Monster = null;
    32         if (monsterPool.Count > 0)
    33         {
    34             Monster = monsterPool.Pop();
    35             Monster.SetActive(true);
    36         }
    37         else 
    38         {
    39             Monster = Instantiate(mMonster);
    40         }
    41         return Monster;
    42     }
    43     private void PushThisGameObjectToMonsterPool(GameObject xt) 
    44     {
    45         xt.transform.SetParent(gameObject.transform);
    46         xt.SetActive(false);
    47         monsterPool.Push(xt);
    48     }
    49 }

     

     

     

  • 相关阅读:
    @RequestParam注解使用:Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
    cglib动态代理导致注解丢失问题及如何修改注解允许被继承
    springboot Autowired BeanNotOfRequiredTypeException
    git根据用户过滤提交记录
    不同包下,相同数据结构的两个类进行转换
    How to use Jackson to deserialise an array of objects
    jooq实践
    java如何寻找main函数对应的类
    Python--matplotlib
    Python 和 Scikit-Learn
  • 原文地址:https://www.cnblogs.com/Future-Better/p/9964903.html
Copyright © 2011-2022 走看看