Isoutline.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using MySql.Data.MySqlClient;
  5. using System;
  6. using System.Data;
  7. using test;
  8. using System.Threading;
  9. namespace cakeslice
  10. {
  11. public class Isoutline : MonoBehaviour
  12. {
  13. static string connstr = "Server=127.0.0.1;Database=test;user ID=root;password=123456;port=3306;";
  14. static string sql = "select * from user";
  15. public string addr = "";
  16. static Rect RectWnd;
  17. SqlBase sqlbase;
  18. DataSet ds = new DataSet();
  19. DataTable table = null;
  20. Transform tf;
  21. public bool isShowTip;//是否显示标签
  22. public bool isShowWindow;//是否显示窗口
  23. public bool isInput;//是否输入
  24. public bool isClick=false;
  25. float x = 0, y = 0;
  26. // Start is called before the first frame update
  27. void Start()
  28. {
  29. sqlbase = new SqlBase(connstr);
  30. ds = sqlbase.GetDataSet(sql);
  31. table = ds.Tables[0];
  32. GetComponent<Outline>().enabled = false;// !GetComponent<Outline>().enabled;
  33. isShowTip = false;
  34. isShowWindow = false;
  35. isInput = false;
  36. //isClick = false;
  37. }
  38. // Update is called once per frame
  39. void Update()
  40. {
  41. if (ds != null)
  42. {
  43. if (isInput == false)
  44. {
  45. ds = sqlbase.GetDataSet(sql);
  46. table = ds.Tables[0];
  47. if(table!=null)
  48. OnorOff(table);
  49. GetObject();
  50. }
  51. RectWnd.x += 10;
  52. }
  53. }
  54. /// <summary>
  55. /// 鼠标点击获的物体
  56. /// </summary>
  57. void GetObject()
  58. {
  59. GetComponent<Outline>().enabled = false;
  60. if (Input.GetMouseButtonDown(0))
  61. {
  62. x = Input.mousePosition.x;
  63. y = Input.mousePosition.y;
  64. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  65. Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow);
  66. RaycastHit hit;
  67. if (Physics.Raycast(ray, out hit))//&&hit.collider.tag== "LeftSwitch"|| hit.collider.tag == "RightSwitch")
  68. {
  69. tf = hit.transform;
  70. if (tf == transform)
  71. {
  72. tf.GetComponent<Outline>().enabled = true;// !GetComponent<Outline>().enabled;
  73. isShowTip = true;
  74. isShowWindow = true;
  75. isInput = true;
  76. }
  77. else
  78. {
  79. GetComponent<Outline>().enabled = false;// !GetComponent<Outline>().enabled;
  80. isShowTip = false;
  81. isShowWindow = false;
  82. isInput = false;
  83. }
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// GUI绘制标签和窗口
  89. /// </summary>
  90. private void OnGUI()
  91. {
  92. if (tf != null)
  93. {
  94. if (isShowTip && tf.GetComponent<Outline>().enabled)
  95. {
  96. GUIStyle style = new GUIStyle();
  97. style.fontSize = 30;
  98. style.normal.textColor = Color.red;
  99. Rect rect = new Rect(x, Screen.height - y, 400, 50);
  100. if (tf != null)
  101. {
  102. GUI.Label(rect, tf.name, style);
  103. //if (isShowWindow && tf.GetComponent<Outline>().enabled)
  104. //{
  105. // GUI.Window(0, new Rect(x + 100, Screen.height - y + 30, 200, 180), Mywindow, "信息");
  106. //}
  107. }
  108. }
  109. if (isShowWindow && tf.GetComponent<Outline>().enabled)
  110. {
  111. //for (int i = 0; i < 100; i += 10)
  112. //{
  113. RectWnd = GUI.Window(0, new Rect(x + 100, Screen.height - y + 30, 200, 180), Mywindow, "信息");
  114. //Thread.Sleep(1000);
  115. //}
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 窗口绘制函数
  121. /// </summary>
  122. /// <param name="WindowID"></param>
  123. void Mywindow(int WindowID)
  124. {
  125. for (int i = 0; i < table.Rows.Count; i++)
  126. {
  127. if (tf.name == table.Rows[i][1].ToString())
  128. {
  129. GUILayout.Label("ID:" + table.Rows[i][0].ToString());
  130. GUILayout.Label("名称:" + table.Rows[i][1].ToString());
  131. GUILayout.Label("年龄:" + table.Rows[i][2].ToString());
  132. GUILayout.Label("状态:" + table.Rows[i][3].ToString());
  133. GUILayout.Label("地址: " + table.Rows[i][4].ToString());
  134. addr = GUI.TextField(new Rect(45, 120, 130, 20), addr);
  135. if (GUILayout.Button("OK"))
  136. {
  137. isShowTip = false;
  138. isShowWindow = false;
  139. isInput = false;
  140. isClick = false;
  141. if (addr != "")
  142. {
  143. UpdateAddr(tf.name, addr);
  144. }
  145. GetComponent<Outline>().enabled = false;
  146. }
  147. else
  148. {
  149. //isInput = false;
  150. }
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 更新地址
  156. /// </summary>
  157. /// <param name="name"></param>
  158. /// <param name="addr"></param>
  159. void UpdateAddr(string name, string addr)
  160. {
  161. int infoaddr = int.Parse(addr);
  162. string updatasql = "update user set infoaddr=" + infoaddr + " where na='" + name + "';";
  163. print(updatasql);
  164. int r = sqlbase.commandExecute(updatasql);
  165. print(r);
  166. if (r > 0)
  167. {
  168. Debug.Log("修改成功");
  169. }
  170. else
  171. {
  172. Debug.Log("修改失败");
  173. }
  174. ds = sqlbase.GetDataSet(sql);
  175. }
  176. /// <summary>
  177. /// 同步更新状态
  178. /// </summary>
  179. /// <param name="table"></param>
  180. public void OnorOff(DataTable table)
  181. {
  182. var Switchs = GameObject.FindGameObjectsWithTag("Switch");
  183. for (int i = 0; i < table.Rows.Count; i++)
  184. {
  185. string name = table.Rows[i][1].ToString();
  186. string state = table.Rows[i][3].ToString();
  187. string rota = table.Rows[i][5].ToString();
  188. Transform tr = Switchs[i].transform;
  189. if (name==tr.name)
  190. {
  191. switch (state, rota)
  192. {
  193. case ("on", "0"): tr.rotation = Quaternion.Euler(-90, 90, 0); break;
  194. case ("on", "1"): tr.rotation = Quaternion.Euler(-90, -90, 0); break;
  195. case ("off", "0"): tr.rotation = Quaternion.Euler(-90, 0, 0); break;
  196. case ("off", "1"): tr.rotation = Quaternion.Euler(-90, 0, 0); break;
  197. }
  198. Debug.Log("name=" + name + "state=" + state);
  199. }
  200. }
  201. }
  202. }
  203. }