Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

unity3d - Unity C# - Internal Variable Not Being Set Correctly

I am having an issue with setting a private variable. I have a PubSub pushing data into a function that sets the variable. Inside my update function, I'm activating a method that is supposed to make a button on the screen clickable. The issue is the private variable never changes and remains false. I can't understand why my variable isn't being set correctly. Thank you for helping.

using System.Collections;
using Assets.Scripts.Schemas;
using Michsky.UI.ModernUIPack;
using SuperMaxim.Messaging;
using System.Collections.Generic;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;

namespace Assets.Scripts.Views
{
    class LeagueSummonerVerificationView : MonoBehaviour
    {
        [HideInInspector] private static TMP_InputField _leagueSummonerNameInput;
        [HideInInspector] private static CustomDropdown _leagueRegionSelectDropdown;
        [HideInInspector] private GameObject _verifyLeagueSummonerButton;

        private bool _leagueButtonActivate;
        
        void Awake()
        {
            _leagueSummonerNameInput = GameObject.Find("Summoner_Name").GetComponent<TMP_InputField>();
            _leagueRegionSelectDropdown = GameObject.Find("Region_Select").GetComponent<CustomDropdown>();
            _verifyLeagueSummonerButton = GameObject.Find("Submit_Button");

            _verifyLeagueSummonerButton.GetComponent<Button>().onClick.AddListener(LeagueVerifySummonerButtonOnClick);
            _leagueSummonerNameInput.onValueChanged.AddListener(delegate { LeagueSummonerNameOnChange(); });

            Messenger.Default.Subscribe<LeagueRegionSchema>(SetupLeagueRegionSelectDropdown);
            Messenger.Default.Subscribe<SummonerNamePassCheck>(SetLeagueButtonActivated);

            _verifyLeagueSummonerButton.GetComponent<Button>().interactable = _leagueButtonActivate;
        }

        void Update()
        {
            ActivateLeagueButtonActivated();
        }

        #region Setup League Region Select Dropdown

        private static void SetupLeagueRegionSelectDropdown(LeagueRegionSchema payload)
        {
            List<CustomDropdown.Item> leagueDropdownItem = new List<CustomDropdown.Item>();

            for (int i = 0; i < payload.leagueRegions.Count; i++)
            {
                var item = new CustomDropdown.Item();
                item.itemName = payload.leagueRegions[i].ToString();
                item.itemIcon = null;
                leagueDropdownItem.Add(item);
            }

            _leagueRegionSelectDropdown.dropdownItems = leagueDropdownItem;
        }

        #endregion  

        #region Toggle LeagueSummonerButton

        void SetLeagueButtonActivated(SummonerNamePassCheck payload)
        {
            _leagueButtonActivate = payload.summonerNameSafe;
        }

        #endregion

        void ActivateLeagueButtonActivated()
        {
            if (_leagueButtonActivate)
            {
                _verifyLeagueSummonerButton.GetComponent<Button>().interactable = true;
            }
        }

        #region League Verify Summoner Button On Click

        private static void LeagueVerifySummonerButtonOnClick()
        {
            var payload = new LeagueVerificationSchema
            {
                region = _leagueRegionSelectDropdown.dropdownItems[_leagueRegionSelectDropdown.selectedItemIndex].itemName,
                summonerName = _leagueSummonerNameInput.text,
                buttonClicked = true
            };


            Messenger.Default.Publish(payload);
        }

        #endregion

        #region League Summoner Name Entered

        private static void LeagueSummonerNameOnChange()
        {
            var payload = new LeagueVerificationSchema
            {
                summonerName = _leagueSummonerNameInput.text,
            };

            Messenger.Default.Publish(payload);
        }

        #endregion
    }
}
question from:https://stackoverflow.com/questions/65924632/unity-c-sharp-internal-variable-not-being-set-correctly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...