物体移出镜头外出现ui提示

ps:刚好项目要实现这个功能,翻了翻网上没有找到相关文章,自己就写了一个,有什么更好的建议还请多指教


实现思路:

1、检测物体是否移出屏幕外

      这部分unity有现成API,OnBecameVisible,与OnBecameInvisible. 这两个函数可以检测,但脚本要挂在检测的物体上,物体要有render组件

2、确定ui边缘位置

      物体世界坐标转为屏幕左边,计算屏幕中心点与物体屏幕坐标连线,与屏幕边缘相交的点.

3、判断与屏幕相交的线是哪一条

      使用矩阵给目标物体建立新的坐标



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

public class BeameF : MonoBehaviour
{
    //参照的摄像机
    GameObject m_camera;
    //目标名称
    string m_objName;
    //提示按钮是否出现
    bool m_isButtonDis = false;

    //提示按钮
    public GameObject m_Button0;

    //提示ui状态
    public enum Disstate
    {
        Left,
        Right,
        Up,
        Down
    }
    Disstate m_dis;

    void Start()
    {
        m_camera = GameObject.FindGameObjectWithTag("MainCamera");
    }

    void OnBecameVisible()
    {
        //提示按钮没有出现
        m_isButtonDis = false;

        Debug.Log(this.name + "+" + "Visible");

        //关闭按钮提示
        m_objName = this.name;
        InterativeButtonCueClose(m_objName);

    }

    void OnBecameInvisible()
    {

        Debug.Log(this.name + "+" + "Invisible");

        //目标坐标转屏幕坐标
        Vector2 objScreen = Camera.main.WorldToScreenPoint(this.transform.position);
        //屏幕中心点与屏幕边缘的交点
        Vector3 point = Point(objScreen);
        //摄像机位置下目标位置
        Vector3 m_targetV3 = CalUVByMatrix(MakeMatrix(this.transform.position, m_camera.transform.forward, m_camera.transform.up, m_camera.transform.right), m_camera.transform.position);

        //打开按钮提示
        m_objName = this.name;
        InterativeButtonCue(m_objName, m_targetV3, point);

        //提示按钮出现
        m_isButtonDis = true;
    }

    void Update()
    {
        if (m_isButtonDis)
        {
            StartCoroutine(Detection());
        }
        else
        {
            //关闭按钮提示
            m_objName = this.name;
            InterativeButtonCueClose(m_objName);
        }

    }

    //协程减少检测频率
    IEnum
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值