版权声明:记录上工作中的一些问题 https://blog.csdn.net/m0_37583098/article/details/82978842
/************************************************************
Copyright (C), 2007-2017,BJ Rainier Tech. Co., Ltd.
FileName: RayCastHitControl.cs
Author: Version :1.0 Date: 2018/8/15
Description: 鼠标进入物体显示标签名(UI位置随3D物体的位置)
************************************************************/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class ShowBendWoodName : MonoBehaviour
{
private Vector3 namePosInScreen;
public Vector3 namePosOffset;//标签偏移量
public GameObject prefabNameSprite;//预制体
public Transform nameLableFather;//标签父类
private GameObject nameLable;
// Use this for initialization
void Start()
{
nameLable = Instantiate(prefabNameSprite, new Vector3(namePosInScreen.x, namePosInScreen.y, 0f), Quaternion.identity) as GameObject;
nameLable.transform.SetParent(nameLableFather);
nameLable.name = gameObject.name;
nameLable.SetActive(false);
nameLable.transform.FindChild("Text").GetComponent<Text>().text = gameObject.name;
}
void OnMouseEnter() {
nameLable.SetActive(true);
OverlayPosition();
}
void OnMouseExit()
{
nameLable.SetActive(false);
}
void OverlayPosition() {
namePosInScreen = Camera.main.WorldToScreenPoint(gameObject.transform.position);
nameLable.GetComponent<RectTransform>().position = new Vector3(namePosInScreen.x, namePosInScreen.y, 0f) + namePosOffset;
}
}
prefabNameSprite预制体 就是 一个摆设底板加文字