赞
踩
鼠标悬停在3D物体上(带有碰撞体的物体),用GUI显示
这里的MHQX_1304和图片都是从数据库里取出来的
1、GUI显示信息(部分)
原理:鼠标发出一条射线,碰撞检测
调用方式:showName()
private void showName(){
Ray ray = _camera.ScreenPointToRay(Input.mousePosition); //定义一条射线,这条射线从摄像机屏幕射向鼠标所在位置
RaycastHit hit; //声明一个碰撞的点
GUI.skin.label.fontSize = 24; //字体大小
if (Physics.Raycast(ray, out hit))
{
//如果真的发生了碰撞,ray这条射线在hit点与物体碰撞了
GUI.Label(new Rect(Input.mousePosition.x + 15, Screen.height - Input.mousePosition.y, 200, 100), "显示的信息");
}
}
2、读数据(部分)
引用集
using MySql.Data.MySqlClient;
using System.Data;
全局变量
private string device_id=null;
private string qr_code= null;
Database db;
DataSet ds = new DataSet();//数据库链接
调用方式
link(“XFS_1301”);//消防栓编号
private Boolean link(string name) { db = new Database(); string str = "select * from build_device where device_id='" + name + "'";//SQL语句 根据物体的编号从数据库中select相对应的记录 try { MySqlDataReader sdr = db.Query(str); Debug.Log(str); if (sdr.Read()) { int id = sdr.GetInt32(sdr.GetOrdinal("Id")); device_id = sdr.GetString(sdr.GetOrdinal("device_id"));//取出此条记录的device_id字段的数据 return true; } else { sdr.Close(); return false; } } catch (Exception e1) { Debug.Log("发生异常中断,错误原因:\n" + e1); return false; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data.MySqlClient; using System.Data; class Database { /* 这里修改数据库信息 Database 数据库名称 User ID 用户 Password 密码 Host 主机 Port 端口号 */ public string connectionString = "User ID=root;Database=fire; Password=123456; Host=127.0.0.1; Port= 3306;Protocol=TCP; Compress=false; Pooling=true; Min Pool Size=0;Max Pool Size=100; Connection Lifetime=0;Charset=utf8 "; MySqlConnection connection = null; DataSet ds1 = new DataSet(); public Database() { connection = new MySqlConnection(connectionString); connection.Open(); } public MySqlDataReader Query(string str) { MySqlDataReader sdr = null; try { MySqlCommand cmd = new MySqlCommand(str, connection); cmd.CommandTimeout = 50; sdr = cmd.ExecuteReader(); } catch (Exception e1) { // MessageBox.Show(e1.Message); } return sdr; } ~Database() { connection.Close(); } public int NonQuery(string str) { int num = 0; try { MySqlCommand cmd = new MySqlCommand(str, connection); cmd.CommandTimeout = 50; num = cmd.ExecuteNonQuery(); } catch (Exception e1) { //MessageBox.Show(e1.Message); } return num; } public DataSet Dataset(string str, string tablename) { MySqlCommand cmd = new MySqlCommand(str, connection); MySqlDataAdapter sda = new MySqlDataAdapter(); sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds, tablename); return ds; } }
using MySql.Data.MySqlClient; using System; using System.Collections; using System.Collections.Generic; using System.Data; using UnityEngine; using UnityEngine.UI; public class GetTag : MonoBehaviour { private Camera _camera; private string device_id=null; private string qr_code= null; private Texture2D imageTexture; // Start is called before the first frame update Database db; DataSet ds = new DataSet();//数据库链接 void Start() { _camera = GetComponent<Camera>();//获取场景中摄像机对象的组件接口 } // Update is called once per frame void Update() { } void OnGUI() { showName(); } private void showName(){ Ray ray = _camera.ScreenPointToRay(Input.mousePosition); //定义一条射线,这条射线从摄像机屏幕射向鼠标所在位置 RaycastHit hit; //声明一个碰撞的点 GUI.skin.label.fontSize = 24; //字体大小 if (Physics.Raycast(ray, out hit)) { //如果真的发生了碰撞,ray这条射线在hit点与物体碰撞了 if (Showindex(hit.transform.name) == true) { LoadByWWW(); GUI.Label(new Rect(Input.mousePosition.x + 15, Screen.height - Input.mousePosition.y, 200, 100), device_id); GUI.Label(new Rect(Input.mousePosition.x + 15, Screen.height - Input.mousePosition.y+30, 300, 300), imageTexture); //GUI.Label(new Rect(Input.mousePosition.x + 15, Screen.height - Input.mousePosition.y, 300, 300), imageTexture); } } } private Boolean Showindex(string name) { db = new Database(); string str = "select * from build_device where device_id='" + name + "'";//SQL语句 根据物体的编号从数据库中select相对应的记录 try { MySqlDataReader sdr = db.Query(str); Debug.Log(str); if (sdr.Read()) { int id = sdr.GetInt32(sdr.GetOrdinal("Id")); device_id = sdr.GetString(sdr.GetOrdinal("device_id")); qr_code = sdr.GetString(sdr.GetOrdinal("qr_code")); return true; } else { Debug.Log("鼠标不在有碰撞体的物体上"); sdr.Close(); return false; } } catch (Exception e1) { Debug.Log("发生异常中断,错误原因:\n" + e1); return false; } } private void LoadByWWW() { StartCoroutine(Load()); } IEnumerator Load() { WWW www = new WWW(qr_code);//请求WWW yield return www; if (www != null && string.IsNullOrEmpty(www.error)) { imageTexture = www.texture;//获取Texture } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。