오브젝트 풀링 (인스턴스로 접근)
C#/Study 2019. 12. 16. 10:06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class objectPool : MonoBehaviour
{
private List<Bullet> bullets;
private GameObject bulletPrefab;
public void Init(GameObject bulletprefab) //Init을 해줘야한다!
{
this.bullets = new List<Bullet>();
this.bulletPrefab = bulletprefab;
}
public void PreLoad()//만들기
{
for(int i =0; i<10; i++)
{
var bulletGo = Instantiate<GameObject>(this.bulletPrefab);
bulletGo.transform.SetParent(this.transform, false);
bulletGo.transform.localPosition = Vector3.zero;
bulletGo.SetActive(false);
var bullet = bulletGo.GetComponent<Bullet>();
}
}
public Bullet GetBullet() //꺼내기
{
Bullet bullet = null;
{
bullet = this.bullets[0];
this.bullets.RemoveAt(0);
bullet.gameObject.SetActive(true);
bullet.transform.SetParent(null);
}
else
{
//재사용할 총알 없음
bullet = Instantiate<GameObject>(this.bulletPrefab).GetComponent<Bullet>();
}
return bullet;
}
public void Release(Bullet bullet) // 넣기
{
bullet.transform.SetParent(this.transform, false);
bullet.transform.localPosition = Vector3.zero;
bullet.gameObject.SetActive(false);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
App 클래스에서 퍼블릭 멤버 변수로 선언하여 사용한다.
'C# > Study' 카테고리의 다른 글
Sprite Atlas 사용하기 (0) | 2019.12.18 |
---|---|
12.10 오브젝트 풀 활용 (0) | 2019.12.10 |
12.09 드래곤플라이트 2 (이펙트 추가) (0) | 2019.12.09 |
12.02 객체 충돌 (0) | 2019.12.02 |
struct 와 클래스의 차이 (0) | 2019.11.02 |