123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- 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<Outline>().enabled = false;// !GetComponent<Outline>().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;
-
- }
- }
- /// <summary>
- /// 鼠标点击获的物体
- /// </summary>
- void GetObject()
- {
- GetComponent<Outline>().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<Outline>().enabled = true;// !GetComponent<Outline>().enabled;
- isShowTip = true;
- isShowWindow = true;
- isInput = true;
- }
- else
- {
- GetComponent<Outline>().enabled = false;// !GetComponent<Outline>().enabled;
- isShowTip = false;
- isShowWindow = false;
- isInput = false;
- }
- }
- }
- }
- /// <summary>
- /// GUI绘制标签和窗口
- /// </summary>
- private void OnGUI()
- {
- if (tf != null)
- {
-
-
- if (isShowTip && tf.GetComponent<Outline>().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<Outline>().enabled)
- //{
- // GUI.Window(0, new Rect(x + 100, Screen.height - y + 30, 200, 180), Mywindow, "信息");
- //}
- }
- }
- if (isShowWindow && tf.GetComponent<Outline>().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);
- //}
- }
-
- }
- }
- /// <summary>
- /// 窗口绘制函数
- /// </summary>
- /// <param name="WindowID"></param>
- 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<Outline>().enabled = false;
- }
- else
- {
- //isInput = false;
- }
- }
- }
- }
- /// <summary>
- /// 更新地址
- /// </summary>
- /// <param name="name"></param>
- /// <param name="addr"></param>
- 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);
- }
- /// <summary>
- /// 同步更新状态
- /// </summary>
- /// <param name="table"></param>
- 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);
- }
-
- }
- }
- }
- }
|