仓库模型补充—— 详情显示
效果展示
点击物体后显示详情,鼠标移开后消失
在需要显示详情的预设体Cube下添加脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class newGui : MonoBehaviour
{
private bool flag;
private Camera cam;//发射射线的摄像机
private GameObject go;//射线碰撞的物体
public string btnName;//射线碰撞物体的名字
private Vector3 screenSpace;
private Vector3 offset;
private void Start()
{
flag = false;
}
private void OnMouseDown()
{
flag = true;
}
private void OnMouseExit()
{
flag = false;
}
private void OnGUI()
{
if (flag)
{
cam = Camera.main;
//整体初始位置
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//从摄像机发出到点击坐标的射线
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
//划出射线,只有在scene视图中才能看到
Debug.DrawLine(ray.origin, hitInfo.point);
go = hitInfo.collider.gameObject;
//print(btnName);
//screenSpace = cam.WorldToScreenPoint(go.transform.position);
//offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
//物体的名字
btnName = go.name;
//发送给winform
// Application.ExternalEval(btnName);
Debug.Log("@@@" + btnName + "@@@");
//组件的名字
sqlAceess sql = new sqlAceess();
sql.Conn();
Save save = sql.selectByName(btnName);
sql.ConnClose();
GUI.Box(new Rect(800, 20, 200, 250), "cube信息");
GUILayout.BeginArea(new Rect(800, 40, 200, 200));
GUILayout.Label("物体名称名称");
GUILayout.Label(save.cubeName);
GUILayout.Label("x坐标");
GUILayout.Label(save.x.ToString());
GUILayout.Label("y坐标");
GUILayout.Label(save.y.ToString());
GUILayout.Label("z坐标");
GUILayout.Label(save.z.ToString());
GUILayout.EndArea();
}
if (Input.GetKeyDown(KeyCode.Z))
{
Destroy(go);
}
}
}
public void didi()
{
//flag = true;
if (flag == false)
{
flag = true;
}
else
{
flag = false;
}
}
}
在sqlAceess下添加新的查询方法
public Save selectByName(string cubeName)
{
String sql = "select * from cubeInfo where cubeName = @cubeName";
SqlCommand sqlComm = new SqlCommand(sql, conn);
// sqlComm.Parameters.AddWithValue("@cubeName", cubeName);
SqlParameter para1 = new SqlParameter("@cubeName", cubeName);
sqlComm.Parameters.Add(para1);
int re = sqlComm.ExecuteNonQuery();
Save save = new Save();
//接收查询到的sql数据
SqlDataReader reader = sqlComm.ExecuteReader();
while (reader.Read())
{
save.cubeName = reader.GetString(2);
save.x = reader.GetDouble(3);
save.y = reader.GetDouble(4);
save.z = reader.GetDouble(5);
save.l = reader.GetDouble(6);
save.m = reader.GetDouble(7);
save.n = reader.GetDouble(8);
save.w = reader.GetDouble(9);
}
reader.Close();
return save;
}