using TMPro; using UnityEngine; using MyCollections.Interfaces; public class AmountInserter : MonoBehaviour { [SerializeField] private TMP_InputField inputField; [SerializeField] private InputType inputType; [SerializeField] private int amount; [SerializeField] private int index; private GameObject receiver; // Start is called before the first frame update void Start() { inputField.lineType = TMP_InputField.LineType.SingleLine; inputField.onValueChanged.AddListener(delegate { Validate(); }); inputField.onSubmit.AddListener(delegate { Submit(); }); } // Update is called once per frame void Update() { //if (Input.GetKeyDown(KeyCode.Return)) //{ // Submit(); //} //Debug.Log("Amount: " + amount); } public void Init(GameObject obj, int limitAmount, int index) { amount = limitAmount; receiver = obj; this.index = index; } private void Submit() { if (receiver.TryGetComponent(out IReceiver<int, int> destination)) { switch (inputType) { case InputType.Numbers: if (int.TryParse(inputField.text, out int result)) { destination.Receive(result, index); Destroy(gameObject); } break; } } } private void Validate() { string text = inputField.text; if (int.TryParse(text, out int result)) { if (result > amount) { inputField.text = amount.ToString(); return; } inputField.text = result.ToString(); return; } Debug.LogError("Couldn't Parse!"); } public void ClosePanel() => Destroy(gameObject); } public enum InputType { Text, Numbers }