using UnityEngine;
using System.Collections;
/// <summary>
/// 时间:20161128 XXX
/// 公司:山东XX有限公司
/// 功能介绍:
/// 适用于,所有想达到效果“用户点击可以出现物体详情窗口”的gameobject.
/// 步骤:1.绑定带有collider的obj之后,拖拽将要显示的uguiPanel,给公共变量“_MyInfoPanelObj”赋值;
/// </summary>
public class ObjBeClickedShowWin : MonoBehaviour {
/// <summary>
/// 当前视角的摄像机
/// </summary>
public Camera _CurCamera;
/// <summary>
/// 鼠标射线
/// </summary>
private Ray _ray;
/// <summary>
/// 射线碰撞的结构
/// </summary>
private RaycastHit _rayhit;
/// <summary>
/// 鼠标拾取的有效距离
/// </summary>
private float _fDistance = 2000f;
/// <summary>
/// 点击物体,即将显示的悬浮窗obj
/// </summary>
public GameObject _MyInfoPanelObj;
/// <summary>
/// 悬浮窗宽和高
/// </summary>
float _MyInfoPanelWidth;
float _MyInfoPanelHeight;
// Use this for initialization
void Start () {
//获取当前摄像机
_CurCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
//获取 悬浮窗宽和高
_MyInfoPanelWidth = _MyInfoPanelObj.GetComponent<RectTransform>().rect.width;
_MyInfoPanelHeight = _MyInfoPanelObj.GetComponent<RectTransform>().rect.height;
}
// Update is called once per frame
void Update () {
//检测鼠标左键的拾取
if (Input.GetMouseButtonDown(0))
{
//鼠标的屏幕坐标空间位置转射线
_ray = _CurCamera.ScreenPointToRay(Input.mousePosition);
//射线检测,相关检测信息保存到RaycastHit 结构中
if (Physics.Raycast(_ray, out _rayhit, _fDistance))
{
//打印射线碰撞到的对象的名称
print("_rayhit.collider.name:" + _rayhit.collider.name);
if (_rayhit.collider.name == this.gameObject.name && !_MyInfoPanelObj.activeInHierarchy)
{//即将显示的悬浮窗obj坐标赋值
_MyInfoPanelObj.transform.position = new Vector3(Input.mousePosition.x+_MyInfoPanelWidth/2,Input.mousePosition.y-_MyInfoPanelHeight/2,0);
_MyInfoPanelObj.SetActive(true);
}
}
}
}
}