Unity/수업내용

Unity NGUI 21.04.28. 수업내용 - 인게임과 UI 연동하기

HappyFrog 2021. 4. 28. 13:16

이 레이어를 찍고잇는 카메라를 찾는 기능이다.

 

UI의 해상도에 상관 없이 좌하단은 (0,0), 우하단은 (1,0), 좌상단은 (0,1), 우상단은 (1,1)을 반환하는 함수이다.

ㄴ개인적인 느낌으론 normalized와 비슷한 느낌인 듯 하다.

 

 

 

Instantiate와 비슷한 기능을 해주는 NGUI의 기능이다 하지aNGUI와 연동을 할 때 종종 렌더링버그가 발생할 수 있어서 AddChild를 사용하는게 좋다고 생각한다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    public GameObject gaugeGo;
    public Hero hero;
    public UIButton btn;
    public GameObject labelPrefab;
    public UIPanel panel;
    //public GameObject labelGo;

    void Start()
    {
        DataManager.GetInstance().LoadDatas();
        Init(102);

        this.btn.onClick.Add(new EventDelegate(() =>
        {
            GameObject go = NGUITools.AddChild(this.panel.gameObject, labelPrefab);
            Vector3 overlay = NGUIMath.WorldToLocalPoint(
                this.hero.hudDamageTrans.position,
                Camera.main,
                UICamera.mainCamera,
                go.transform);
            overlay.z = 0.0f;
            go.transform.localPosition = overlay;

            var hud = go.GetComponent<HudText>();
            hud.Init(9999);

        }));
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit;
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.LogFormat("Input.mousePosition : {0}", Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction, Color.red, 2.0f);

            hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);

            if (hit)
            {
                this.hero.Move(hit.point);
            }
        }
    }

    public void Init(int id)
    {
        var shell = new GameObject();
        shell.name = "Hero";
        var hudGauge = Instantiate(new GameObject(), shell.transform);
        hudGauge.name = "hudGauge";
        var model = CreateModel(id);
        var data = DataManager.GetInstance().dicCharacterData[id];
        model.name = data.name;
        model.transform.SetParent(shell.transform);
        var hero = shell.AddComponent<Hero>();
        hero.Init(data, model);
        
        
    }

    public GameObject CreateModel(int id)
    {
        var data = DataManager.GetInstance().dicCharacterData[id];
        var path = string.Format("Prefabs/{0}", data.name);
        var prefab = Resources.Load<GameObject>(path);
        var go = Instantiate<GameObject>(prefab);
        return go;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterData
{
    public int id;
    public string name;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class HudText : MonoBehaviour
{
    public UILabel label;
    public void Init(int damage)
    {
        this.label.text = damage.ToString();

        var endPos = this.label.transform.position.y + 200;

        float myFloat = 1f;
        var color = this.label.color;
        DOTween.To(() => myFloat, x => myFloat = x, 0f, 0.5f).onUpdate = () => 
        {
            color.a = myFloat;
            this.label.color = color; 
        };

        this.label.transform.DOScale(2, 0.2f).onComplete = () =>
         {
             this.label.transform.DOScale(1, 0.2f);
         };

        this.label.transform.DOLocalMoveY(endPos, 0.5f).SetEase(Ease.OutQuad).onComplete = () =>
        {
            Destroy(this.gameObject);
        };
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    public Transform hudGaugeTrans;
    public Transform hudDamageTrans;
    public GameObject gaugeGo;
    public float speed;

    Animator anime;
    Coroutine routine;
    CharacterData data;
    GameObject model;

    private void Start()
    {
        
    }

    void Update()
    {
        //https://www.tasharen.com/forum/index.php?topic=14754.0
        //https://www.tasharen.com/ngui/docs/class_n_g_u_i_math.html
        Vector3 overlay = NGUIMath.WorldToLocalPoint(this.hudGaugeTrans.position, Camera.main, UICamera.mainCamera, gaugeGo.transform);
        overlay.z = 0.0f;
        //Debug.Log(overlay);
        gaugeGo.transform.localPosition = overlay;

    }

    public void Init(CharacterData data, GameObject model)
    {
        this.data = data;
        this.model = model;
        this.gaugeGo = Resources.Load<GameObject>("Prefabs/gauge");
        var hudGauge = GameObject.Find("hudGauge");
        this.hudGaugeTrans = hudGauge.transform;
        this.anime = this.model.GetComponent<Animator>();
    }


    public void Move(Vector3 tpos)
    {
        if (this.routine != null)
        {
            StopCoroutine(this.routine);
        }
        this.routine = StartCoroutine(this.MoveImpl(tpos));
    }

    IEnumerator MoveImpl(Vector3 tpos)
    {
        this.anime.Play("run" +
            "");
        if (this.transform.position.x < tpos.x)
        {
            this.transform.localScale = Vector3.one;
        }
        else
        {
            this.transform.localScale = new Vector3(-1, 1, 1);
        }

        while (true)
        {
            var dir = (tpos - this.transform.position).normalized;
            this.transform.Translate(dir * this.speed * Time.deltaTime);
            var distance = Vector3.Distance(tpos, this.transform.position);
            if (distance <= 0.1f)
            {
                this.transform.position = tpos;
                this.anime.Play("idle");
                break;
            }
            yield return null;

        }

    }

}

shell에 캐릭터 프리팹까지는 불러와지는데 게이지가 불러와지지 않는다.

 

 

테이블

======

id name

int string

101 birdOrc

102 centaurGeneral

103 kobold