亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用下拉 Unity 隱藏/顯示面板

使用下拉 Unity 隱藏/顯示面板

C#
MMMHUHU 2022-12-24 09:40:39
我剛剛開始了一些基本游戲內容的統一項目。這可能是錯誤的問題或無效的過程。我已經在單擊按鈕時隱藏/顯示面板,現在我想在下拉列表的值更改后隱藏/顯示。我有兩個面板,一個用于基本信息,另一個用于安全信息。在選擇下拉值后,我想顯示其中一個面板并隱藏第二個面板。但我不知道如何實現這一目標。我正在嘗試一些基本邏輯并堅持下去。我做了什么:using UnityEngine;using UnityEngine.UI;using UnityEngine.Events;using System.Collections;public class WithrowModalPanel : MonoBehaviour{    public Button cancelButton;    public GameObject modalPanelObject;    public GameObject modalPanelObjectAdvance;    public Dropdown myDropdown;    private static WithrowModalPanel modalPanel;    public static WithrowModalPanel Instance()    {        if (!modalPanel)        {            modalPanel = FindObjectOfType(typeof(WithrowModalPanel)) as WithrowModalPanel;            if (!modalPanel)                Debug.LogError("There needs to be one active ModalPanel script on a GameObject in your scene.");        }        return modalPanel;    }    void Update()    {        switch (myDropdown.value)        {            case 1:                Debug.Log("Basic panel!");                modalPanelObject.SetActive(true);                modalPanelObjectAdvance.SetActive(false);                break;            case 2:                Debug.Log("Advance panel!");                modalPanelObjectAdvance.SetActive(true);                modalPanelObject.SetActive(false);                break;        }    }}我只是盯著 unity 看,對它的結構并沒有太多的了解。
查看完整描述

1 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

請注意,這Dropdown.value是 0 基索引,因此第一個條目0不是1. 我不知道您的完整設置,但我想這是您嘗試的主要問題。

然后 Dropdowns 有一個事件onValueChanged而不是在Update你應該注冊一個監聽器

private void Start()

{

    // Just to be sure it is always only added once

    // I have the habit to remove before adding a listener

    // This is valid even if the listener was not added yet

    myDropdown.onValueChanged.RemoveListener(HandleValueChanged);

    myDropdown.onValueChanged.AddListener(HandleValueChanged);

}


private void OnDestroy()

{

    // To avoid errors also remove listeners as soon as they

    // are not needed anymore

    // Otherwise in the case this object is destroyed but the dropdown is not

    // it would still try to call your listener -> Exception

    myDropdown.onValueChanged.RemoveListener(HandleValueChanged);

}


private void HandleValueChanged(int newValue)

{

    switch (newValue)

    {

        case 0:

            Debug.Log("Basic panel!");

            modalPanelObject.SetActive(true);

            modalPanelObjectAdvance.SetActive(false);

            break;


        case 1:

            Debug.Log("Advance panel!");

            modalPanelObjectAdvance.SetActive(true);

            modalPanelObject.SetActive(false);

            break;

    }

}

提示:你可以使用通用的FindObjectOfType


modalPanel = FindObjectOfType<WithrowModalPanel>();


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 282 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號