2D Animation, Mecanim, Background

C#/수업내용 2019. 5. 7. 18:32

Animation 만들고 Mecanim Parameters 이용하여 재생, Background, Tile 반복이동



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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestSunnyLand : MonoBehaviour
{
    public Button idleBtn;
    public Button runBtn;
    public Button jumpBtn;
    public GameObject player;
    public GameObject backGround;
    public GameObject tile;
    private Animator anim;
    private bool move;
 
    private void Awake()
    {
        this.anim = this.player.gameObject.GetComponent<Animator>();
    }
 
    public void Start()
    {
        this.StartGame();
    }
 
    public void StartGame()
    {
        this.idleBtn.onClick.AddListener(() =>
        {
            StartCoroutine(this.OnCompleteAnim("isIdle"));
        });
        this.runBtn.onClick.AddListener(() =>
        {
            StartCoroutine(this.OnCompleteAnim("isRun"));
        });
        this.jumpBtn.onClick.AddListener(() =>
        {
            StartCoroutine(this.OnCompleteAnim("isJump"));
            StartCoroutine(this.Jump());
        });
    }
 
    private IEnumerator MoveBackground()
    {
        while (this.move)
        {
            this.backGround.transform.position = new Vector3(Mathf.Repeat(Time.time, 15), this.backGround.transform.position.y, this.backGround.transform.position.z);
            yield return null;
        }
    }
    private IEnumerator MoveTile()
    {
        while (this.move)
        {
            if (this.tile.transform.position.x > -5f)
            {
                this.tile.transform.position = new Vector3(this.tile.transform.position.x - 1.5f * Time.deltaTime, this.tile.transform.position.y, this.tile.transform.position.z);
                yield return null;
            }
            else
            {
                this.tile.transform.position = new Vector3(000);
            }
        }
    }
 
    private IEnumerator OnCompleteAnim(string name)
    {
        var state = this.anim.GetCurrentAnimatorStateInfo(0);
        if(name == "isRun")
        {
            this.anim.SetBool("isIdle"false);
            this.anim.SetBool("isRun"true);
            this.move = true;
            StartCoroutine(this.MoveTile());
            StartCoroutine(this.MoveBackground());
            yield return new WaitForSeconds(this.anim.GetCurrentAnimatorStateInfo(0).length);
        }
        else if(name == "isIdle")
        {
            this.anim.SetBool("isRun"false);
            this.anim.SetBool("isIdle"true);
            this.move = false;
        }
        else if(name == "isJump")
        {
            if(state.IsName("isIdle"))
            {
                this.StopAllAnim();
                this.anim.SetBool(name, true);
                yield return new WaitForSeconds(this.anim.GetCurrentAnimatorStateInfo(0).length);
                this.anim.SetBool(name, false);
                this.anim.SetBool("isIdle"true);
            }
            else if(state.IsName("isRun"))
            {
                this.move = true;
                StartCoroutine(this.MoveTile());
                StartCoroutine(this.MoveBackground());
                this.anim.SetBool("isIdle"false);
                this.anim.SetBool(name, true);
                yield return new WaitForSeconds(this.anim.GetCurrentAnimatorStateInfo(0).length);
                this.anim.SetBool(name, false);
                this.anim.SetBool("isRun"true);
            }
        }
    }
 
    private void StopAllAnim()
    {
        var state = this.anim.runtimeAnimatorController.animationClips;
        foreach(var anim in state)
        {
            this.anim.SetBool(anim.name, false);
        }
    }
 
    private IEnumerator Jump()
    {
        bool isJump = true;
        while (true)
        {
            if (isJump)
            {
                this.player.transform.position = new Vector3(this.player.transform.position.x, this.player.transform.position.y + 5f * Time.deltaTime, 0);
                if (this.player.transform.position.y > 1)
                {
                    isJump = false;
                }
            }
            else if (!isJump)
            {
                this.player.transform.position = new Vector3(this.player.transform.position.x, this.player.transform.position.y - 5f * Time.deltaTime, 0);
                if (this.player.transform.position.y <= 0.1f)
                {
                    this.player.transform.position = new Vector3(this.player.transform.position.x, 00);
                    break;
                }
 
            }
            yield return null;
        }
    }
}
 
cs


: