unity C# 如何将文本设置为 XML 组件? [英] Unity C# How to set text as XML component?

查看:28
本文介绍了unity C# 如何将文本设置为 XML 组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作问答游戏,但刚刚回答了有关 XML 连接错误的问题.这已解决,但现在我无法弄清楚为什么我的文本没有显示 XML 文件中的问题和答案.我按照这个问题制作了我的 XML 连接代码和其余大部分代码.

I am trying to make a quiz game and have just had a question answered about an XML connection error. This has been resolved but now I cannot work out why my text is not displaying the questions and answers from the XML file. I followed this question to make my code for the XML connection and most of the rest of the code. http://answers.unity3d.com/questions/639381/too-much-prefab-for-question-quiz-games.html

Here is my code that I am having trouble with:

[SerializeField]
private TextAsset questionDataXMLFile; 
private QuestionData questionData; 
private Question currentQuestion; 
public Text answerAText;
public Text answerBText;
public Text answerCText;
public Text question; 

void Start(){
    questionData = QuestionData.LoadFromText (questionDataXMLFile.text);
}

public void SetNewQuestion(){
    int q = Random.Range (0, questionData.questions.Count - 1);
    currentQuestion = questionData.questions [q]; 

    question.text = currentQuestion.questionText; 
    answerAText.text = currentQuestion.answerA;
    answerBText.text = currentQuestion.answerB;
    answerCText.text = currentQuestion.answerC;
}

public bool CorrectAnswerSelected(int selectedAnswerID){
    return selectedAnswerID == currentQuestion.correctAnswerID;
}

public void isACorrect(){
    if (currentQuestion.correctAnswerID == 0){
        //user got it right!
    }
    else { 
        //user got it wrong!
    }
}
public void isBCorrect(){
    if (currentQuestion.correctAnswerID == 1){
        //user got it right!
    }
    else { 
        //user got it wrong!
    }
}
public void isCCorrect(){
    if (currentQuestion.correctAnswerID == 2){
        //user got it right!
    }
    else { 
        //user got it wrong!
    }
}

I think the problem is with the:

question.text = currentQuestion.questionText;

and the same with setting the answer texts. On the other tutorial, it says to use code such as: // add code here to set text values of your Question GameObject // e.g. GetComponent().Text = currentQuestion.questionText; }

But I'm not sure how to do this and what values to use etc.

Here is the c# file where I access the XML file:

[XmlRoot("Questions")] 
public class QuestionData { 
    [XmlArray("Questions")]
    [XmlArrayItem("Question")]
    public List<Question>
    questions = new List<Question>();

    public static QuestionData LoadFromText(string text){
        try{
            XmlSerializer serializer = new XmlSerializer(typeof(QuestionData));
            return serializer.Deserialize(new StringReader(text)) as QuestionData;
        }
        catch (Exception e){
            UnityEngine.Debug.LogError("Exception loading question data: " + e);
            return null; 
        }
    }
}

and here is the XML file with some test questions in:

<Questions>

<Question>
<questionText>What is the capital city of France?</questionText>
<answerA>London</answerA>
<answerB>Paris</answerB>
<answerC>Rome</answerC>
<correctAnswerID>1</correctAnswerID>
</Question>

<Question> 
<questionText>What is the capital city of England?</questionText>
<answerA>London</answerA>
<answerB>Paris</answerB>
<answerC>Rome</answerC>
<correctAnswerID>0</correctAnswerID>
</Question>

</Questions>

解决方案

Ok, I got it to work with these changes:

Change the question struct to this:

public class Question
{
    [XMLElement("questionText")]
    public string questionText;
    public string answerA;
    public string answerB;
    public string answerC;
    public int correctAnswerID;
}

Surround the stuff in the xml file with another block like this:

<QuestionsCollection>
  <Questions>
    <Question>
      <questionText>What is the capital city of France?</questionText>
      <answerA>London</answerA>
      <answerB>Paris</answerB>
      <answerC>Rome</answerC>
      <correctAnswerID>1</correctAnswerID>
    </Question>

    <Question> 
      <questionText>What is the capital city of England?</questionText>
      <answerA>London</answerA>
      <answerB>Paris</answerB>
      <answerC>Rome</answerC>
      <correctAnswerID>0</correctAnswerID>
    </Question>
  </Questions>
</QuestionsCollection>

Change

[XmlRoot("Questions")] 
public class QuestionData

to

[XmlRoot("QuestionsCollection")]
public class QuestionData

EDIT:
One flaw still in there: questionText is Null this way. Need to have a look at that.

EDIT2:
Got that fixed too. [XMLAttribute("questionText")] needs to be [XMLElement("questionText")]. Changed that block above.

EDIT3: Some further additions:
Remove the -1 from Random.Range, integer is inclusive/exclusive.

For the button click do this:

public void checkAnswer(int answerID)
{
    if(answerID == currentQuestion.correctAnswerID)
    {
        // answer was correct
        Debug.Log("correct");
    }
    else
    {
        // answer was wrong
        Debug.Log("wrong");
    }
    SetNewQuestion();
}

Give all the buttons a OnClick-event in the inspector and pass the index of the button (matching the answer ids). Example for Button3 here (the script is on the QuestionDisplay gameobject:

这篇关于unity C# 如何将文本设置为 XML 组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆