BCA/4. Unity

24일 : dotween 이용, 2D슈팅게임

Beabletoet 2018. 3. 21. 19:33

에셋스토어에서 dotween 무료버전 다운 후, tools-demigiant-dotween utility panel을 누른 후 setup을 누른다.


order in layer가 크면 더 앞으로 나옴


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
//StoneScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class StoneScript : MonoBehaviour
{
 
    public Vector3 mDir;
    public float speed;
    public float damTime;
    public int hp;
    public int dam;
    // Use this for initialization
    void Start()
    {
        speed = Random.Range(0.5f, 2.0f); 
        damTime = 1.0f;
        dam = 0;
        mDir = new Vector3(0-10);
        mDir.Normalize();
    }
 
    // Update is called once per frame
    void Update()
    {
        transform.Translate(mDir * Time.deltaTime * speed);
    }
 
    //void OnTriggerEnter2D(Collider2D other)
    //{
    //    if (other.gameObject.name == "playerShip(Clone)")
    //    {
            
    //    }
    //}
}
 
cs


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//PlayerScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerScript : MonoBehaviour
{
    public GameObject GameManager = null;
    public GameObject EffectManager = null;
    public GameObject PlayerLaser = null;
    public float shootTime = 0;
    public bool boolShoot = false;
    public GameObject damEffect = null;
    public GameObject bombEffect = null;
    public GameObject nowLaser = null;
    // Use this for initialization
    void Start()
    {
        GameManager = GameObject.FindWithTag("GameManager");
        EffectManager = GameObject.FindWithTag("EffectManager");
 
        Vector3 laserPos = transform.position + new Vector3(06.0f, 0);
        nowLaser = Instantiate(PlayerLaser, laserPos, Quaternion.identity);
        nowLaser.transform.parent = transform;
        nowLaser.gameObject.SetActive(false);
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            if (transform.position.x > -10)
                transform.Translate(-Time.deltaTime * 10.0f, 00);
        if (Input.GetKey(KeyCode.RightArrow))
            if (transform.position.x < 10)
                transform.Translate(Time.deltaTime * 10.0f, 00);
        if (Input.GetKey(KeyCode.UpArrow))
            if (transform.position.y < 6.5)
                transform.Translate(0, Time.deltaTime * 10.0f, 0);
        if (Input.GetKey(KeyCode.DownArrow))
            if (transform.position.y > -4.5)
                transform.Translate(0-Time.deltaTime * 10.0f, 0);
 
        if (Input.GetKey(KeyCode.Space))
        {
            
            //hit.collider.gameObject.name.Contains("BigIron")
            Vector3 laserPos = transform.position + new Vector3(06.0f, 0);
            if (!boolShoot)
            {
                //GameObject nowLaser = Instantiate(PlayerLaser, laserPos, Quaternion.identity);
                //nowLaser.transform.parent = transform;
                nowLaser.transform.position = laserPos;
                boolShoot = true;
                nowLaser.gameObject.SetActive(true);
            }
            //RaycastHit[] objectHit = Physics.RaycastAll(transform.position, Vector3.up);
            RaycastHit2D[] objectHit = Physics2D.RaycastAll(transform.position, Vector2.up);
            foreach (RaycastHit2D hit in objectHit)
            {
                if (hit.collider.gameObject != null)
                {
                    StoneScript hitStone = hit.collider.gameObject.GetComponent<StoneScript>();
                    if(hitStone)
                    {
                        if (hitStone.dam != hitStone.hp)
                        {
                            hitStone.damTime += Time.deltaTime;
                            if (hitStone.damTime >= 1.0f)
                            {
                                hitStone.damTime -= 1.0f;
                                ++hitStone.dam;
                                GameManager.GetComponent<GameManagerScript>().score += hitStone.dam * 100;
                                GameObject ef = Instantiate(damEffect, hit.collider.gameObject.transform.position + new Vector3(00-1), Quaternion.identity);
                                ef.transform.parent = EffectManager.transform;
                            }
                        }
                        else
                        {
                            Destroy(hit.collider.gameObject);
                            GameManager.GetComponent<GameManagerScript>().score += (Random.Range(06* 100);
                            GameObject ef = Instantiate(bombEffect, hit.collider.gameObject.transform.position, Quaternion.identity);
                            ef.transform.parent = EffectManager.transform;
                        }
                        GameManager.GetComponent<GameManagerScript>().ChangeScore();
                    }                    
                }
            }
        }
        else
        {
            if (boolShoot)
            {
                nowLaser.gameObject.SetActive(false);
                boolShoot = false;
                //foreach (Transform c in this.transform)
                    //GameObject.Destroy(c.gameObject);
            }
        }
    }
}
 
cs


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
//PlayerLaserScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerLaserScript : MonoBehaviour
{
    public GameObject player = null;
    public int myLaserCntNum;
    // Use this for initialization
    void Start ()
    {
        player = this.transform.parent.gameObject;
    }
    
    // Update is called once per frame
    void Update ()
    {
        if (transform.position.y >= 7.0f)
        {
            //적을 안죽인다.
        }
        else
        {
            //적을 죽인다.
        }
    }
}
 
cs


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//라이프 값 바꿔주기, instantiate destroy 줄이기
//플레이어랑 돌 충돌시 플레이어 라이프 깎기
//이펙트도 instantiate 줄이고, 
//레이저도 enable uneanable하기-완료
//돌을 담을 자료구조. 돌도 처음 1번 만들고 더 이상 만들지 않고 자료구조 이용해서 계속 불러쓰기.
//하이스코어 시스템 (갱신 시 보상 +)
//상점 + 보상 금화
//돌의 라이프를 좀 더 다양하게. 상점을 이용해 데미지 레벨 업 도입.
//소리 넣기
 
 
//GameManagerScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
 
public class GameManagerScript : MonoBehaviour
{
    public GameObject background = null;
    public GameObject backGroundManager = null;
 
    public GameObject MenuGameStartButton = null;
    public GameObject MenuGameExitButton = null;
 
    public GameObject playerShip = null;
    public GameObject player = null;
 
    public GameObject StoneManager = null;
 
    //public Image[] lifeimage = null;
 
    public float makeStoneCoolTime = 5.0f;
    public bool boolInGame = false;
    public bool firstScore = true;
    public bool playerFullHp = true;
    public int score;
    public float makeStoneTime;
    public int stageLv;
    public int playerLife;
    public GameObject[] stones = new GameObject[8];
    //public GameObject[] num = new GameObject[10];
    //public GameObject[] nowScoreSprite = new GameObject[6];
 
    //public Sprite PlayerLifeSprite = null;
    public Image[] NowPlayerLifeSpirte = null;
 
    public Image[] num = null;
    public Sprite[] numSprites = null;
    // Use this for initialization
    void Start()
    {
        MakeBackground();
        MakeMenuLayer();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (boolInGame)
        {
            makeStoneTime += Time.deltaTime;
            if (makeStoneTime >= makeStoneCoolTime)
            {
                int stoneType;
                if (stageLv < 5)
                    stoneType = Random.Range(04);
                else
                {
                    int now = Random.Range(05);
                    if (now == 4)
                        stoneType = Random.Range(48);
                    else
                        stoneType = Random.Range(04);
                }
                float stoneXPos = Random.Range(-10.0f, 10.0f);
                GameObject thisStone = Instantiate(stones[stoneType], new Vector3(stoneXPos, 70), Quaternion.identity);
                thisStone.transform.parent = StoneManager.transform;
                makeStoneTime -= makeStoneCoolTime;
            }
        }
    }
 
    void MakeBackground()
    {
        for (int i = 0; i < 9++i)
            for (int j = 0; j < 7++j)
            {
                GameObject nowBG = GameObject.Instantiate(background, new Vector3((i * 2.5f) - 10.0f, 6.0f - (j * 2.5f), 0), Quaternion.identity);
                nowBG.transform.parent = backGroundManager.transform;
            }
    }
 
    void MakeMenuLayer()
    {
        //init
        makeStoneCoolTime = 5.0f;
        score = 0;
        makeStoneTime = makeStoneCoolTime - 1.0f;
        firstScore = true;
        stageLv = 1;
        playerLife = 3;
        playerFullHp = true;
        //----------
        boolInGame = false;
        MenuGameStartButton.transform.position = new Vector3(020);
        MenuGameExitButton.transform.position = new Vector3(0-20);
 
        for (int i = 0; i < 6++i)
        {
            num[i].gameObject.SetActive(false);
            if (i < 3)
                NowPlayerLifeSpirte[i].gameObject.SetActive(false);
        }
 
    }
 
    public void MakeGameLayer()
    {
        boolInGame = true;
        MenuGameStartButton.transform.position = new Vector3(50020);
        MenuGameExitButton.transform.position = new Vector3(500-20);
        MakePlayer();
        ChangeScore();
        SeeLife();
    }
 
    void MakePlayer()
    {
        player = GameObject.Instantiate(playerShip, new Vector3(0-40), Quaternion.identity);
        player.transform.parent = this.transform;
    }
 
    public void ChangeScore()
    {
        if(!num[0].gameObject.activeInHierarchy)
            for(int i = 0; i < 6++i)
                num[i].gameObject.SetActive(true);
        int[] nowScore = new int[6];
        for (int i = 100000, j = score, k = 0; i > 0; i /= 10++k)
        {
            nowScore[k] = j / i;
            j %= i;
        }
        for (int i = 0; i < 6++i)
            num[i].sprite = numSprites[nowScore[i]];
 
        if (score / 3000 + 1 > stageLv)
        {
            if (makeStoneCoolTime > 0.5f)
            {
                makeStoneCoolTime -= 0.5f;
                ++stageLv;
            }
        }
    }
    public void SeeLife()
    {
        if (playerLife == 3)
        {
            for (int i = 0; i < 3++i)
                NowPlayerLifeSpirte[i].gameObject.SetActive(true);
        }
        else if (playerLife > -1 && playerLife < 3)
            NowPlayerLifeSpirte[playerLife].gameObject.SetActive(false);
    }
}
 
cs


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
//ButtonSizeUpScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
public class ButtonSizeUpScript : MonoBehaviour
{
    public GameObject GameManager = null;
    public int buttonType; //1 = start, 2 = exit
    void OnMouseEnter()
    {
        gameObject.transform.DOScale(new Vector3(666), 0.2f);
    }
 
    void OnMouseExit()
    {
        gameObject.transform.DOScale(new Vector3(555), 0.2f);
    }
 
    void OnMouseDown()
    {
        gameObject.transform.DOScale(new Vector3(5.5f, 5.5f, 5.5f), 0.05f);
    }
 
    void OnMouseUp()
    {
        gameObject.transform.DOScale(new Vector3(666), 0.05f);
        if (buttonType == 1)
            GameManager.GetComponent<GameManagerScript>().MakeGameLayer();
        else
            quit();
    }
 
    void quit()
    {
        #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
        #elif UNITY_WEBPLAYER
            Application.OpenURL("http://google.com");
        #else
            Application.Quit();
        #endif
    }
}
 
cs


===하다가 말았음. 내일 이어서.