我正在制作游戏,我正在尝试创建一个带有3个按钮的buff插槽系统和一个带有7个按钮的buff商店。我想要做的是每次点击一个buff槽按钮,它将打开buff商店面板,当用户点击面板中的7个buff之一时,buff按钮的图像将转移到buff槽按钮。 / p>
我已经实现了所有的一切,所以当我点击第一个buff槽并选择一个buff时,buff槽的图像将变为所选的buff。但是当我点击第二个buff插槽并选择另一个buff时,它将替换两个按钮上的图像。然后第三个将对所有3个按钮执行相同操作。当我点击其他按钮时,几乎就像递归调用自身一样。如何避免按钮更改其他按钮的图像?我是Unity / C#的新手,我很感激我能得到的任何建议。
编辑:对不起!我完全忘了发布代码。那是我的错!这是我到目前为止所做的:Toast.makeText(this.context, result, Toast.LENGTH_LONG).show();
这就是方法的作用:
//This is where I initialize the buttons under Start()
buffSlot_1.GetComponent<Button>();
buffSlot_1.onClick.AddListener(() => addBuff(buffSlot_1));
buffSlot_2.GetComponent<Button>();
buffSlot_2.onClick.AddListener(() => addBuff(buffSlot_2));
buffSlot_3.GetComponent<Button>();
buffSlot_3.onClick.AddListener(() => addBuff(buffSlot_3));
谢谢!
答案 0 :(得分:0)
我不在Unity中编码,因此请考虑其中一些&#34;伪代码&#34;。但是,一般的想法应该是可行的。
更改如下:buff-shop监听器现在只在Start()中添加一次。这些相同的侦听器引用button
替换为ActiveButton
。现在单击一个buff-slot按钮只会分配活动按钮,然后打开buff-shop。 buff-shop的按钮现在每个只有一个监听器,因为新的监听器永远不会在Start()之后添加,并且每个监听器将使用ActiveButton
仅将图像分配给活动按钮而不是每个已添加的听众。
注意:在此更改之前,如果您单击一次buff-slot 100次,则每个buff-shop上最终会有100个侦听器,因为每次单击该按钮都会添加一个新的侦听器。除了它存在的其他问题之外,这基本上是内存泄漏。您应该只添加一次侦听器,或者在完成后删除它们。这里的代码只添加一次。
// Some globally visible area of your code
public Button ActiveButton { get; set; }
//This is where I initialize the buttons under Start()
void Start()
{
buffSlot_1.GetComponent<Button>();
buffSlot_1.onClick.AddListener(() => addBuff(buffSlot_1));
buffSlot_2.GetComponent<Button>();
buffSlot_2.onClick.AddListener(() => addBuff(buffSlot_2));
buffSlot_3.GetComponent<Button>();
buffSlot_3.onClick.AddListener(() => addBuff(buffSlot_3));
// Initialize buff slot listeners. These are added only once in Start()
damageBoostButton.GetComponent<Button>();
damageBoostButton.onClick.AddListener(() => {
Debug.Log("Gain a damage boost!");
dmgBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
damageBoostButton.interactable = false;
ActiveButton.image.overrideSprite = damageBoostButton.image.sprite;
});
defenseBoostButton.GetComponent<Button>();
defenseBoostButton.onClick.AddListener(() => {
Debug.Log("Gain a defense boost!");
defBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
defenseBoostButton.interactable = false;
ActiveButton.image.overrideSprite = defenseBoostButton.image.sprite;
});
critBoostButton.GetComponent<Button>();
critBoostButton.onClick.AddListener(() => {
Debug.Log("Crit Boost");
critBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
critBoostButton.interactable = false;
ActiveButton.image.overrideSprite = critBoostButton.image.sprite;
});
hasteBoostButton.GetComponent<Button>();
hasteBoostButton.onClick.AddListener(() => {
Debug.Log("Haste Boost");
hasteBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
hasteBoostButton.interactable = false;
ActiveButton.image.overrideSprite = hasteBoostButton.image.sprite;
});
iceSpikeButton.GetComponent<Button>();
iceSpikeButton.onClick.AddListener(() => {
Debug.Log("Increase Ice Spike");
iceSpikeBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
iceSpikeButton.interactable = false;
ActiveButton.image.overrideSprite = iceSpikeButton.image.sprite;
});
iceWallButton.GetComponent<Button>();
iceWallButton.onClick.AddListener(() => {
Debug.Log("Increase Ice Wall");
iceWallBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
iceWallButton.interactable = false;
ActiveButton.image.overrideSprite = iceWallButton.image.sprite;
});
healthRegenButton.GetComponent<Button>();
healthRegenButton.onClick.AddListener(() => {
Debug.Log("HPRegen Boost");
hpRegenBool = true;
buffPanel.SetActive(false);
ActiveButton.interactable = false;
healthRegenButton.interactable = false;
ActiveButton.image.overrideSprite = healthRegenButton.image.sprite;
});
}
// Clicking a buff-slot button opens the buff-shop,
// and assigns the global ActiveButton variable to the button that was clicked.
void addBuff(Button button) {
buffPanel.GetComponent<GameObject>();
buffPanel.SetActive(true);
ActiveButton = button;
}