주녘공부일지

[ShootingGame] 8. 최적화의 기본, 오브젝트 풀링 본문

GameEngine/Unity - Lecture

[ShootingGame] 8. 최적화의 기본, 오브젝트 풀링

주녘 2022. 2. 1. 18:00
728x90

Scripts.zip
0.01MB

 

 

1. 오브젝트 풀링

class ObejctManager

오브젝트 풀링 : 미리 생성해둔 풀에서 활성화 / 비활성화로 사용

https://godgjwnsgur7.tistory.com/21

 

[Unity] Object Pooling 기법

Object Pooling : 오브젝트를 재활용하기 위한 디자인 패턴 ( 최적화 ) 빈번히 생성, 파괴되는 오브젝트들을 풀에 모아서 미리 생성해놓고 활성화 / 비활성화로 관리하며 재사용 - 런타임 중에 수십개

godgjwnsgur7.tistory.com

 

2. 풀 사용

public ObejctManager obejectManager;

Instantiate() - 오브젝트 풀링으로 로직 변경

- Instantiate()는 MakeObj() 호출로 변경

  ( transform.position 로직 작성 ) // 생성 시에 위치를 주지 않기 때문

 

- Destory() 는 SetActive(false) 로 변경

 

+ class Enemy()는 프리팹이기 때문에 게임 매니저가 줌

 

class GameManager

 

+ ObjectManager를 제외한 모든 곳에 적용

 

 

3. 로직 정리

 

3-1. 재사용하는 오브젝트 관리

좌측 로직에서 우측 로직으로 변경

 

- OnEnable()을 활용하여 활성화 될때마다 다시 초기화

https://godgjwnsgur7.tistory.com/6

 

[Unity] Start()와 Awake() 그리고, OnEnable()

1. Awake() - 오브젝트가 비활성화되어 있어도 호출가능 ex) 게임의 상태, 변수를 초기화 2. OnEnable() - 오브젝트가 활성화 될 때마다 호출 (여러번 호출 가능) 3. Start() - 오브젝트가 활성화 된 경우에

godgjwnsgur7.tistory.com

 

3-2. FInd 함수 변경

- 오브젝트를 직접 찾는 Find 계열 함수도 성능 부하를 유발할 수 있기 때문에 GetPool()로 변경

 

class Player - Boom() 기존 로직
class Player - Boom() 변경 로직

- 코드는 길어졌지만, 메모리 상의 최적화는 이루어짐

 

MakeObj(), GetPool()

class ObjectManager

+ 활성화 오브젝트를 따로 관리하는 배열도 있다면 더욱 좋음

 

 

https://github.com/godgjwnsgur7/ShootingGame

 

GitHub - godgjwnsgur7/ShootingGame

Contribute to godgjwnsgur7/ShootingGame development by creating an account on GitHub.

github.com

 

강의 출처

https://www.youtube.com/watch?v=x-5eHMRSgeg&list=PLO-mt5Iu5TeYtWvM9eN-xnwRbyUAMWd3b&index=8 

728x90