using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using System.IO; using Newtonsoft.Json.Linq; public class UI_AutoDisCussWindow : MonoBehaviour { /// /// 窗体框单例 /// private static UI_AutoDisCussWindow pInit = null; /// /// /// /// public InputField InputField_FileName; /// /// /// public InputField InputField_simulatorUID; /// /// /// public InputField InputField_simulatorNickName; public Text resultText; /// /// 读取按钮 /// public GameObject ReadButton; /// /// 保存按钮 /// public GameObject SaveButton; /// /// 按钮 /// public GameObject CloseButton; /// /// 清空按钮 /// public GameObject ClearButton; /// /// /// public RectTransform mScrollRectContent; /// /// /// public Text mContentText; /// /// 获取单键 /// /// 对象 public static UI_AutoDisCussWindow Instance() { return pInit; } /// /// 初始化 /// private void Awake() { //// 初始化 pInit = this; } private void Start() { mScrollRectContent = this.transform.Find("Panel/Scroll View/Viewport/Content").GetComponent(); ////mPrefab = this.transform.Find("Panel/Scroll View/Viewport/Content/prefab").gameObject; ////mPrefab.SetActive(false); mContentText = this.transform.Find("Panel/Scroll View/Viewport/Content/Text").GetComponent(); mContentText.text = string.Empty; InputField_FileName = this.transform.Find("Panel/InputField_FileName").GetComponent(); InputField_simulatorUID = this.transform.Find("Panel/InputField_simulatorUID").GetComponent(); InputField_simulatorNickName = this.transform.Find("Panel/InputField_simulatorNickName").GetComponent(); SaveButton = this.transform.Find("Panel/SaveButton").gameObject; CloseButton = this.transform.Find("Panel/CloseButton").gameObject; ReadButton = this.transform.Find("Panel/ReadButton").gameObject; ClearButton = this.transform.Find("Panel/ClearButton").gameObject; resultText = this.transform.Find("Panel/resultText").GetComponent(); resultText.text = string.Empty; EventTriggerListener.Get(ReadButton).onClick = OnClicked_Createcontent; EventTriggerListener.Get(CloseButton).onClick = OnClicked_CloseButton; EventTriggerListener.Get(ClearButton).onClick = OnClicked_ClearButton; EventTriggerListener.Get(SaveButton).onClick = OnClicked_SaveButton; } private void OnClicked_ClearButton(GameObject btn) { str_info = string.Empty; InputField_simulatorUID.text = string.Empty; InputField_simulatorNickName.text = string.Empty; //////if (mScrollRectContent.childCount > 0) //////{ ////// for (int i = mScrollRectContent.childCount - 1; i >= 0; i--) ////// { ////// Transform go = mScrollRectContent.GetChild(i); ////// if (go.name.Contains("message")) ////// { ////// Destroy(go.gameObject); ////// } ////// } //////} mContentText.text = string.Empty; } private string str_info = string.Empty; private void OnClicked_Createcontent(GameObject go) { if (string.IsNullOrEmpty(InputField_simulatorUID.text)) return; if (string.IsNullOrEmpty(InputField_simulatorNickName.text)) return; str_info = string.Empty; string readFilePath = Application.dataPath + "/Resources/" + InputField_FileName.text + ".txt"; if (!File.Exists(readFilePath)) { Debug.LogError(readFilePath); return; } TextAsset headFile = (TextAsset)Resources.Load(InputField_FileName.text); string[] recordArray = headFile.text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string msg in recordArray) { string msg11111 = msg.Trim().Replace("\r", string.Empty); msg11111 = msg.Replace(" ", "..."); if (string.IsNullOrEmpty(msg11111)) continue; string modelId = msg11111.Substring(0, 6); string content = msg11111.Substring(6); ////GameObject tempOne = GameObject.Instantiate(mPrefab); ////tempOne.transform.SetParent(mScrollRectContent); ////tempOne.transform.localScale = Vector3.one; ////tempOne.transform.localPosition = Vector3.zero; ////tempOne.name = "message_"; ////tempOne.SetActive(true); ////Text value_nickText = tempOne.transform.Find("Text").GetComponent(); int ts = (int)UserProxy.Instance.GetCurrentUnixTimeStamp(); string sss = "HSET \"gamerun-herodiscuss-"; sss += modelId + "-item\" "; sss += "\"" + InputField_simulatorUID.text + "\" "; tempDis aaaa = new tempDis(); aaaa.uid = InputField_simulatorUID.text; aaaa.name = InputField_simulatorNickName.text; aaaa.msg = content.Replace("\r", string.Empty); aaaa.ts = ts; sss += JsonUtility.ToJson(aaaa); mContentText.text += "" + sss + "\n"; sss += "\r"; //////ZADD "gamerun-herodiscuss-101013-score" 1520315307 "gg0006" string score = string.Empty; score += "ZADD \"gamerun-herodiscuss-" + modelId + "-score\""; score += " " + ts + " "; score += "\"" + InputField_simulatorUID.text + "\""; mContentText.text += "" + score + "\n"; score += "\r"; //////value_nickText.text = sss; str_info += sss + score; } mContentText.text += "\nOver"; mContentText.rectTransform.sizeDelta = new Vector2(1000,mContentText.preferredHeight+100); mScrollRectContent.sizeDelta = mContentText.rectTransform.sizeDelta; ////GameObject end = GameObject.Instantiate(mPrefab); ////end.transform.SetParent(mScrollRectContent); ////end.transform.localScale = Vector3.one; ////end.transform.localPosition = Vector3.zero; ////end.name = "message_"; ////end.SetActive(true); ////Text txt = end.transform.Find("Text").GetComponent(); ////txt.text ="Over"; } /// /// 发送消息 /// /// 点击对象 public void OnClicked_SaveButton(GameObject go) { if (string.IsNullOrEmpty(str_info)) return; if (string.IsNullOrEmpty(InputField_simulatorUID.text)) return; //写入数据到 discuss文本 string saveFilePath = Application.dataPath + "/Resources/" + InputField_simulatorUID.text + ".txt"; StreamWriter sw; if (!File.Exists(saveFilePath)) { sw = File.CreateText(saveFilePath);//创建一个用于写入 UTF-8 编码的文本 Debug.Log("文件创建成功!"); } else { sw = File.AppendText(saveFilePath);//打开现有 UTF-8 编码文本文件以进行读取 } sw.WriteLine(str_info);//以行为单位写入字符串 sw.Close(); sw.Dispose();//文件流释放 resultText.text = "保存成功"; } public class tempDis { /// /// 评论的UID /// public string uid; public string name; public string msg; public int ts; /// /// 点赞次数 /// public int praise; public string[] praised_uids = new string[0]; } public void OnClicked_CloseButton(GameObject go) { PanelHelper.Instance.ClosePanel("UI_AutoDisCussWindow"); } }