【问题标题】:Resize Serializable List size (Unity C#)调整可序列化列表大小(Unity C#)
【发布时间】:2021-06-24 07:20:31
【问题描述】:

目前我有一个名为 QuestionAnswers 的可序列化类,具有以下内容:

public string question;
public string[] answers;
public int correctAnswer;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class QuestionAnswers
{
public string question;
public string[] answers;
public int correctAnswer;
}

在名为 ListManager 的普通 MonoBehaviour 脚本中,我将名为 QuestionAnswers 的可序列化类引用为 QnA:

public List<QuestionAnswers> QnA;

public class ListManager : MonoBehaviour
{

public List<QuestionAnswers> QnA;

在检查器中,公共列表 QnA 显示如下

Inspector image 1

我的问题是如何通过 inputfield.text 字段在我的 ListManger 脚本中更改 QnA 大小值(当前为 1,如上所示)和 Answers Size 值(当前为 4,如上所示)。

请注意,List QnA(如上图)的结构应保持不变,正确答案值连接到答案,答案连接到问题。

例子:

当我将 QnA 大小值增加到 3 时,检查器显示以下内容:

Inspector Image 2 (注意每个元素左侧的箭头)

当我点击每个元素时,检查器会显示以下内容:

Inspector Image 3 (注意每个答案左侧的箭头)

当我点击答案时,检查员会显示以下内容: Inspector Image 4 (我在检查器中的每个答案旁边添加了值 4)

只是回顾一下我的问题:

如何通过 inputfield.text 字段在我的 ListManger 脚本中更改 QnA 大小值(当前为 1,如上所示)和 Answers Size 值(当前为 4,如上所示)。

注意List&lt;QuestionAnswers&gt; QnA(如上图)的结构应该保持不变,正确答案值连接到答案,答案连接到问题。

【问题讨论】:

  • via an inputfield.text field 你的意思是你想在你的场景中有一个 UI 输入字段元素,它可以让你改变你的列表的大小?
  • 这是正确的

标签: c# unity3d arraylist serializable


【解决方案1】:

这是一个列表,因此您可以简单地使用 AddRemoveAt 条目,直到列表具有您想要的大小,例如

[SerializeField] InputField input;

private void Start ()
{
    input.contentType = ContentType.IntegerNumber;
    input.onValueChanged.AddListener(OnValueChanged);
}

private void OnInputValueChanged(string newInput)
{
    if(!int.TryParse(newInput, out var newCount)
    {
        Debug.LogError($"\"{newInput}\" is no valid number!", this);
        return;
    }

    if(newCount < 0)
    {
        Debug.LogError("Negative values are not allowed!", this);
        return;
    }

    if(newCount == QnA.Count)
    {
        Debug.Log("Already same count", this);
        return;
    }

    while(newCount > QnA.Count)
    {
        QnA.Add(new QuestionAnswers());
    }

    while(newCount < QnA.Count)
    {
        QnA.RemoveAt(QnA.Count - 1);
    }   
}

或者简单地创建一个具有正确大小的新列表,例如

private void OnInputValueChanged(string newInput)
{
    if(!int.TryParse(newInput, out var newCount)
    {
        Debug.LogError($"\"{newInput}\" is no valid number!", this);
        return;
    }
    if(newCount < 0)
    {
        Debug.LogError("Negative values are not allowed!", this);
        return;
    }

    if(newCount == QnA.Count)
    {
        Debug.Log("Already same count", this);
        return;
    }

    QnA = new QuestionAnswers[newCount].ToList();
}

很大程度上取决于您的具体用例。


如果您希望默认情况下已经初始化它们,答案将自动具有4 的大小

public string[] answers = new string[4];

【讨论】:

  • QnA.Add 和 QnA.Remove 会出错,因为它将 QnA 视为数组而不是列表
  • @DesertFox 在您的问题中显然是 List&lt;QuestionAnswers&gt; 并且绝对 不是 数组...我们只能根据您提供的代码提供答案...顺便说一句在第二个解决方案中,我现在已经完全知道你在回答中所做的事情......除了我已经告诉你如何从 InputField 中获取值,如果你的是一个数组 - 那么只需跳过 ToList() 调用.. .
【解决方案2】:

我想我解决了这个问题。

这是我的名为 QnA 的可序列化列表的代码

    int newsize = 3 (This value will eventually come from an inputfield)   
    
    [Serializable]
    private class QnA
    {
        public string question;
        public string[] answers;
        public int correctAnswer;
    }

然后我做了一个这样的序列化字段

[SerializeField] private QnA[] qnaArray;

在我的 Start 函数中刚刚做了以下

public void Start()
{
    qnaArray = new QnA[newsize];

}

因此,当我运行应用程序时,QnA 大小会自动更改为 newsize int 值。 我现在将继续尝试通过代码更改答案值并实现 inputfield 以替换 newsize int。

感谢大家的帮助。 非常感谢。

【讨论】:

  • 下一个问题是如何访问可序列化类中的公共字符串 [] 答案以更改其值。
猜你喜欢
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
相关资源
最近更新 更多