unity|從數據庫中讀取信息 鼠標懸停顯示文字/圖片 (GUI)


一、效果圖

鼠標懸停在3D物體上(帶有碰撞體的物體),用GUI顯示
這里的MHQX_1304和圖片都是從數據庫里取出來的
在這里插入圖片描述

1、需要顯示信息的物體

  • 給物體添加碰撞體
  • 將名字命名為數據庫里的主鍵或者是唯一的就好(因為要根據名字去數據庫里搜索)

在這里插入圖片描述

2、代碼分解

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; } } 

3、連接數據庫、數據庫配置信息(完整)

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; } } 

4、掛在攝像機上的代碼(完整)

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 } } } 

總結

  • 將3和4的代碼放入Project->Assets->Scripts
  • 將4代碼掛在攝像機上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM