【问题标题】:Unity c# use dropdown value in addition code rather than dropdown string nameUnity c#在附加代码中使用下拉值而不是下拉字符串名称
【发布时间】:2021-07-14 10:59:14
【问题描述】:

我目前正在学习 C# 和 unity 来开发我自己的游戏,目前我正在尝试使用下拉 UI 在农作物之间进行选择,以在数据管理脚本中保存列表的农场建筑中种植。

我想将变量引入并在添加脚本中使用,以便作物数量将根据延迟和世代数增加,当我尝试将变量引入它时,添加项目名称而不是使用它将数字相加,我尝试使用 Convert.ToInt32 和一个临时变量来解决这个问题,但它不会读取数字并将它们相加。

主脚本

[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"};

这是目前的代码,只是我在学习和弄清楚我能做什么以及如何做一个简单的版本,底部应该读取下拉列表中选择的内容,获取作物是什么增长,然后将发电量添加到持有的总作物上,例如: 谷物 += 谷物世代

【问题讨论】:

  • 你能删除所有与下拉问题无关的东西吗?让它成为minimal reproducible example(专注于最小化)
  • 我想要做的是从拉入列出的项目中获取值以在 dataScript.X 和 dataScript.Y 中使用,其中 X 和 Y 是 Items 和 itemGeneration 持有的值,即 grain 和grainGeneration,因此理想情况下类似于 dataScript.(Items) += dataScript.(itemGeneration) 但实际上可以正常工作
  • 嗯,那是因为你正在转换一个没有任何数字的字符串。 ToInt32 需要像 "0""1" 这样的字符串。我不确定您要做什么;如果您的示例显示了您想对 stringToInt1 做什么,这可能会很有用。如果您想在将两个整数附加到字符串之前将它们添加在一起,您可以这样做 "A string" + (int1 + int2);.

标签: c# unity3d drop-down-menu type-conversion


【解决方案1】:

我不确定我是否理解了这个问题,下面的代码只是让程序知道你想要从下拉列表中得到什么。

public Dropdown crops;
public string selected;

void Start()
{
   crops.onValueChanged.AddListener(
            delegate
            {
                DropdownValueChanged();
            });
}

void DropdownValueChanged()
{
   switch(crops)
   {
     case 0:
        selected = "bananas";
        break;
   }
}

PS:我无法发表评论,但正如 cmets 所述,您应该仅使用基本信息使您的问题更加清晰。如果您有各种事情要做,只需列举它们,以便其他人更容易理解和帮助。

【讨论】:

    猜你喜欢
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    相关资源
    最近更新 更多