UI跟随3D对象
Posted 那个妹子留步
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UI跟随3D对象相关的知识,希望对你有一定的参考价值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using Core.UI;
using Game.UI;
using UnityEngine.UI;
public class UIFollowObj : MonoBehaviour
public GameObject FallowObj = null;
public bool isEnableFallow = true;
public bool AllowXFollow = true;
public bool AllowYFollow = true;
public Camera WorldCamera;
public Camera UICamera;
public RectTransform Rectangle;
private Vector3 OffsetWorldV3 = Vector3.zero;
public void SetOffset(float x,float y,float z)
OffsetWorldV3.x = x;
OffsetWorldV3.y = y;
OffsetWorldV3.z = z;
void Update()
if (!isEnableFallow || WorldCamera == null|| UICamera == null)
return;
if (FallowObj != null)
Vector3 screenPos = WorldCamera.WorldToScreenPoint(FallowObj.transform.position+ OffsetWorldV3);
Vector2 localPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(Rectangle, screenPos, UICamera, out localPos))
transform.localPosition = localPos;
protected void OnDestroy()
FallowObj = null;
public void SetFallowObject(GameObject obj)
FallowObj = obj;
public void EnableFallow(bool bVal)
isEnableFallow = bVal;
以上是关于UI跟随3D对象的主要内容,如果未能解决你的问题,请参考以下文章
虚拟仿真Unity3D中实现UI跟随3D模型旋转移动UI一直面朝屏幕
虚拟仿真Unity3D中实现UI跟随3D模型旋转移动UI一直面朝屏幕
Unity实战篇 |制作一个跟随鼠标转向的 简易箭头指示标,包括 UI指向 和 3D指向标