Unity3D use object pooling ideas in game development

Category: U3D

1. In the glory of the King, a soldier every 30 seconds there will be a wave, the enemy will be destroyed after the emergence of a player or an enemy soldier, a game down, the soldier will be created many times, it will also be destroyed in the game this frequently create and destroy objects game is loss performance. In the game development process, we will create objects and destroying objects frequently, in order to improve game performance, we will use object pooling to think.

2. In the shooting game will frequently create and destroy bullets, which can be thought of using an object pool.

What is the object pool?

Pool, think of the pool, the pool is a collection of water. Therefore, the object pool is a collection of objects, we can follow the initial idea to understand the object pool. Using the principles of the word is, when we create a game object, we take the objects out of the pool, and is set to visible (Obj.seAactive (true)), again the object into the pool when the object to be destroyed and then hidden objects (Obj .setActive (false)), the time required for the next object is taken from the pool.

Then realization came to see.

First is that the effect to be achieved, any of our single screen a little, then you can launch a bullet (ball), and the use of bullets storage pool target, to achieve the goal to optimize performance;

First, create a script that do not inherit from Mono, and implemented using the Singleton pattern.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameObjectManage
{
    private List<GameObject> gameobjectlist;   // 保存对象的集合
    // 不再累述单例模式
    private GameObjectManage()
    {
        gameobjectlist=new List<GameObject>();
    }

    private static  GameObjectManage instance;

    public static GameObjectManage GetInstance
    {
        get
        {
            if (instance == null ) 
            { 
                instance = new new GameObjectManage (); 
            } 
            return instance; 
        } 
    } 
    // Create a method bullet 
    public the GameObject Instance_obj (the GameObject obj) 
    { 
        the GameObject Result; 
        IF (gameobjectlist.Count> 0 )     // if the set is not empty, remove objects from the collection 
        { 
            Result = gameobjectlist [ 0 ];    // get the first object set 
            result.SetActive ( to true );           // and display
            gameobjectlist.Remove (Result); // has been the object, the object is removed from the collection 
        }
         the else     // set is empty, the object can not be taken from the pool to give directly create 
        { 
            Result = MonoBehaviour.Instantiate (obj); 
        } 
        return Result; 
    } 
    // methods destruction bullet (actually hidden bullet) 
    public  void DestroryObj (the GameObject obj) 
    { 
        obj.transform.position = new new Vector3 ( 0 , 0 , 0 );     // this zero position of the bullet 
        obj .SetActive ( false );      //And the setting is not visible 
        gameobjectlist.Add (obj);   // added to the target cell, when the object is to create the next backup 
    } 

}

Then create a script to achieve the creation of a bullet, and then mount the camera on the script

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class contro : MonoBehaviour
{
    public GameObject obj;
    private Ray _ray;   // 射线
    void Update()
    {
        if (Input.GetMouseButtonDown(0))    
        {
            _ray = Camera.main.ScreenPointToRay(Input.mousePosition);//发射射线
           GameObject gb= GameObjectManage.GetInstance.Instance_obj(obj);
           obj.transform.position = transform.position;
           gb.GetComponent<Rigidbody>().velocity = _ray.direction *100;
        }
        
    }
}

To be able to achieve the bullet after 2 seconds to hide, we also need to create a script to mount the bullet preform, yes, we need to add on a rigid preform.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cube_contro : MonoBehaviour
{
    private void OnEnable()    // 不可换成Start哦
    {
        StartCoroutine(WaitDestrory());
    }

    IEnumerator WaitDestrory()    // 协程实现2秒钟后隐藏
    {
        yield return new WaitForSeconds(2f);
        GameObjectManage.GetInstance.DestroryObj(gameObject);
    }

}

By these scripts can be achieved bullet fired by object pooling effect slightly, very simple, but very use.

More thought, by analogy, the object pool ideas for your own game now!

 

Guess you like

Origin www.cnblogs.com/spiderljx/p/10966931.html