Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of ObjectPool?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the principle of ObjectPool". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the principle of ObjectPool"?

What is ObjectPool?

By parsing the UI source code of Unity, we found an Object cache pool ObjectPool.cs related to game performance optimization.

Why

In the game, we need to create and destroy 3D models, particle effects, List,Array, including objects such as bullets created by frequent release skills in combat, which will produce a lot of fragments in memory in the process of frequent destruction and creation. Without a unified manager to recycle, it is easy to cause stutters in the game. Therefore, ObjectPool is used for object pool management of Object objects in the game.

How?

Let's take a look at the source code and see how it works.

Using System.Collections.Generic

Using UnityEngine.Events

Namespace UnityEngine.UI

{

Internal class ObjectPool where T: new ()

{

Private readonly Stack m_Stack = new Stack ()

Private readonly UnityAction m_ActionOnGet

Private readonly UnityAction m_ActionOnRelease

Public int countAll {get; private set;}

Public int countActive {get {return countAll-countInactive;}}

Public int countInactive {get {return masked Stack.Count;}}

Public ObjectPool (UnityAction actionOnGet, UnityAction actionOnRelease)

{

M_ActionOnGet = actionOnGet

M_ActionOnRelease = actionOnRelease

}

Public T Get ()

{

T element

If (m_Stack.Count = = 0)

{

Element = new T ()

CountAll++

}

Else

{

Element = m_Stack.Pop ()

}

If (m_ActionOnGet! = null)

M_ActionOnGet (element)

Return element

}

Public void Release (T element)

{

If (m_Stack.Count > 0 & & ReferenceEquals (m_Stack.Peek (), element))

Debug.LogError ("Internal error. Trying to destroy object that is already released to pool.")

If (m_ActionOnRelease! = null)

M_ActionOnRelease (element)

M_Stack.Push (element)

}

}

}

ObjectPool uses a Stack heap (last-in-first-out (LIFO)) last-in-first-out structure. When we need to create (Get) a new Object, we first take the first one from the top of the Stack, and release it (Release) at the end of use, that is, put it back into the Stack heap. The principle is actually very simple. Of course, the Action for input and release is added, which can be used to pass in callback functions, which can be called when fetching and releasing, respectively.

At this point, I believe you have a deeper understanding of "what is the principle of ObjectPool". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report