1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | namespace KerbalEngineer.Unity { using System; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class Setting : MonoBehaviour { [SerializeField] private Text m_Label = null; [SerializeField] private Transform m_ButtonsTransform = null; [SerializeField] private GameObject m_SettingButtonPrefab = null; [SerializeField] private GameObject m_SettingTogglePrefab = null; private Action m_OnUpdate; public Button AddButton(string text, float width, UnityAction onClick) { Button button = null; if (m_SettingButtonPrefab != null) { GameObject buttonObject = Instantiate(m_SettingButtonPrefab); if (buttonObject != null) { button = buttonObject.GetComponent<Button>(); SetParentTransform(buttonObject, m_ButtonsTransform); SetWidth(buttonObject, width); SetText(buttonObject, text); SetButton(buttonObject, onClick); } } return button; } public Toggle AddToggle(string text, float width, UnityAction<bool> onValueChanged) { Toggle toggle = null; if (m_SettingTogglePrefab != null) { GameObject toggleObject = Instantiate(m_SettingTogglePrefab); if (toggleObject != null) { toggle = toggleObject.GetComponent<Toggle>(); SetParentTransform(toggleObject, m_ButtonsTransform); SetWidth(toggleObject, width); SetText(toggleObject, text); SetToggle(toggleObject, onValueChanged); } } return toggle; } public void AddUpdateHandler(Action onUpdate) { m_OnUpdate = onUpdate; } public void SetLabel(string text) { if (m_Label != null) { m_Label.text = text; } } protected virtual void Update() { m_OnUpdate?.Invoke(); } private static void SetButton(GameObject buttonObject, UnityAction onClick) { if (buttonObject != null) { Button button = buttonObject.GetComponent<Button>(); if (button != null) { button.onClick.AddListener(onClick); } } } private static void SetParentTransform(GameObject childObject, Transform parentTransform) { if (childObject != null && parentTransform != null) { childObject.transform.SetParent(parentTransform, false); } } private static void SetText(GameObject parentObject, string text) { if (parentObject != null) { Text textComponent = parentObject.GetComponentInChildren<Text>(); if (textComponent != null) { textComponent.text = text; } } } private static void SetToggle(GameObject toggleObject, UnityAction<bool> onValueChanged) { if (toggleObject != null) { Toggle toggle = toggleObject.GetComponent<Toggle>(); if (toggle != null) { toggle.onValueChanged.AddListener(onValueChanged); } } } private static void SetWidth(GameObject parentObject, float width) { if (parentObject != null) { LayoutElement layout = parentObject.GetComponent<LayoutElement>(); if (layout != null) { if (width > 0.0f) { layout.preferredWidth = width; } else { layout.flexibleWidth = 1.0f; } } } } } } |