美文网首页unity3D技术分享
unity3dUi跟随场景移动,并且做到屏幕外隐藏

unity3dUi跟随场景移动,并且做到屏幕外隐藏

作者: 好怕怕 | 来源:发表于2022-04-29 12:12 被阅读0次

    3D


    image.png

    ui


    image.png image.png
    image.png
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class FlowTarget : MonoBehaviour
    {
    
        public enum mode
        {
            ui = 1,
            d3 = 2,
        }
    
        public GameObject item;
    
        public int mindis = 5;
        public int maxdis = 20;
    
        public mode type = mode.ui;
    
        public GameObject target;
    
        public Canvas ui_Canvas;
        public Camera UI_Camera;
        public Camera sceneCamera;// camera.main
    
        // Update is called once per frame
        void Update()
        {
            if (target)
            {
                if (type == mode.ui)
                {
                    var dis = Vector3.Distance(sceneCamera.transform.position, target.transform.position);
                    if (dis <= mindis || dis > maxdis)
                    {
                        item.gameObject.SetActive(false);
                    }
                    else
                    {
                        item.gameObject.SetActive(true);
    
                        Vector2 mouseDown = sceneCamera.WorldToScreenPoint(target.transform.position);
                        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(ui_Canvas.transform as RectTransform, mouseDown, UI_Camera, out var mouseUGUIPos);
                        if (isRect && IsInView(target.transform.position))
                        {
                            transform.GetComponent<RectTransform>().anchoredPosition = mouseUGUIPos;
                        }
                        else
                        {
                            item.gameObject.SetActive(false);
                        }
                    }
                }
                else
                {
                    var dis = Vector3.Distance(sceneCamera.transform.position, target.transform.position);
                    if (dis <= mindis || dis > maxdis)
                    {
                        item.gameObject.SetActive(false);
                    }
                    else
                    {
                        // 翻转了,需要处理floatitemroot,x=-1
                        transform.position = target.transform.position;
                        transform.LookAt(sceneCamera.transform, Vector3.up);
    
                        /*Vector2 mouseDown = sceneCamera.WorldToViewportPoint(target.transform.position);
                        if (IsInView(target.transform.position))
                        {
                            item.gameObject.SetActive(true);
                            transform.position = mouseDown;
                            transform.LookAt(sceneCamera.transform);
                        }
                        else
                        {
                            item.gameObject.SetActive(false);
                        }*/
                    }
                }
            }
        }
    
        public bool IsInView(Vector3 worldPos)
        {
            if (type == mode.ui)
            {
                Transform camTransform = sceneCamera.transform;
                Vector2 viewPos = sceneCamera.WorldToViewportPoint(worldPos);
                Vector3 dir = (worldPos - camTransform.position).normalized;
                float dot = Vector3.Dot(camTransform.forward, dir);// 检测对象是否在相机的正前面
    
                if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
                    return true;
                else
                    return false;
            }
            else
            {
                Transform camTransform = sceneCamera.transform;
                Vector2 viewPos = sceneCamera.WorldToViewportPoint(worldPos);
                Vector3 dir = (worldPos - camTransform.position).normalized;
                float dot = Vector3.Dot(camTransform.forward, dir);// 检测对象是否在相机的正前面
    
                if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
                    return true;
                else
                    return false;
            }
    
        }
    
    
    }
    
    
    
    image.png
    image.png
    image.png

    相关文章

      网友评论

        本文标题:unity3dUi跟随场景移动,并且做到屏幕外隐藏

        本文链接:https://www.haomeiwen.com/subject/mpvwjrtx.html