UI位置跟隨3D物體
阿新 • • 發佈:2018-12-14
/************************************************************ 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預製體 就是 一個擺設底板加文字