| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Events; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class AttributeXYZ : MonoBehaviour |
| | 9 | | { |
| | 10 | | public TMP_InputField xField; |
| | 11 | | public Image xTextBoxImage; |
| | 12 | | public TMP_InputField yField; |
| | 13 | | public Image yTextBoxImage; |
| | 14 | | public TMP_InputField zField; |
| | 15 | | public Image zTextBoxImage; |
| | 16 | |
|
| | 17 | | public event Action<Vector3> OnChanged; |
| | 18 | |
|
| | 19 | | Vector3 currentValue; |
| | 20 | |
|
| | 21 | | bool isSelected = false; |
| | 22 | |
|
| | 23 | | private void Awake() |
| | 24 | | { |
| 66 | 25 | | ConfigureField(xField, xTextBoxImage, ChangeXValue); |
| 66 | 26 | | ConfigureField(yField, yTextBoxImage, ChangeYValue); |
| 66 | 27 | | ConfigureField(zField, zTextBoxImage, ChangeZValue); |
| 66 | 28 | | } |
| | 29 | |
|
| | 30 | | private void OnDestroy() |
| | 31 | | { |
| 66 | 32 | | UnsubscribeField(xField); |
| 66 | 33 | | UnsubscribeField(yField); |
| 66 | 34 | | UnsubscribeField(zField); |
| 66 | 35 | | } |
| | 36 | |
|
| | 37 | | private void ConfigureField(TMP_InputField field, Image textBoxImage, UnityAction<string> onChangeAction) |
| | 38 | | { |
| 198 | 39 | | if (field != null) |
| | 40 | | { |
| 198 | 41 | | field.onValueChanged.AddListener(onChangeAction); |
| | 42 | |
|
| 198 | 43 | | field.onSelect.AddListener((currentText) => |
| | 44 | | { |
| 0 | 45 | | InputSelected(currentText); |
| 0 | 46 | | SetTextboxActive(textBoxImage, true); |
| 0 | 47 | | }); |
| | 48 | |
|
| 198 | 49 | | field.onEndEdit.AddListener((newText) => |
| | 50 | | { |
| 0 | 51 | | SetTextboxActive(textBoxImage, false); |
| 0 | 52 | | InputDeselected(newText); |
| | 53 | |
|
| 0 | 54 | | if (EventSystem.current != null && !EventSystem.current.alreadySelecting) |
| 0 | 55 | | EventSystem.current.SetSelectedGameObject(null); |
| 0 | 56 | | }); |
| | 57 | |
|
| 198 | 58 | | field.onSubmit.AddListener((newText) => EventSystem.current?.SetSelectedGameObject(null)); |
| | 59 | | } |
| | 60 | |
|
| 198 | 61 | | SetTextboxActive(textBoxImage, false); |
| 198 | 62 | | } |
| | 63 | |
|
| | 64 | | private void UnsubscribeField(TMP_InputField field) |
| | 65 | | { |
| 198 | 66 | | if (field != null) |
| | 67 | | { |
| 198 | 68 | | field.onValueChanged.RemoveAllListeners(); |
| 198 | 69 | | field.onSelect.RemoveAllListeners(); |
| 198 | 70 | | field.onEndEdit.RemoveAllListeners(); |
| 198 | 71 | | field.onSubmit.RemoveAllListeners(); |
| | 72 | | } |
| 198 | 73 | | } |
| | 74 | |
|
| | 75 | | public void SetValues(Vector3 value) |
| | 76 | | { |
| 0 | 77 | | if (isSelected) |
| 0 | 78 | | return; |
| | 79 | |
|
| 0 | 80 | | currentValue = value; |
| 0 | 81 | | xField.SetTextWithoutNotify(value.x.ToString("0.##")); |
| 0 | 82 | | yField.SetTextWithoutNotify(value.y.ToString("0.##")); |
| 0 | 83 | | zField.SetTextWithoutNotify(value.z.ToString("0.##")); |
| | 84 | |
|
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void ChangeXValue(string value) |
| | 88 | | { |
| 0 | 89 | | if (!isSelected || string.IsNullOrEmpty(value)) |
| 0 | 90 | | return; |
| | 91 | |
|
| 0 | 92 | | value = value.Replace(".", ","); |
| 0 | 93 | | if (float.TryParse(value, out currentValue.x)) |
| 0 | 94 | | OnChanged?.Invoke(currentValue); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void ChangeYValue(string value) |
| | 98 | | { |
| 0 | 99 | | if (!isSelected || string.IsNullOrEmpty(value)) |
| 0 | 100 | | return; |
| | 101 | |
|
| 0 | 102 | | value = value.Replace(".", ","); |
| 0 | 103 | | if (float.TryParse(value, out currentValue.y)) |
| 0 | 104 | | OnChanged?.Invoke(currentValue); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public void ChangeZValue(string value) |
| | 108 | | { |
| 0 | 109 | | if (!isSelected || string.IsNullOrEmpty(value)) |
| 0 | 110 | | return; |
| | 111 | |
|
| 0 | 112 | | value = value.Replace(".", ","); |
| 0 | 113 | | if (float.TryParse(value, out currentValue.z)) |
| 0 | 114 | | OnChanged?.Invoke(currentValue); |
| 0 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | public void InputSelected(string text) { isSelected = true; } |
| | 118 | |
|
| 0 | 119 | | public void InputDeselected(string text) { isSelected = false; } |
| | 120 | |
|
| | 121 | | private void SetTextboxActive(Image textBoxImage, bool isActive) |
| | 122 | | { |
| 198 | 123 | | if (textBoxImage == null) |
| 0 | 124 | | return; |
| | 125 | |
|
| 198 | 126 | | textBoxImage.enabled = isActive; |
| 198 | 127 | | } |
| | 128 | | } |