UI_AutoDisCussWindow.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using System.IO;
  7. using Newtonsoft.Json.Linq;
  8. public class UI_AutoDisCussWindow : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 窗体框单例
  12. /// </summary>
  13. private static UI_AutoDisCussWindow pInit = null;
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. ///
  18. public InputField InputField_FileName;
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public InputField InputField_simulatorUID;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public InputField InputField_simulatorNickName;
  27. public Text resultText;
  28. /// <summary>
  29. /// 读取按钮
  30. /// </summary>
  31. public GameObject ReadButton;
  32. /// <summary>
  33. /// 保存按钮
  34. /// </summary>
  35. public GameObject SaveButton;
  36. /// <summary>
  37. /// 按钮
  38. /// </summary>
  39. public GameObject CloseButton;
  40. /// <summary>
  41. /// 清空按钮
  42. /// </summary>
  43. public GameObject ClearButton;
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public RectTransform mScrollRectContent;
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. public Text mContentText;
  52. /// <summary>
  53. /// 获取单键
  54. /// </summary>
  55. /// <returns>对象</returns>
  56. public static UI_AutoDisCussWindow Instance()
  57. {
  58. return pInit;
  59. }
  60. /// <summary>
  61. /// 初始化
  62. /// </summary>
  63. private void Awake()
  64. {
  65. //// 初始化
  66. pInit = this;
  67. }
  68. private void Start()
  69. {
  70. mScrollRectContent = this.transform.Find("Panel/Scroll View/Viewport/Content").GetComponent<RectTransform>();
  71. ////mPrefab = this.transform.Find("Panel/Scroll View/Viewport/Content/prefab").gameObject;
  72. ////mPrefab.SetActive(false);
  73. mContentText = this.transform.Find("Panel/Scroll View/Viewport/Content/Text").GetComponent<Text>();
  74. mContentText.text = string.Empty;
  75. InputField_FileName = this.transform.Find("Panel/InputField_FileName").GetComponent<InputField>();
  76. InputField_simulatorUID = this.transform.Find("Panel/InputField_simulatorUID").GetComponent<InputField>();
  77. InputField_simulatorNickName = this.transform.Find("Panel/InputField_simulatorNickName").GetComponent<InputField>();
  78. SaveButton = this.transform.Find("Panel/SaveButton").gameObject;
  79. CloseButton = this.transform.Find("Panel/CloseButton").gameObject;
  80. ReadButton = this.transform.Find("Panel/ReadButton").gameObject;
  81. ClearButton = this.transform.Find("Panel/ClearButton").gameObject;
  82. resultText = this.transform.Find("Panel/resultText").GetComponent<Text>();
  83. resultText.text = string.Empty;
  84. EventTriggerListener.Get(ReadButton).onClick = OnClicked_Createcontent;
  85. EventTriggerListener.Get(CloseButton).onClick = OnClicked_CloseButton;
  86. EventTriggerListener.Get(ClearButton).onClick = OnClicked_ClearButton;
  87. EventTriggerListener.Get(SaveButton).onClick = OnClicked_SaveButton;
  88. }
  89. private void OnClicked_ClearButton(GameObject btn)
  90. {
  91. str_info = string.Empty;
  92. InputField_simulatorUID.text = string.Empty;
  93. InputField_simulatorNickName.text = string.Empty;
  94. //////if (mScrollRectContent.childCount > 0)
  95. //////{
  96. ////// for (int i = mScrollRectContent.childCount - 1; i >= 0; i--)
  97. ////// {
  98. ////// Transform go = mScrollRectContent.GetChild(i);
  99. ////// if (go.name.Contains("message"))
  100. ////// {
  101. ////// Destroy(go.gameObject);
  102. ////// }
  103. ////// }
  104. //////}
  105. mContentText.text = string.Empty;
  106. }
  107. private string str_info = string.Empty;
  108. private void OnClicked_Createcontent(GameObject go)
  109. {
  110. if (string.IsNullOrEmpty(InputField_simulatorUID.text))
  111. return;
  112. if (string.IsNullOrEmpty(InputField_simulatorNickName.text))
  113. return;
  114. str_info = string.Empty;
  115. string readFilePath = Application.dataPath + "/Resources/" + InputField_FileName.text + ".txt";
  116. if (!File.Exists(readFilePath))
  117. {
  118. Debug.LogError(readFilePath);
  119. return;
  120. }
  121. TextAsset headFile = (TextAsset)Resources.Load(InputField_FileName.text);
  122. string[] recordArray = headFile.text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
  123. foreach (string msg in recordArray)
  124. {
  125. string msg11111 = msg.Trim().Replace("\r", string.Empty);
  126. msg11111 = msg.Replace(" ", "...");
  127. if (string.IsNullOrEmpty(msg11111))
  128. continue;
  129. string modelId = msg11111.Substring(0, 6);
  130. string content = msg11111.Substring(6);
  131. ////GameObject tempOne = GameObject.Instantiate(mPrefab);
  132. ////tempOne.transform.SetParent(mScrollRectContent);
  133. ////tempOne.transform.localScale = Vector3.one;
  134. ////tempOne.transform.localPosition = Vector3.zero;
  135. ////tempOne.name = "message_";
  136. ////tempOne.SetActive(true);
  137. ////Text value_nickText = tempOne.transform.Find("Text").GetComponent<Text>();
  138. int ts = (int)UserProxy.Instance.GetCurrentUnixTimeStamp();
  139. string sss = "HSET \"gamerun-herodiscuss-";
  140. sss += modelId + "-item\" ";
  141. sss += "\"" + InputField_simulatorUID.text + "\" ";
  142. tempDis aaaa = new tempDis();
  143. aaaa.uid = InputField_simulatorUID.text;
  144. aaaa.name = InputField_simulatorNickName.text;
  145. aaaa.msg = content.Replace("\r", string.Empty);
  146. aaaa.ts = ts;
  147. sss += JsonUtility.ToJson(aaaa);
  148. mContentText.text += "<color=#f0F000>" + sss + "</color>\n";
  149. sss += "\r";
  150. //////ZADD "gamerun-herodiscuss-101013-score" 1520315307 "gg0006"
  151. string score = string.Empty;
  152. score += "ZADD \"gamerun-herodiscuss-" + modelId + "-score\"";
  153. score += " " + ts + " ";
  154. score += "\"" + InputField_simulatorUID.text + "\"";
  155. mContentText.text += "<color=#0fFF00>" + score + "</color>\n";
  156. score += "\r";
  157. //////value_nickText.text = sss;
  158. str_info += sss + score;
  159. }
  160. mContentText.text += "\n<color=#00FF00>Over</color>";
  161. mContentText.rectTransform.sizeDelta = new Vector2(1000,mContentText.preferredHeight+100);
  162. mScrollRectContent.sizeDelta = mContentText.rectTransform.sizeDelta;
  163. ////GameObject end = GameObject.Instantiate(mPrefab);
  164. ////end.transform.SetParent(mScrollRectContent);
  165. ////end.transform.localScale = Vector3.one;
  166. ////end.transform.localPosition = Vector3.zero;
  167. ////end.name = "message_";
  168. ////end.SetActive(true);
  169. ////Text txt = end.transform.Find("Text").GetComponent<Text>();
  170. ////txt.text ="<color=#00FF00>Over</color>";
  171. }
  172. /// <summary>
  173. /// 发送消息
  174. /// </summary>
  175. /// <param name="go">点击对象</param>
  176. public void OnClicked_SaveButton(GameObject go)
  177. {
  178. if (string.IsNullOrEmpty(str_info))
  179. return;
  180. if (string.IsNullOrEmpty(InputField_simulatorUID.text))
  181. return;
  182. //写入数据到 discuss文本
  183. string saveFilePath = Application.dataPath + "/Resources/" + InputField_simulatorUID.text + ".txt";
  184. StreamWriter sw;
  185. if (!File.Exists(saveFilePath))
  186. {
  187. sw = File.CreateText(saveFilePath);//创建一个用于写入 UTF-8 编码的文本
  188. Debug.Log("文件创建成功!");
  189. }
  190. else
  191. {
  192. sw = File.AppendText(saveFilePath);//打开现有 UTF-8 编码文本文件以进行读取
  193. }
  194. sw.WriteLine(str_info);//以行为单位写入字符串
  195. sw.Close();
  196. sw.Dispose();//文件流释放
  197. resultText.text = "保存成功";
  198. }
  199. public class tempDis
  200. {
  201. /// <summary>
  202. /// 评论的UID
  203. /// </summary>
  204. public string uid;
  205. public string name;
  206. public string msg;
  207. public int ts;
  208. /// <summary>
  209. /// 点赞次数
  210. /// </summary>
  211. public int praise;
  212. public string[] praised_uids = new string[0];
  213. }
  214. public void OnClicked_CloseButton(GameObject go)
  215. {
  216. PanelHelper.Instance.ClosePanel("UI_AutoDisCussWindow");
  217. }
  218. }