首页 > 代码库 > 控制同一窗体的显隐(Toggle和Button)

控制同一窗体的显隐(Toggle和Button)

公共变量的脚本

using UnityEngine;using System.Collections;using UnityEngine.UI;public class CommonValues : MonoBehaviour {    public GameObject AnswerShowWin;//答案显示窗口    public Button CloseBtn;    public GameObject SubmitTog;//提交答案    void Start () {        }        void Update () {        }}

Toogle和Button控制同一窗口的显隐

using UnityEngine;using System.Collections;using UnityEngine.UI;public class ToggleControl : MonoBehaviour{    private GameObject _SubmitTog;//控制窗口的显隐的Toggle    private GameObject _AnswerShowWin;//窗口    public GameObject CommonValuesBG;//挂载公共变量的背景图片    private Button _CloseBtn;    bool flag; //获取开关的状态    bool CloseBtnFlag;//获取窗口的状态    int _ClickBtnNo;//判断是否点击的参数    void Awake()    {
//从CommonValuse脚本获取变量 _SubmitTog
= CommonValuesBG.GetComponent<CommonValues>().SubmitTog; _AnswerShowWin = CommonValuesBG.GetComponent<CommonValues>().AnswerShowWin; _CloseBtn = CommonValuesBG.GetComponent<CommonValues>().CloseBtn;
_AnswerShowWin.SetActive(
false); } void Start() { EventTriggerListener.Get(_SubmitTog).onClick += TogClick; _CloseBtn.onClick.AddListener(delegate { this.CloseBtnClick(); }); } void TogClick(GameObject tog) { flag = _SubmitTog.GetComponent<Toggle>().isOn; _AnswerShowWin.SetActive(flag); } void CloseBtnClick() { CloseBtnFlag = _AnswerShowWin.activeSelf; if (CloseBtnFlag==true) { _AnswerShowWin.SetActive(false); _SubmitTog.GetComponent<Toggle>().isOn = false; flag = false; } }}

 

控制同一窗体的显隐(Toggle和Button)