按钮在不应该的时候交换位置



更新:正在交换的是按钮本身,而不是tempRefId。尽管这个问题仍然存在。

我正试图为Unity游戏编写一个mod,但我不确定这是否是一个与Unity相关的问题。在游戏中,玩家可以打开一个地图,这个代码所做的就是在地图中的几个对象旁边添加一个按钮。我遇到的问题是,每次打开映射时,都会交换与每个按钮相关的整数值tempRefId。它一开始是正确的,然后被设置为不正确的值,然后交换回来等等。我确信下面的代码只运行一次,第一次打开地图时。

// Loop through a set of gameobjects in code above
if (child.gameObject.name.Contains("res-entry") && child.GetComponent<UIResAmountEntry>().refId != 0 && child.GetComponent<UIResAmountEntry>().valueString.Trim() != "0" && !child.Find("unique-string"))
{
int tempRefId = child.GetComponent<UIResAmountEntry>().refId; // ID of resource
Transform iconTransform = child.Find("icon"); 
GameObject toggleHighlightButton = GameObject.Instantiate<GameObject>(
original: iconTransform.gameObject,
position: new Vector3(child.position.x+1f, child.position.y-0.02f, child.position.z),
rotation: Quaternion.identity,
parent:   child);
UIButton uiButton1 = toggleHighlightButton.GetComponent<UIButton>();
uiButton1.onClick += (id) => { ToggleVeinHeighlight(tempRefId, ref toggleHighlightButton); };
uiButton1.onRightClick += (id) => { DebugStuff(tempRefId); };  // Why is this value inconsistant?
toggleHighlightButton.name = "unique-string";
}

根据我的理解,int是通过值传递的,所以我不明白tempRefId的值在赋值后会如何变化,这可能意味着我没有正确理解我的lambda表达式。如果代码中没有明显的问题,那么是什么原因导致了问题?

该问题不会影响已创建按钮的所有实例,似乎只影响循环中创建的第一个和最后一个按钮。这两个值是每次打开地图时交换的值。

问题是,每次打开地图时,我用作按钮父对象的child对象实际上都在移动,这似乎是游戏中的一个错误。我通过使用child的父对象作为按钮的父对象来解决这个问题,该对象每次都不会移动。

最新更新