EffectTest

Unity 2019. 4. 24. 00:11

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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class EffectTest : MonoBehaviour
{
    [SerializeField]
    private Button btnAttack;
    [SerializeField]
    private GameObject hero;
    [SerializeField]
    private GameObject monster;
 
    public GameObject objPool;
    public Material hitMaterial;
    private Material originMaterial;
 
    private Animator heroAnim;
    private Animator monsterAnim;
    private bool isAttacking;
    private Coroutine heroActionRoutine;
    private GameObject fx;
 
    private System.Action OnDamageMonster;
 
    private void Awake()
    {
        //FX로드
        var loadFx = Resources.Load<GameObject>("Prefabs/Test/SwordHitRed");
 
        //인스턴스화
        this.fx = Instantiate<GameObject>(loadFx);
        fx.transform.SetParent(this.objPool.transform);
        fx.name = "SwordHitRed";
        this.fx.SetActive(false);
    }
 
    private void Start()
    {
        //버튼 이벤트 등록
        this.btnAttack.onClick.AddListener(() =>
        {
            if (this.isAttacking == false)
            {
                //몬스터가 공격을 받을 시
                this.OnDamageMonster = () =>
                {
                    StartCoroutine(this.MonsterGetDamage());
                };
                Debug.Log("어택!");
                //영웅이 공격! attack_sword_01 실행
                this.heroActionRoutine = StartCoroutine(HeroPlayAttackAnim());
            }
            else
            {
                Debug.Log("공격불가");
            }
            //attack_sword_01 애니메이션 실행 후  idle@loop실행
        });
 
        //애니메이션 컴포넌트 가져옴
        this.heroAnim = this.hero.GetComponent<Animator>();
        this.monsterAnim = this.monster.GetComponent<Animator>();
 
        this.originMaterial = this.monster.transform.Find("StoneMonster").GetComponent<SkinnedMeshRenderer>().material;
    }
 
    private IEnumerator MonsterGetDamage()
    {
        this.monsterAnim.Play("Anim_Damage");
 
        var length = this.GetAnimLength("Anim_Damage");
 
        StartCoroutine(this.MonsterSwitchMaterial());
 
        yield return new WaitForSeconds(length);
 
        this.monsterAnim.Play("Anim_Idle");
    }
 
    private IEnumerator MonsterSwitchMaterial()
    {
        this.monster.transform.Find("StoneMonster").GetComponent<SkinnedMeshRenderer>().material = this.hitMaterial;
        yield return new WaitForSeconds(0.1f);
 
        this.monster.transform.Find("StoneMonster").GetComponent<SkinnedMeshRenderer>().material = this.originMaterial;
    }
 
    private IEnumerator HeroPlayAttackAnim()
    {
        this.isAttacking = true;
        this.heroAnim.Play("attack_sword_01");
 
        var length = this.GetAnimLength("attack_sword_01");
 
        yield return new WaitForSeconds(0.25f);
 
        Debug.Log("타격!!");
        this.AddFx("SwordHitRed"this.monster.transform.Find("fsPoint").position);
        this.OnDamageMonster();
        yield return new WaitForSeconds(length - 0.25f);
 
        this.isAttacking = false;
 
        Debug.Log("공격 완료!!!");
        this.heroAnim.Play("idle@loop");
    }
 
    private void AddFx(string effectName, Vector3 fxPos)
    {
        //만들어진 프리팹 인스턴스의 위치를 fxPos로 설정
        this.fx.transform.SetParent(this.monster.transform.Find("HitFxPoint"));
        this.fx.transform.position = fxPos;
        this.fx.SetActive(true);
        var particle = this.fx.GetComponent<ParticleSystem>();
 
        this.StartCoroutine(this.PlayFx(particle));
    }
 
    private IEnumerator PlayFx(ParticleSystem particle)
    {
        particle.Play();
        yield return new WaitForSeconds(particle.main.duration);
        this.fx.transform.SetParent(this.objPool.transform);
        this.fx.SetActive(false);
    }
 
    private float GetAnimLength(string animName)
    {
        float time = 0;
        RuntimeAnimatorController ac1 = heroAnim.runtimeAnimatorController;
        RuntimeAnimatorController ac2 = monsterAnim.runtimeAnimatorController;
 
        foreach (var data in ac1.animationClips)
        {
            Debug.Log(data.name);
        }
        for (int i = 0; i < ac1.animationClips.Length; i++)
        {
            if (ac1.animationClips[i].name == animName)
            {
                time = ac1.animationClips[i].length;
            }
        }
 
        if (time == 0)
        {
            foreach (var data in ac2.animationClips)
            {
                Debug.Log(data.name);
            }
            for (int i = 0; i < ac2.animationClips.Length; i++)
            {
                if (ac2.animationClips[i].name == animName)
                {
                    time = ac2.animationClips[i].length;
                }
            }
        }
        return time;
    }
}
 
cs


'Unity' 카테고리의 다른 글

개인정보 처리방침  (0) 2019.06.13
: