146 lines
3.5 KiB
C#
146 lines
3.5 KiB
C#
using Newtonsoft.Json.Serialization;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
public class ClientWorkSceneController : MonoBehaviour
|
|
{
|
|
public static ClientWorkSceneController Instance;
|
|
|
|
[Header("UI 元件")]
|
|
public TMP_InputField messageTextInputField;
|
|
public TMP_InputField messagePresentInputField;
|
|
public Button submitButton;
|
|
public ToggleGroup typeButtonGroup;
|
|
public Toggle typeWorkButton;
|
|
public Toggle typeMoneyButton;
|
|
public Toggle typeHealthButton;
|
|
public Toggle typeLoveButton;
|
|
|
|
string typeText = "typeWorkButton";
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
SetupScene();
|
|
}
|
|
|
|
void SetupScene()
|
|
{
|
|
if (submitButton != null)
|
|
{
|
|
submitButton.enabled = false;
|
|
submitButton.onClick.AddListener(submitButton_onClicked);
|
|
}
|
|
|
|
if (typeButtonGroup)
|
|
{
|
|
typeButtonGroup.allowSwitchOff = false;
|
|
foreach (var item in typeButtonGroup.GetComponentsInChildren<Toggle>())
|
|
{
|
|
item.onValueChanged.AddListener(isOn => typeToggles_onChanged(item, isOn));
|
|
typeButtonGroup.RegisterToggle(item);
|
|
}
|
|
}
|
|
|
|
if (messageTextInputField)
|
|
{
|
|
messageTextInputField.onValueChanged.AddListener(textInputField_onValueChanged);
|
|
}
|
|
|
|
if (messagePresentInputField)
|
|
{
|
|
messageTextInputField.onValueChanged.AddListener(textInputField_onValueChanged);
|
|
}
|
|
|
|
Debug.Log("ClientWorkScene 初始化完成");
|
|
}
|
|
|
|
private void textInputField_onValueChanged(string arg0)
|
|
{
|
|
checkMessageForSubmitButtonEnable();
|
|
}
|
|
|
|
private void typeToggles_onChanged(Toggle elem, bool isOn)
|
|
{
|
|
typeText = elem.name;
|
|
checkMessageForSubmitButtonEnable();
|
|
}
|
|
|
|
private void checkMessageForSubmitButtonEnable()
|
|
{
|
|
if (messageTextInputField.text.Length == 0)
|
|
{
|
|
submitButton.enabled = false;
|
|
return;
|
|
}
|
|
|
|
if (messagePresentInputField.text.Length == 0)
|
|
{
|
|
submitButton.enabled = false;
|
|
return;
|
|
}
|
|
|
|
if (!typeButtonGroup.AnyTogglesOn())
|
|
{
|
|
submitButton.enabled = false;
|
|
return;
|
|
}
|
|
submitButton.enabled = true;
|
|
}
|
|
|
|
private void submitButton_onClicked()
|
|
{
|
|
|
|
|
|
//foreach (var item in typeButtonGroup.GetComponentsInChildren<Toggle>())
|
|
//{
|
|
// if (item.isOn)
|
|
// {
|
|
// typeText = item.name;
|
|
// //break;
|
|
// }
|
|
//}
|
|
|
|
GameMessageWork payloadObj = new GameMessageWork
|
|
{
|
|
count = ClientWorkMessageHandleSceneController.messageResultCount,
|
|
message = messageTextInputField.text,
|
|
present = messagePresentInputField.text,
|
|
type = typeText
|
|
};
|
|
string payloadStr = JsonUtility.ToJson(payloadObj);
|
|
|
|
if (ClientMessageHandler.Instance != null)
|
|
{
|
|
ClientMessageHandler.Instance.SendMessageToServer("workMessage", payloadStr);
|
|
StartCoroutine(LoadNextScene());
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Send Failed: workMessage " + payloadStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public IEnumerator LoadNextScene()
|
|
{
|
|
yield return new WaitForSeconds(0f);
|
|
SceneManager.LoadScene("ClientWorkMessageHandleScene");
|
|
|
|
}
|
|
|
|
|
|
} |