unity倉庫管理簡易模型(二.顯示物體詳情)

倉庫模型補充—— 詳情顯示

效果展示
點擊物體後顯示詳情,鼠標移開後消失
在這裏插入圖片描述

在需要顯示詳情的預設體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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章