(3,4)ShootAndObjectPooling

Unity/수업내용 2019. 5. 1. 19:05



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
 
public class TestPlayerControll : MonoBehaviour
{
    public Transform bulletSpwanPos;
    public float fireLate;
 
    private float nexFireTime;
 
    public GameObject objPool;
    private GameObject boltPrePab;
    private List<GameObject> bolts;
 
    private void Awake()
    {
        this.bolts = new List<GameObject>();
        this.boltPrePab = Resources.Load<GameObject>("Done_Bolt");//총알 리소스 불러오기
 
        for (int i = 0; i < 10; i++)
        {
            var bolt = Instantiate(this.boltPrePab, this.objPool.transform);
            bolt.SetActive(false);
            bolt.name = $"Done_Bolt ({i})";
            bolts.Add(bolt);
        }
    }
 
    private void Start()
    {
        this.OnClickAttackFire();
    }
 
    private void OnClickAttackFire()
    {
        StartCoroutine(this.OnClickAttackFireImpl());
    }
 
    private IEnumerator OnClickAttackFireImpl()
    {
        while (true)
        {
            if (Input.GetMouseButton(0&& Time.time > this.nexFireTime)
            {
                this.nexFireTime = Time.time + fireLate;
                //오브젝트 풀에 들어있는 Done_Bolt 꺼내기
                this.Shoot();
            }
            yield return null;
        }
    }
 
    private void Shoot()
    {
        StartCoroutine(this.ShootImpl());
    }
 
    private IEnumerator ShootImpl()
    {
        for (int i = 0; i < bolts.Count; i++)
        {
            if (bolts[i].activeSelf == false)
            {
                var usingBolt = bolts[i];
                usingBolt.transform.position = this.transform.position;
                usingBolt.SetActive(true);
                yield return new WaitForSeconds(1);
                usingBolt.SetActive(false);
                break;
            }
        }
        yield return null;
    }
}
 
cs


'Unity > 수업내용' 카테고리의 다른 글

<Test_SunnyLand>짬뿌2  (0) 2019.05.08
<Test_SunnyLand>짬뿌  (0) 2019.05.07
(2)MoveAnd//한번 더 확인  (0) 2019.05.01
(1)BGLoop  (0) 2019.05.01
LayerMask  (0) 2019.05.01
: