| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.UI; |
| | 3 | |
|
| | 4 | | [RequireComponent(typeof(Toggle))] |
| | 5 | | public class ToggleActiveGameObject : MonoBehaviour |
| | 6 | | { |
| | 7 | | [SerializeField] GameObject activeOn = null; |
| | 8 | | [SerializeField] GameObject activeOff = null; |
| | 9 | |
|
| | 10 | | private Toggle targetToggle; |
| | 11 | |
|
| | 12 | | private void Awake() |
| | 13 | | { |
| 32 | 14 | | targetToggle = GetComponent<Toggle>(); |
| 32 | 15 | | targetToggle.onValueChanged.AddListener(UpdateState); |
| 32 | 16 | | } |
| | 17 | |
|
| 14 | 18 | | protected void Start() { UpdateState(targetToggle.isOn); } |
| | 19 | |
|
| | 20 | | private void UpdateState(bool isOn) |
| | 21 | | { |
| 15 | 22 | | if (activeOn) |
| 15 | 23 | | activeOn.gameObject.SetActive(isOn); |
| 15 | 24 | | if (activeOff) |
| 15 | 25 | | activeOff.gameObject.SetActive(!isOn); |
| 15 | 26 | | } |
| | 27 | | } |