HudText (damage text), world to screen position

Unity3D 2022. 2. 17. 16:31
반응형

 

 

 

테스트 코드 

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

public class Test : MonoBehaviour
{
    public Button btn1;
    public Button btn2;

    public Animator anim1;   //player1 
    public Animator anim2;   //player2 

    public ParticleSystem fs;
    public Transform point;
    public RectTransform damageTextRect;
    public RectTransform canvasRect;
    private Canvas canvas;

    public Camera uiCam;

    void Start()
    {
        this.canvas = this.canvasRect.GetComponent<Canvas>();

        var animEventReceiver1 = this.anim1.gameObject.GetComponent<AnimationEventReceiver>();
        animEventReceiver1.triggerAnimationEvent.AddListener((eventName) => {
            Debug.LogFormat("<color=yellow>eventName: {0}</color>", eventName);
            //Debug.LogErrorFormat("<color=yellow>eventName: {0}</color>", eventName);
            this.anim2.Play("GetHit", -1, 0); //reset and play 

            fs.Play();
        });

        var animEventReceiver2 = this.anim2.gameObject.GetComponent<AnimationEventReceiver>();
        animEventReceiver2.triggerAnimationEvent.AddListener((eventName) => {
            Debug.LogErrorFormat("<color=yellow>eventName: {0}</color>", eventName);
        });


        this.btn1.onClick.AddListener(() => {
            this.anim1.Play("Attack01", -1, 0); //reset and play 
        });

        this.btn2.onClick.AddListener(() => {
            this.anim2.Play("GetHit", -1, 0); //reset and play 
        });
    }

    private void Update()
    {
        switch (canvas.renderMode)
        {
            case RenderMode.ScreenSpaceOverlay:
                {
                    var pos = Camera.main.WorldToScreenPoint(this.point.position);
                    this.damageTextRect.position = pos;
                }
                break;

            case RenderMode.ScreenSpaceCamera:
                {
                    var offset = Vector3.zero;
                    Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(Camera.main, this.point.position + offset);
                    var pos = Vector2.zero;
                    RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPos, this.uiCam, out pos);
                    this.damageTextRect.localPosition = pos;
                }
                break;
        }

    }

}

 


 

최종 코드 

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

public class UIHudText : MonoBehaviour
{
    public Text damageText;

    public void Init(float damage) {
        this.damageText.text = damage.ToString();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class Test : MonoBehaviour
{
    public Button btn1;
    public Button btn2;

    public Animator anim1;   //player1 
    public Animator anim2;   //player2 

    public ParticleSystem fs;
    public Transform hudPivot;
    public GameObject hudPivotPrefab;
    public RectTransform canvasRectTrans;

    void Start()
    {
        var animEventReceiver1 = this.anim1.gameObject.GetComponent<AnimationEventReceiver>();
        animEventReceiver1.triggerAnimationEvent.AddListener((eventName) => {
            Debug.LogFormat("<color=yellow>eventName: {0}</color>", eventName);
            //Debug.LogErrorFormat("<color=yellow>eventName: {0}</color>", eventName);
            this.anim2.Play("GetHit", -1, 0); //reset and play 

            fs.Play();
        });

        var animEventReceiver2 = this.anim2.gameObject.GetComponent<AnimationEventReceiver>();
        animEventReceiver2.triggerAnimationEvent.AddListener((eventName) => {
            Debug.LogFormat("<color=yellow>eventName: {0}</color>", eventName);

            GameObject uiGo = Instantiate<GameObject>(this.hudPivotPrefab, this.canvasRectTrans);
            UIHudText uiHudText = uiGo.GetComponent<UIHudText>();

            uiHudText.Init(Random.Range(1, 100));

            Vector3 screenPos = Camera.main.WorldToScreenPoint(this.hudPivot.position);
            var rectTrans = uiGo.GetComponent<RectTransform>();

            //init
            rectTrans.position = screenPos;
            rectTrans.localScale = Vector3.zero;

            rectTrans.DOScale(1.5f, 0.3f).onComplete = () => {
                rectTrans.DOScale(1f, 0.3f);
            };
            //move tween 
            rectTrans.DOLocalMoveY(200f, 0.6f).onComplete = () => {
                Debug.Log("complete!");
                Destroy(rectTrans.gameObject);  //hudText go 
            };


        });


        this.btn1.onClick.AddListener(() => {
            this.anim1.Play("Attack01", -1, 0); //reset and play 
        });

        this.btn2.onClick.AddListener(() => {
            this.anim2.Play("GetHit", -1, 0); //reset and play 
        });
    }

}

 


 

참고

https://tech.pjin.jp/blog/2017/07/14/unity_ugui_sync_rendermode/

https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html

반응형

'Unity3D' 카테고리의 다른 글

Load font from mobile device and assign as fallback TMP_FontAsset  (0) 2022.05.03
SphereCast & LayerMask  (0) 2022.03.31
Quaternion.SetLookRotation  (0) 2022.01.26
도전 5번 코드 입니다  (0) 2022.01.26
Overview UIBuidler 1.0.0-preview.18  (0) 2021.11.08
: