赞
踩
实现的功能:当鼠标移动到该物体上,屏幕上就会显示出该物体的标签信息,在unity中改变 Name,当鼠标再次回来时标签上的名字也会改变
我主要用到的是:鼠标进出OnMouseEnter、OnMouseExit
相对于Main Camera相机的世界坐标转屏幕坐标
Camera.main.WorldToScreenPoint(要转换的世界坐标)
话不多说,上代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- /// <summary>
- /// 每个需要展示标签的物体都需要挂载此脚本
- /// </summary>
- public class ShowLabels : MonoBehaviour
- {
- public string Name;
-
- public float Offset_Y; //偏移量,微调显示标签的y轴
-
- private GameObject obj;
-
- private Transform label;
-
- private Transform can;
-
- private Vector2 curPos;
-
- private Vector2 centerPos;
- private Vector2 sizePos;
-
- void Start()
- {
- label = Resources.Load<GameObject>("Label").transform;
-
- can = GameObject.Find("
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。