unity UGUI跟随3D物体的坐标转换

ugui 在物体上实时更新位置

本脚本挂在那个地方都行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UICollor : MonoBehaviour {
    public  GameObject model;//随便选一个3d模型point
    public Image image;//ugui图片
    public Text text;  //ugui图片text
    public float sety = 1;
    public LineRenderer line;   //line
    Vector3[] positions = new Vector3[] { Vector3.zero, Vector3.zero };
  
    void Update()
    {
       // Vector3 Nposition = Camera.main.WorldToScreenPoint(new Vector3(model.transform.position.x, model.transform.position.y, model.transform.position.z));
        image.transform.position = Camera.main.WorldToScreenPoint(new Vector3(model.transform.position.x, model.transform.position.y+sety, model.transform.position.z));
        //image.transform.position = Camera.main.WorldToScreenPoint(model.transform.position);
        Vector3 Lposition = Camera.main.ScreenToWorldPoint(image.transform.position);
        positions[0] = model.transform.position;
        positions[1] = Lposition;
        line.transform.position = model.transform.position ;
        line.SetPositions(positions);

    }
    //显示文字
    public void SetText(string value)
    {
        text.text = value;
    }
    //图片消失
    public void SetDisPlay()
    {
        image.gameObject.SetActive(false);
    }
    //图片出现
    public void Setplay()
    {
        image.gameObject.SetActive(true);
    }
    //
    public void SetModel(GameObject model)
    {
        this.model = model;
    }
    //线出现
    public void SetLineplay()
    {
        line.gameObject.SetActive(true);
    }
    //线消失
    public void SetLineHiding()
    {
        line.gameObject.SetActive(false);
    }

}

这边写一个协程 功能是一个模型一个模型的显示名称   具体使用自己看情况,自己写下来方便记载

 

unity UGUI跟随3D物体的坐标转换

unity UGUI跟随3D物体的坐标转换

上一篇:uGUI动态加载控件位置错误


下一篇:UGUI 之 Mask 和 RectMask2D 与 drawcall