Damage Text 출력하기
C#/수업내용 2019. 4. 25. 18:17WorldToScreenPoint를 이용하여 캐릭터의 상단에 데미지 텍스트를 출력함
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 | using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestHudText : MonoBehaviour { public Button btn; public Button btn2; public GameObject pivot; public Transform hudPoint; public Text txt0; //worldspace point public Text txt1; //viewport point public Text txt2; //spaceport point private Canvas canvas; private void Awake() { this.canvas = FindObjectOfType<Canvas>(); } private void Start() { this.btn.onClick.AddListener(() => { //프리팹 로드 var hudPre = Resources.Load<HUDDamageText>("Prefabs/Test/HUDDamageText"); //인스탄트 var screenPoint = Camera.main.WorldToScreenPoint(this.hudPoint.position); var targetLocalPos = new Vector3(screenPoint.x - Screen.width / 2, screenPoint.y - Screen.height / 2, screenPoint.z); this.pivot.GetComponent<RectTransform>().anchoredPosition = targetLocalPos; var hud = Instantiate(hudPre); //인잇 메서드 호출(텍스트랑 컬러) Color color = new Color(1, 0, 0); hud.Init("456", color); //부모 Canvas로 설정 hud.transform.SetParent(this.pivot.transform, false); //초기화 hud.transform.localPosition = Vector3.zero; //타겟 포지션 Debug.LogFormat("{0},{1},{2}", targetLocalPos.x, targetLocalPos.y, targetLocalPos.z); Debug.LogFormat("{0},{1},{2}", screenPoint.x, screenPoint.y, screenPoint.z); var target = new Vector3(0, 100, 0); DOTween.ToAlpha(() => hud.txt.color, x => hud.txt.color = x, 0, 0.5f); hud.transform.DOScale(new Vector3(2, 2, 2), 0.3f).OnComplete(() => { hud.transform.DOScale(new Vector3(1, 1, 1), 0.2f); }); hud.transform.DOLocalMove(target, 1.5f).SetEase(Ease.OutBack).OnComplete(() => { Destroy(hud.gameObject); }); }); this.btn2.onClick.AddListener(() => { Debug.LogFormat("hudPoint world coordinate : {0}", this.hudPoint.position); this.txt0.text = this.hudPoint.position.ToString(); var viewPortPoint = Camera.main.WorldToViewportPoint(this.hudPoint.position); this.txt1.text = viewPortPoint.ToString(); var screenPoint = Camera.main.WorldToScreenPoint(this.hudPoint.position); this.txt2.text = screenPoint.ToString(); }); } } | cs |
'C# > 수업내용' 카테고리의 다른 글
2D Animation, Mecanim, Background (0) | 2019.05.07 |
---|---|
Generic, Dictionary, Json을 이용한 데이터 처리 (0) | 2019.04.04 |
ItemData구현 (0) | 2019.04.04 |
Dictionary를 이용한 Inventory구현 및 Json사용 (0) | 2019.04.03 |
Inventory구현 (창고이동, 제작, 목록 등등) (0) | 2019.04.02 |