카테고리 없음
2018.04.04 수업내용 (2D Toolkit)
일등하이
2018. 5. 15. 21:40
반응형
2D Toolkit 임포트
2D Sprite 임포트
Main.cs파일 생성
빈오브젝트 생성 (Main)
Main오브젝트에 Main.cs파일 Assign
헬로 월드
포토샵 열어 놓기
Game 뷰 해상도 설정 (1280*720)
Main Camera 의 Projection : Perspective -> Orthographic
Clipping Planes : Far -> 100
position : 0, 0, 0
Project 뷰 Tk2dResource폴더 생성
우클릭 -> Create -> tk2d -> SpriteCollection
SpriteCollection 프리팹 이름 -> CharacterSpriteCollection
CharacterSpriteCollection프리팹 선택
인스펙터에서 Open Editor선택
2d 리소스 선택, male -> idle -> 1번부터 16번까지
끌어다가 CharacterSpriteCollection Editor에서 놓음
1번부터 16번까지 선택된 상태에서 Anchor를 Custom으로 변경
변경후 Anchor포인트를 발끝중앙으로 마춤
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Main : MonoBehaviour { public GameObject hero; public GameObject target; private tk2dSpriteAnimator heroAnimator; private tk2dSpriteAnimator targetAnimator; private float heroMoveSpeed = 2f; private float heroAttackRange = 1.5f; private int targetHp = 10; private int heroDamage = 2; // Use this for initialization void Start () { this.heroAnimator = this.hero.GetComponent<tk2dSpriteAnimator>(); this.targetAnimator = this.target.GetComponent<tk2dSpriteAnimator>(); } // Update is called once per frame void Update () { } private void OnGUI() { if (GUILayout.Button("idle", GUILayout.Width(200), GUILayout.Height(100))) { this.heroAnimator.Play("hero_idle"); } else if (GUILayout.Button("attack", GUILayout.Width(200), GUILayout.Height(100))) { this.heroAnimator.Play("hero_attack"); } else if (GUILayout.Button("run", GUILayout.Width(200), GUILayout.Height(100))) { this.heroAnimator.Play("hero_run"); } else if (GUILayout.Button("die", GUILayout.Width(200), GUILayout.Height(100))) { this.heroAnimator.Play("hero_die"); } else if (GUILayout.Button("block", GUILayout.Width(200), GUILayout.Height(100))) { this.heroAnimator.Play("hero_block"); } else if (GUILayout.Button("Attack Target", GUILayout.Width(200), GUILayout.Height(100))) { //타겟으로 이동해 공격함. StartCoroutine(MoveAttack()); } } IEnumerator MoveAttack() { this.heroAnimator.Play("hero_run"); while(true) { var step = this.heroMoveSpeed * Time.deltaTime; this.hero.transform.position = Vector2.MoveTowards(this.hero.transform.position, this.target.transform.position, step); yield return null; var dis = Vector2.Distance(this.target.transform.position, this.hero.transform.position); if (dis < this.heroAttackRange) { break; } } //대상 공격 while (true) { yield return null; if (this.targetHp <= 0) { break; } this.heroAnimator.Play("hero_attack"); //전체 frame, 타격 frame, 시간 계산 var clip = this.heroAnimator.GetClipByName("hero_attack"); var totalLength = clip.frames.Length / clip.fps; var hitLength = 9 / clip.fps; yield return new WaitForSeconds(hitLength); this.targetHp -= this.heroDamage; //피격 애니 (사실 방어 애님이긴 한데 그냥 피격 애님으로 사용) this.targetAnimator.Play("hero_block"); yield return new WaitForSeconds(totalLength - hitLength); } //여기로 오면 대상이 죽었다는거임 this.targetAnimator.Play("hero_die"); this.heroAnimator.Play("hero_idle"); } } | cs |
다음 영상이 50MB가 넘어가서 부득이하게 유투브로 올림
반응형