123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Newtonsoft.Json;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UIElements;
- /* 用于管理用户反馈界面和提交
- */
- public class UserFeedbackController : MonoBehaviour
- {
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- private DropdownField type;
- private TextField feedback;
- private Button submit, cancel;
- void OnEnable()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- type = root.Q<DropdownField>("type");
- feedback = root.Q<TextField>("feedback");
- submit = root.Q<Button>("submit");
- cancel = root.Q<Button>("cancel");
- InitSetting();
- cancel.RegisterCallback<ClickEvent>(ev => CancelClick());
- submit.RegisterCallback<ClickEvent>(ev => SubmitRequest());
- }
- // Update is called once per frame
- void Update()
- {
- if (feedback.text.Trim() == "")
- {
- submit.SetEnabled(false);
- }
- else
- {
- submit.SetEnabled(true);
- }
- }
- void InitSetting()
- {
- string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "button", "submit", EnviromentSetting.languageCode });
- submit.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "button", "cancel", EnviromentSetting.languageCode });
- cancel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "type", EnviromentSetting.languageCode });
- type.label = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "feedback", EnviromentSetting.languageCode });
- feedback.label = textValue;
- string typeOption0 = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "option_0", EnviromentSetting.languageCode });
- string typeOption1 = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "option_1", EnviromentSetting.languageCode });
- type.choices.Clear();
- type.choices.Add(typeOption0);
- type.choices.Add(typeOption1);
- type.value = typeOption0;
- }
- void CancelClick()
- {
- HomeSoundEffectController.Instance.PlaySoundEffect(2);
- var uiPlaceholder = GameObject.Find("UI Placeholder");
- if (uiPlaceholder != null)
- {
- var FeedbackUI = uiPlaceholder.transform.Find("Feedback").gameObject;
- var VoiceAndMenuUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
- FeedbackUI.SetActive(false);
- VoiceAndMenuUI.SetActive(true);
- }
- }
- void SubmitRequest()
- {
- Debug.Log("Feedback Submit request");
- string url = "/api/user/feedback/";
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("type", type.value);
- form.AddField("feedback", feedback.text);
- StartCoroutine(WebController.PostRequest(url, form, callback: SubmitCallback));
- }
- void SubmitCallback(string json)
- {
- var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
- if (data != null && data["status"] == "success")
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "message", "success", EnviromentSetting.languageCode });
- MessageBoxController.ShowMessage(msg, () => CancelClick());
- }
- else
- {
- Debug.Log(json);
- }
- }
- }
|