gpt4 book ai didi

c# - 无法解释的变量增加

转载 作者:行者123 更新时间:2023-11-30 19:55:27 24 4
gpt4 key购买 nike

在我用来更改按钮设置的循环之一中,我还使用了 AddListener 函数,而不是 Inspector 中的函数。我有 5 个项目,给出了 0-4 的“i”范围,但是当我通过它应该调用的函数打印“i”时,无论我按什么按钮,它总是记录 5,这很奇怪,因为“i”从来没有达到 5。有什么想法吗?

附言我使用 CustomEditor 在检查器中显示 2 个按钮“预览布局”和“删除预览”。

代码:

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

public class RateMeManager : MonoBehaviour {

public GameObject rateMeCanvas;
public Sprite emptyStar, fullStar, button;
public float spriteWidth, spriteHeight, spritePadding;

[HideInInspector]
public GameObject currentCanvas, tempButton;

void Start () {
RemovePreview();
GenerateStars();
}

// Update is called once per frame
public void GenerateStars () {
RectTransform myRectTransform;
if (currentCanvas != null)
{
GameObject temp;
temp = currentCanvas;
DestroyImmediate(temp);
}
currentCanvas = Instantiate(rateMeCanvas, Vector3.zero, Quaternion.identity) as GameObject;
GameObject subCanvas = currentCanvas.transform.FindChild("subCanvas").gameObject;
myRectTransform = subCanvas.GetComponent<RectTransform>();
myRectTransform.sizeDelta = new Vector2((5*spriteWidth) + (4*spritePadding), spriteHeight);
myRectTransform.anchoredPosition = Vector2.zero;
Button[] buttons = subCanvas.GetComponentsInChildren<Button>();
float[] positions = new float[] {((2*spriteWidth)+(2*spritePadding))*-1, ((1 * spriteWidth) + (1 * spritePadding)) * -1 , 0, ((1 * spriteWidth) + (1 * spritePadding)), ((2 * spriteWidth) + (2 * spritePadding))};
for (int i = 0; i < buttons.Length; i++)
{
Debug.Log(i);
tempButton = buttons[i].gameObject;
tempButton.GetComponent<Button>().image.sprite = emptyStar;
myRectTransform = buttons[i].GetComponent<RectTransform>();
myRectTransform.sizeDelta = new Vector2(spriteWidth, spriteHeight);
myRectTransform.anchoredPosition = new Vector2(positions[i], 0);
tempButton.GetComponent<Button>().onClick.AddListener(() => OnGivenRate(i));
}
}

public void RemovePreview()
{
DestroyImmediate(currentCanvas);
}

private void OnGivenRate(int stars)
{
Debug.Log("pressed star: " + stars);
}

public class RateMeEditor
{
[CustomEditor(typeof(RateMeManager))]
public class button : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

RateMeManager myScript = (RateMeManager)target;
if (GUILayout.Button("Preview Layout"))
{
myScript.GenerateStars();
}
if (GUILayout.Button("Delete Preview"))
{
myScript.RemovePreview();
}
}
}
}
}

最佳答案

你的错误在这里:

tempButton.GetComponent<Button>().onClick.AddListener(() => OnGivenRate(i));

在将它传递给 OnGivenRate 或使用闭包之前,您必须将 i 存储在一个变量中。因为在循环结束时 i 等于 5。这就是为什么当您单击按钮时 i 显示 5。

也一样:

var rate = i;
tempButton.GetComponent<Button>().onClick.AddListener(() => OnGivenRate(rate));

Action<int> OnGivenRateClosure(int rate)
{
return () => OnGivenRate(rate);
}

tempButton.GetComponent<Button>().onClick.AddListener(OnGivenRateClosure(i));

关于c# - 无法解释的变量增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37460404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com