using System.Collections; using System.Collections.Generic; using UnityEngine; using MySql.Data.MySqlClient; using System; using System.Data; using test; using System.Threading; namespace cakeslice { public class Isoutline : MonoBehaviour { static string connstr = "Server=127.0.0.1;Database=test;user ID=root;password=123456;port=3306;"; static string sql = "select * from user"; public string addr = ""; static Rect RectWnd; SqlBase sqlbase; DataSet ds = new DataSet(); DataTable table = null; Transform tf; public bool isShowTip;//是否显示标签 public bool isShowWindow;//是否显示窗口 public bool isInput;//是否输入 public bool isClick=false; float x = 0, y = 0; // Start is called before the first frame update void Start() { sqlbase = new SqlBase(connstr); ds = sqlbase.GetDataSet(sql); table = ds.Tables[0]; GetComponent().enabled = false;// !GetComponent().enabled; isShowTip = false; isShowWindow = false; isInput = false; //isClick = false; } // Update is called once per frame void Update() { if (ds != null) { if (isInput == false) { ds = sqlbase.GetDataSet(sql); table = ds.Tables[0]; if(table!=null) OnorOff(table); GetObject(); } RectWnd.x += 10; } } /// /// 鼠标点击获的物体 /// void GetObject() { GetComponent().enabled = false; if (Input.GetMouseButtonDown(0)) { x = Input.mousePosition.x; y = Input.mousePosition.y; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow); RaycastHit hit; if (Physics.Raycast(ray, out hit))//&&hit.collider.tag== "LeftSwitch"|| hit.collider.tag == "RightSwitch") { tf = hit.transform; if (tf == transform) { tf.GetComponent().enabled = true;// !GetComponent().enabled; isShowTip = true; isShowWindow = true; isInput = true; } else { GetComponent().enabled = false;// !GetComponent().enabled; isShowTip = false; isShowWindow = false; isInput = false; } } } } /// /// GUI绘制标签和窗口 /// private void OnGUI() { if (tf != null) { if (isShowTip && tf.GetComponent().enabled) { GUIStyle style = new GUIStyle(); style.fontSize = 30; style.normal.textColor = Color.red; Rect rect = new Rect(x, Screen.height - y, 400, 50); if (tf != null) { GUI.Label(rect, tf.name, style); //if (isShowWindow && tf.GetComponent().enabled) //{ // GUI.Window(0, new Rect(x + 100, Screen.height - y + 30, 200, 180), Mywindow, "信息"); //} } } if (isShowWindow && tf.GetComponent().enabled) { //for (int i = 0; i < 100; i += 10) //{ RectWnd = GUI.Window(0, new Rect(x + 100, Screen.height - y + 30, 200, 180), Mywindow, "信息"); //Thread.Sleep(1000); //} } } } /// /// 窗口绘制函数 /// /// void Mywindow(int WindowID) { for (int i = 0; i < table.Rows.Count; i++) { if (tf.name == table.Rows[i][1].ToString()) { GUILayout.Label("ID:" + table.Rows[i][0].ToString()); GUILayout.Label("名称:" + table.Rows[i][1].ToString()); GUILayout.Label("年龄:" + table.Rows[i][2].ToString()); GUILayout.Label("状态:" + table.Rows[i][3].ToString()); GUILayout.Label("地址: " + table.Rows[i][4].ToString()); addr = GUI.TextField(new Rect(45, 120, 130, 20), addr); if (GUILayout.Button("OK")) { isShowTip = false; isShowWindow = false; isInput = false; isClick = false; if (addr != "") { UpdateAddr(tf.name, addr); } GetComponent().enabled = false; } else { //isInput = false; } } } } /// /// 更新地址 /// /// /// void UpdateAddr(string name, string addr) { int infoaddr = int.Parse(addr); string updatasql = "update user set infoaddr=" + infoaddr + " where na='" + name + "';"; print(updatasql); int r = sqlbase.commandExecute(updatasql); print(r); if (r > 0) { Debug.Log("修改成功"); } else { Debug.Log("修改失败"); } ds = sqlbase.GetDataSet(sql); } /// /// 同步更新状态 /// /// public void OnorOff(DataTable table) { var Switchs = GameObject.FindGameObjectsWithTag("Switch"); for (int i = 0; i < table.Rows.Count; i++) { string name = table.Rows[i][1].ToString(); string state = table.Rows[i][3].ToString(); string rota = table.Rows[i][5].ToString(); Transform tr = Switchs[i].transform; if (name==tr.name) { switch (state, rota) { case ("on", "0"): tr.rotation = Quaternion.Euler(-90, 90, 0); break; case ("on", "1"): tr.rotation = Quaternion.Euler(-90, -90, 0); break; case ("off", "0"): tr.rotation = Quaternion.Euler(-90, 0, 0); break; case ("off", "1"): tr.rotation = Quaternion.Euler(-90, 0, 0); break; } Debug.Log("name=" + name + "state=" + state); } } } } }