当前位置:   article > 正文

【Unity学习】实现screen space模式下UI跟踪游戏物体(在 UI 中跟踪和显示任何 Gameobject 位置)_unity ui 追踪物体

unity ui 追踪物体

话不多说,直接上代码:


using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(CanvasGroup))]
public class UI_Tracking_Target_Pos : MonoBehaviour
{
	//目标物体
    public Transform target;
    //UI与目标物体的距离
    public Vector3 offset;
    //当物体不在视图范围内,显示的物体方向图片
    public Image targetOrientation;
    //要赋值给上面Image的上下左右sprite
    public Sprite up_Texture;
    public Sprite down_Texture;
    public Sprite left_Texture;
    public Sprite right_Texture;
	//目标物体的屏幕空间坐标
    private Vector3 targetScreenPos;

    // Update is called once per frame
    void Update()
    {
        if (!isTargetInView())
        {
            transform.GetComponent<CanvasGroup>().alpha = 0;
        }
        else
        {
            transform.GetComponent<CanvasGroup>().alpha = 1;
        }

        targetScreenPos = Camera.main.WorldToScreenPoint(target.position);
        transform.GetComponent<RectTransform>().position = targetScreenPos + offset;
    }

    private bool isTargetInView()
    {
        Vector3 vec = Camera.main.WorldToViewportPoint(target.position);
        if (vec.x > 0 && vec.x < 1 && vec.y > 0 && vec.y < 1 && vec.z > 0)
        {
            targetOrientation.transform.SetParent(transform);
            targetOrientation.transform.position = transform.position;
            targetOrientation.transform.GetComponent<CanvasGroup>().alpha = 0;
            //物体的中心的在相机内部
            return true;
        }
        else
        {
            targetOrientation.transform.SetParent(transform.parent);
            targetOrientation.transform.GetComponent<CanvasGroup>().alpha = 1;

            if (vec.x < 0)
            {
                targetOrientation.sprite = left_Texture;
                targetOrientation.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 0.5f);
                targetOrientation.transform.position = new Vector3(0, transform.position.y, 0);
            }
            else if (vec.x > 1)
            {
                targetOrientation.sprite = right_Texture;
                targetOrientation.transform.GetComponent<RectTransform>().pivot = new Vector2(1, 0.5f);
                //canvas scale设置为1920*1080
                targetOrientation.transform.position = new Vector3(1920, transform.position.y, 0);
            }
            else if (vec.y < 0)
            {
                targetOrientation.sprite = down_Texture;
                targetOrientation.transform.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0);
                targetOrientation.transform.position = new Vector3(transform.position.x, 0, 0);
            }
            else if (vec.y > 1)
            {
                targetOrientation.sprite = up_Texture;
                targetOrientation.transform.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 1);
                targetOrientation.transform.position = new Vector3(transform.position.x, 1080, 0);
            }
            return false;
        }
    }
}
  • 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

Inspector面板配置如下:
在这里插入图片描述
效果如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/111373
推荐阅读
相关标签
  

闽ICP备14008679号