2018.04.18 수업내용 (UI연출, 월드 위치를 NGUI 위치로 변환)
카테고리 없음 2018. 5. 15. 22:01반응형
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Test { public class InGame : MonoBehaviour { public Camera uiCam; public UIInGame uiInGame; public Character character; public UIRoot uiRoot; public Transform uiTargetPoint; // Use this for initialization void Start() { } // Update is called once per frame void Update() { if (Input.GetMouseButtonUp(0)) { RaycastHit hit; var ray = Camera.main.ScreenPointToRay(Input.mousePosition); Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 5); if (Physics.Raycast(ray, out hit, 100)) { Debug.Log(hit.collider.tag); if (hit.collider.tag == "Item") { Debug.Log(hit.collider.name); //w_300 //월드상에 있는 아이템 파괴 또는 오브젝트 풀로 보내버리기 hit.collider.gameObject.SetActive(false); //UI상에 같은 아이템 프리팹 생성 var obj = Resources.Load("UI/ui_w_300"); var prefab = Instantiate(obj) as GameObject; prefab.transform.parent = this.uiRoot.transform; prefab.transform.localScale = Vector3.one; //월드상의 아이템 좌표 -> UI상의 월드 좌표 var tpos = this.uiCam.ViewportToWorldPoint(Camera.main.WorldToViewportPoint(hit.collider.transform.position)); prefab.transform.position = tpos; StartCoroutine(this.MoveUIIcon(prefab.transform)); } } } } private IEnumerator MoveUIIcon(Transform prefab) { while (true) { var speed = 1.0f / 5.4f; var step = speed * Time.deltaTime; prefab.transform.position = Vector2.MoveTowards(prefab.transform.position, this.uiTargetPoint.position, speed); yield return null; //탈출구 var dis = Vector2.Distance(this.uiTargetPoint.position, prefab.transform.position); if (dis <= 0) break; } //날라간 UI아이콘 제거 Debug.Log("날라간 UI아이콘 제거 "); //UI 아이콘 보여주기 Debug.Log("UI 아이콘 보여주기"); } } } | cs |
아이템 획득하고 UI연출 하기
이하 영상의 해당 코드 첨부
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 | this.characterMono.OnGetItem = (item) => { //획득한 월드의 아이템 파괴 또는 오브젝트 풀로 보내기 item.gameObject.SetActive(false); //UI상에 아이템 생성 var uiPrefab = Instantiate(Resources.Load("UI/UI.Ico.Item")) as GameObject; //이미지 동기화 var spIcon = uiPrefab.GetComponent<UISprite>(); spIcon.spriteName = app.dicItemData[item.id].icoName; spIcon.MakePixelPerfect(); //UI Root밑에 넣기 uiPrefab.transform.parent = app.uiRoot.transform; //스케일 조정 uiPrefab.transform.localScale = Vector3.one; //위치 변환 var uiCam = NGUITools.FindCameraForLayer(LayerMask.NameToLayer("UI")); uiPrefab.transform.position = uiCam.ViewportToWorldPoint(Camera.main.WorldToViewportPoint(item.transform.position)); //이동 var targetPosition = this.uiInGame.icoWeapon.transform.position; uiPrefab.transform.DOMove(targetPosition, 0.5f).SetEase(Ease.InQuad).OnComplete(() => { //이동 완료 //스케일 연출 uiPrefab.transform.DOScale(new Vector3(1.2f, 1.2f, 1.2f), 0.5f).OnComplete(() => { uiPrefab.transform.DOScale(Vector3.one, 0.5f).OnComplete(() => { //UI아이템 파괴 또는 오브젝트 풀에 넣기 uiPrefab.gameObject.SetActive(false); //UI표시 this.uiInGame.icoWeapon.spriteName = app.dicItemData[item.id].icoName; this.uiInGame.icoWeapon.MakePixelPerfect(); }); }); }); this.characterMono.info.itemInfo.id = item.id; app.gameInfo.characterInfo = this.characterMono.info; }; | cs |
반응형