Unity On Click Listener 在重新编译后不工作(由于脚本更改)

问题描述 投票:0回答:0

目前,当我进入播放模式时,我的代码工作得很好(为了简单起见,我在这里禁用了一些东西)但是每当我对代码进行任何更改(与按钮管理器中的任何内容无关)时,脚本都会重新编译(如预期的那样)但是我的按钮停止工作,我必须完全停止游戏并再次进入游戏模式。这导致我的工作流程非常缓慢。

我怀疑这可能是 unity 的重新编译特定问题,但我不是 100% 确定

我完全不明白的是,如果我的代码可以正常工作,那么如果我在播放模式下重新编译它,为什么它会中断

对不起,我犯了一个愚蠢的错误。我完全是自学的,把学习这些东西作为一种爱好

这是我正在使用的代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;

public class ButtonManager : MonoBehaviour
{
    [SerializeField] GameObject ButtonPrefab;
    [SerializeField] List<GameObject> ActionButtons;
    [SerializeField] GameObject ButtonHolder;
    TurnManager turnManager;
    ReticalManager reticalManager;
    MapManager mapManager;
    MoveDictionaryManager moveDictionaryManager;
    void Awake()
    {
        //Debug.Log("ButtonManager Awake!");
        setVariables();
    }
    public void setVariables()
    {
        turnManager = this.GetComponent<TurnManager>();
        reticalManager = this.GetComponent<ReticalManager>();
        mapManager = this.GetComponent<MapManager>();
        moveDictionaryManager = this.GetComponent<MoveDictionaryManager>();
    }
    public void clearButtons()
    {
        for (int i = 0; i < ActionButtons.Count; i++)
            Destroy(ActionButtons[i]);
        ActionButtons.Clear();
    }
    public void InstantiateButtons(List<String> listFromCDH)
    {
        clearButtons();
        for (int i = 0; i < listFromCDH.Count; i++)
        {
            ActionButtons.Add(Instantiate(ButtonPrefab));//Just Instanting
            //Setting Transforms
            ActionButtons[i].transform.SetParent(ButtonHolder.transform, false);
            ActionButtons[i].transform.localPosition = new Vector3(ActionButtons[i].transform.localPosition.x, -50 * i);
            //getting TMP to assign Text
            TMPro.TextMeshProUGUI TMPthis;
            TMPthis = ActionButtons[i].transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>();
            // getting cache for captured variables
            //int captured = i;//no Longer needed
            string listCDHTEXT = listFromCDH[i];//This is the cache now
            // using variables to set text
            TMPthis.text = listCDHTEXT;
            ActionButtons[i].name = listCDHTEXT + " Button";

            //Assigning On Click Functions
            Button thisButton = ActionButtons[i].GetComponent<Button>();
            thisButton.onClick.RemoveAllListeners();
            thisButton.onClick.AddListener(delegate
            {
                //moveDictionaryManager.doAction(listCDHTEXT);
                Debug.Log("Button clicked: " + listCDHTEXT);
            });
        }
    }


}

如果你需要我传递这个列表的参数

List<string> GetCharacterMoveList()
    {
        List<string> defaultMovesAvaliable;
        defaultMovesAvaliable = new List<string>();
        defaultMovesAvaliable.AddRange(new List<string>
        {
            "Move",
            "Attack",
            "FireBall",
            "End Turn"
        });
        return defaultMovesAvaliable;
    }

我曾尝试缓存“i”、“thisButton”等值,甚至创建了一个“Action”,假设它可能是 for 循环的问题,但即使我不使用 for 循环,问题似乎仍然存在

c# for-loop delegates listener unity-ui
© www.soinside.com 2019 - 2024. All rights reserved.