상세 컨텐츠

본문 제목

Unity Study_006 Effect and WeaponChange

C#/수업내용

by McRobbin 2020. 5. 13. 18:33

본문

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
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
 
public class Hero : MonoBehaviour
{
    public enum eState
    {
        Idle,
        Attack
    }
 
    public UnityAction onAttackEffect;
    public eState state;
    private GameObject dummyRHand;
 
 
    private Animation anim;
    private float time;
    private string attackName;
 
    private bool isEffect;
 
    private void Awake()
    {
        this.anim = this.gameObject.transform.GetChild(0).GetComponent<Animation>();
        this.dummyRHand = GameObject.Find("DummyRHand");
    }
 
    void Start()
    {
        
    }
 
 
    public void Attack(string attackName)
    {
        this.attackName = attackName;
        this.state = eState.Attack;
        this.anim.Play(this.attackName);
        this.time = 0.0f;
        this.isEffect = false;
    }
 
    void Update()
    {
        this.time += Time.deltaTime;
        if (this.state == eState.Attack)
        {
            if(!this.isEffect)
            {
                float effectTime = this.attackName == "attack_sword_01" ? 0.3f : 0.6f;
 
                if (this.time >= effectTime)
                {
                    this.onAttackEffect();
                    this.isEffect = true;
                }
                
            }
 
            if (this.time > this.anim[attackName].length)
            {
                this.state = eState.Idle;
            }
        }
        
        else if (this.state == eState.Idle)
        {
            this.anim.Play("idle@loop");
        }
 
    }
}
 
 
cs

Hero.cs

eState enum타입을 정의하고 업데이트 돌렸습니다.

 

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
 
public class UIStudio : MonoBehaviour
{
    public enum eBtnTypes
    {
        Axe,
        Hammer,
        Machete
    }
 
    public Button[] btns;
    public Button[] attackBtn;
    public GameObject effect;
    public Image weaponIconImage;
    public Sprite[] sprites;
    public GameObject[] weapons;
    public Hero hero;
    public GameObject dummyHand;
    public float speed;
 
 
    private int capture;
 
    private void Awake()
    {
        ObjectPool.GetInstance().LoadAssets();
    }
 
    void Start()
    {
        for(int i = 0; i < this.btns.Length; i++)
        {
            int captured = i;
            this.btns[captured].onClick.AddListener(() =>
            {
                weaponIconImage.sprite = sprites[captured];
                foreach (var weapon in weapons)
                {
                    weapon.transform.SetParent(nullfalse);
                    weapon.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
                    weapon.SetActive(false);
                }
                weapons[captured].SetActive(true);
                this.capture = captured;
                weapons[capture].transform.SetParent(this.dummyHand.transform, false);
                weapons[capture].transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
            });
 
            this.attackBtn[captured].onClick.AddListener(() =>
            {
                hero.onAttackEffect += this.AttackEffect;
                string attackName = "attack_sword_0" + (captured + 1);
                hero.Attack(attackName);
            });
        }
 
    }
 
    private void AttackEffect()
    {
        this.effect.gameObject.SetActive(false);
        this.effect.gameObject.SetActive(true);
    }
 
    void Update()
    {
        //this.weapons[capture].transform.Rotate(Vector3.up * Time.deltaTime * speed);
    }
 
 
cs

UIStudio.cs 입니다.

 

공격시 이펙트가 터지도록 hero에 onAttackEffect UnityAction을 멤버로 놨습니다.

공격 호출시 액션에 이펙트가 터지는 메서드를 등록하고 해당 시점에 실행하도록 했습니다.

 

이펙트, 무기 변경은 SetActive로 껐다 켜도록 했습니다.

 

 

 

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

Unity Study_013-1 로그인 구현  (0) 2020.05.22
Unity Study_007 무기변경, 닭 잡기.  (0) 2020.05.14
Study_004 - UnityActionExmaple  (0) 2020.05.11
Unity Study_003 5Run  (0) 2020.05.08
Study_019_2 일일 출석 보상  (0) 2020.04.29

관련글 더보기