12.10 오브젝트 풀 활용
C#/Study 2019. 12. 10. 16:47코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
public Button btn;
public GameObject bulletPrefab;
public objectPool objectPool;
// Start is called before the first frame update
void Start()
{
this.objectPool.PreLoad();
btn.onClick.AddListener(() => {
var bullet = this.objectPool.GetBullet();
var initPos = new Vector3(0, 0, 1);
bullet.onDestroy = () =>
{
objectPool.Release(bullet);
};
});
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed =3.5f;
public System.Action onDestroy;
private Coroutine coroutine;
public void Init(Vector3 initPos)
{
this.transform.position = initPos;
this.gameObject.SetActive(true);
this.Move();
}
public void Move()
{
coroutine = this.StartCoroutine(this.MoveImpl());
}
public IEnumerator MoveImpl()
{
while(true)
{
yield return null;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.LogFormat("OnTriggerEnter2D:{0}", collision);
StopCoroutine(coroutine);
this.onDestroy();
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
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)
{
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
|
'C# > Study' 카테고리의 다른 글
Sprite Atlas 사용하기 (0) | 2019.12.18 |
---|---|
오브젝트 풀링 (인스턴스로 접근) (0) | 2019.12.16 |
12.09 드래곤플라이트 2 (이펙트 추가) (0) | 2019.12.09 |
12.02 객체 충돌 (0) | 2019.12.02 |
struct 와 클래스의 차이 (0) | 2019.11.02 |