43 lines
		
	
	
		
			854 B
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			43 lines
		
	
	
		
			854 B
		
	
	
	
		
			C#
		
	
	
	
|  | using System; | |||
|  | using System.Collections.Generic; | |||
|  | using UnityEngine; | |||
|  | using UnityEngine.UI; | |||
|  | 
 | |||
|  | [RequireComponent(typeof(Button))] | |||
|  | public class UIToggleSwap : MonoBehaviour | |||
|  | { | |||
|  |     public event Action<bool> OnSwitch; | |||
|  | 
 | |||
|  |     public bool IsOn | |||
|  |     { | |||
|  |         get | |||
|  |         { | |||
|  |             return mIsOn; | |||
|  |         } | |||
|  |         set | |||
|  |         { | |||
|  |             mIsOn = value; | |||
|  |             if (mSignOn != null) mSignOn.SetActive(mIsOn); | |||
|  |             if (mSignOff != null) mSignOff.SetActive(!mIsOn); | |||
|  | 
 | |||
|  |             OnSwitch?.Invoke(mIsOn); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     [SerializeField] GameObject mSignOn; | |||
|  |     [SerializeField] GameObject mSignOff; | |||
|  | 
 | |||
|  |     private Button mBtnClick; | |||
|  |     private bool mIsOn; | |||
|  | 
 | |||
|  |     private void Awake() | |||
|  |     { | |||
|  |         mBtnClick = GetComponent<Button>(); | |||
|  |         mBtnClick.onClick.AddListener(OnClick); | |||
|  |     } | |||
|  | 
 | |||
|  |     private void OnClick() | |||
|  |     { | |||
|  |         IsOn = !IsOn; | |||
|  |     } | |||
|  | } |