鼠标放在物品上显示物品名字,点击物品后出现物品信息
首先创建一个Cube,并且给它添加BoxColider组件,使它可以被检测到。然后新建一个C#脚本,并添加给Cube。
C#源代码:
using UnityEngine;
using System.Collections;public class Cube : MonoBehaviour
{// public Transform cube;bool isShowTip;public bool WindowShow = false;// // Use this for initializationvoid Start(){isShowTip = false;}void OnMouseEnter(){isShowTip = true;//Debug.Log (cube.name);//可以得到物体的名字}void OnMouseExit(){isShowTip = false;}void OnGUI(){if (isShowTip){GUIStyle style1= new GUIStyle();style1.fontSize = 30;style1.normal.textColor = Color.red;GUI.Label(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 400, 50),"Cube", style1);}if (WindowShow)GUI.Window(0, new Rect(30, 30, 200, 100), MyWindow, "Cube");}//对话框函数void MyWindow(int WindowID){GUILayout.Label("你想写入的内容");}//鼠标点击事件void OnMouseDown(){Debug.Log("show");if (WindowShow)WindowShow = false;elseWindowShow = true;}
}
原文链接:https://blog.csdn.net/HanGuangFei/article/details/78094214