我目前正在学习C#和unity在我自己的游戏上工作,目前我正在尝试使用下拉UI在农场建筑中选择作物,并在数据管理脚本中保存列表。
我想把变量带过来并在加法脚本中使用,这样作物的数量就会根据延迟和代号增加,当我试图把变量带到它上面时,添加项目的名称,而不是使用它来将数字相加在一起,我试图使用Convert.ToInt32和一个temp变量来绕过这个问题,但它不读取数字并将它们相加。
主脚本
[SerializeField]
private dataManager dataScript;
[SerializeField]
private Text Grain, Veg;
[SerializeField]
private float delay, delayBase;
[SerializeField]
private int stringToInt1, stringToInt2;
[SerializeField]
private string Items, itemGeneration;
public Dropdown farmDropdown;
Coroutine farming;
// Start is called before the first frame update
void Start()
{
GameObject dataObj = GameObject.Find("Base");
dataScript = dataObj.GetComponent<dataManager>();
farming = StartCoroutine(farmGrowth());
}
// Update is called once per frame
void Update()
{
Grain.GetComponent<UnityEngine.UI.Text>().text = ("Grain: " + dataScript.grain.ToString());
Veg.GetComponent<UnityEngine.UI.Text>().text = ("Veg: " + dataScript.veg.ToString());
delay = delayBase;
}
public void dropdown_IndexChanged(int index)
{
Items = dataScript.ItemChoices[index];
itemGeneration = dataScript.itemGenerationChoices[index];
}
private IEnumerator farmGrowth() {
while (true) {
yield return new WaitForSeconds(delay);
farmGeneration();
}
}
private void farmGeneration(){
stringToInt1 = Convert.ToInt32(Items);
stringToInt2 = Convert.ToInt32(itemGeneration);
stringToInt1 += stringToInt2;
//Items += itemGeneration;
}数据脚本
public int grain, veg;
public int grainGeneration, vegGeneration;
public List<string> ItemChoices = new List<string>() { "grain", "veg"};
public List<string> itemGenerationChoices = new List<string>() { "grainGeneration", "vegGeneration"};这是目前的代码,这只是我在学习和弄清楚我能做什么以及如何做它的一个简单版本,底部应该读取下拉列表中选择的内容,获得要种植的作物,然后将世代量添加到持有的总作物上,例如: grain += grainGeneration
发布于 2021-07-16 22:28:00
我不确定我是否理解了这个问题,下面的代码只是让程序知道你想从下拉列表中得到什么。
public Dropdown crops;
public string selected;
void Start()
{
crops.onValueChanged.AddListener(
delegate
{
DropdownValueChanged();
});
}
void DropdownValueChanged()
{
switch(crops)
{
case 0:
selected = "bananas";
break;
}
}PS:我不能评论,但是正如评论中提到的,你应该让你的问题更清楚,只需要基本的信息。如果你有各种各样的事情要做,只需列举它们,这样其他人就更容易效仿和帮助。
https://stackoverflow.com/questions/68376805
复制相似问题