What it looks like in unity. I have also drawn a red arrow to show what I am trying to move
我正在尝试做一个问答游戏,以显示带有相应图像的随机单词。我可以同时显示随机问题和随机图像。我的问题是,将显示的图像并不总是该单词的正确图像。是否可以将随机图像向上移动并与随机文本一起放置?
我尝试将图像直接与文本一起附加,问题是文本将消失,只有图像会保留。我要做的就是将图像复制到游戏对象中,是重复所有文本操作。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public Question[] questions;
public Image[] images;
private static List<Question> unansweredQuestions;
private static List<Image> unansweredImages;
private Question currentQuestion;
private Image currentImage;
[SerializeField]
private Text factText;
[SerializeField]
private Image factImage;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Image trueAnswerImage;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Image falseAnswerImage;
[SerializeField]
private Animator animator;
[SerializeField]
private float timeBetweenQuestions = 1f;
[SerializeField]
private float timeBetweenImages = 1f;
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
if (unansweredImages == null || unansweredImages.Count == 0)
{
unansweredImages = images.ToList<Image>();
}
SetCurrentQuestion();
}
void SetCurrentQuestion()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
int randomImageIndex = Random.Range(0, unansweredQuestions.Count);
currentImage = unansweredImages[randomImageIndex];
factText.text = currentQuestion.fact;
if (currentQuestion.isTrue)
{
trueAnswerText.text = "CORRECT";
falseAnswerText.text = "WRONG";
}
else
{
trueAnswerText.text = "WRONG";
falseAnswerText.text = "CORRECT";
}
}
IEnumerator TransitionToNextQuestion()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
unansweredImages.Remove(currentImage);
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
我希望图片和正确的文字会随机出现。
答案 0 :(得分:0)
基于少量信息,我认为您正在尝试设置一个随机问题,但图像正确。
如果是这种情况: 我看到的问题是您正在使用列表来访问问题和图像。您可以使用字典解决问题。
-------------------
| Stock Value: 7$ | < This information gets updated
-------------------
shell:> I can still type here
新功能:
public Question[] questions;
public Image[] images;
这里的整数将是两个字典的相同随机索引。
您可以在运行时手动设置问题和图像。然后,无论何时要将图像分配给该随机文本,都只需使用Random.Range函数获得一个随机数。它将返回一个可用作索引的整数。让我知道是否有帮助。
PS:您可以删除int数据类型之前的空格。