Unity 5.0在函数中运行代码而不调用函数

时间:2017-12-11 10:17:58

标签: c# unity5 unityscript

我正在尝试为我的团结太空射击游戏制作测试屏幕。我可以在船上更换武器/船舶/插槽,这样测试就更容易了。

这是代码:

private void CheckValueChange(int value)
    {
        RemoveListItems();

        if (value > 0 && value <= Ship.current.GetComponent<WeaponSystem>().WeaponSlots.Count)
        {
            WeaponSlot slot = Ship.current.GetComponent<WeaponSystem>().WeaponSlots[value - 1];
            if (slot.Weapon)
            {
                FieldInfo[] fields = slot.Weapon.GetType().GetFields();
                for (int i = 0; i < fields.Length; ++i)
                {
                    if (typeof(ChildStat).IsAssignableFrom(fields[i].FieldType) && fields[i].FieldType != typeof(WeaponDamage))
                    {
                        FieldInfo fInfo = fields[i];

                        ChildStat target = fInfo.GetValue(slot.Weapon) as ChildStat;
                        GameObject go = Instantiate(InputFieldStats, InputContainer);
                        go.GetComponentInChildren<TMP_Text>().text = target.Name;

                        TMP_InputField _InputFieldStats = go.GetComponentInChildren<TMP_InputField>();
                        _InputFieldStats.text = target.BaseValue.ToString();

                        _InputFieldStats.onValueChanged.AddListener((string val) =>
                        {
                            int intVal = Convert.ToInt32(val);
                            target.BaseValue = intVal;
                            fInfo.SetValue(slot.Weapon, target);
                        });

                        //ChildStat dbStat = WeaponDatabase.WeaponList[slot.WeaponIndex].StatValues.Find(stat => stat.Type == target.Type);
                        //dbStat.BaseValue = target.BaseValue;
                    }
                }
            }
        }
    }

这工作正常,但现在我更改游戏名称的变量已直接更新到datebase。这种情况发生在这段代码中:

_InputFieldStats.onValueChanged.AddListener((string val) =>
       {
          int intVal = Convert.ToInt32(val);
          target.BaseValue = intVal;
          fInfo.SetValue(slot.Weapon, target);
       });

正如我所说这样工作正常,但我希望在按钮上发生这种情况,点击什么是最好的方法,我可以在不将每个变量存储在私有变量中。提前谢谢!

0 个答案:

没有答案