在我基于 Unity 的 Android 游戏中,我希望根据每个级别的问题数量动态添加图像。显示图像仅供参考。每个正确答案都将标记为绿色,错误答案将标记为红色。我对团结很陌生,并努力寻找实现这一目标的步骤。
任何有关此要求的示例的帮助都会有很大帮助。
我曾经编写过一个脚本,用于根据每个级别动态创建按钮。我所做的是在场景中创建第一个按钮,并基于第一个按钮添加其他按钮。下面是我的代码的外壳:
// tutorialButton and levelButtons are public variables which can be set from Inspector
RectTransform rect = tutorialButton.GetComponent<RectTransform> ();
for (int i = 1; i < levelSize; i++) {
// Instantiate the button dynamically
GameObject newButton = GameObject.Instantiate (tutorialButton);
// Set the parent of the new button (In my case, the parent of tutorialButton)
newButton.transform.SetParent (levelButtons.transform);
//Set the scale to be the same as the tutorialButton
newButton.transform.localScale = tutorialButton.transform.localScale;
//Set the position to the right of the tutorialButton
Vector3 position = tutorialButton.transform.localPosition;
position.x += rect.rect.width*i;
newButton.transform.localPosition = position;
}
我不太确定这是否是正确的方法,因为根据不同的屏幕尺寸和画布,它可能会也可能不会给出意外的结果,但希望它能让您了解动态创建对象的想法。
我不确定这是否有帮助,但如果您将场景中的所有图像都放在画布下,则只需将画布拖动到脚本上并使用
//level-1 is to keep the array notation
FindObjectOfType<NameOfScript>.ChangeColor(level-1,Color.green);
或者你也可以这样做
//level-1 is to keep the array notation
FindObjectOfType<NameOfScript>.RevertColor(level - 1);
这是脚本:
//Keep it private but you still see it in inspector
//#Encapsulation :)
[SerializeField]
private Canvas _canvas;
private Image[] _images;
//keep the original colors in case you want to change back
private Color[] _origColors;
void Start () {
_images = GetComponentsInChildren<Image>();
_origColors = new Color[_images.Length];
for (int i = 0; i < _images.Length; i++)
{
_origColors[i] = _images[i].color;
}
}
//Reverts the color of the image back to the original
public void RevertToOriginal(int imageIndex)
{
_images[imageIndex].color = _origColors[imageIndex];
}
//Change to color to the coresponding index, starts from 0
public void ChangeColor(int imageIndex, Color color)
{
_images[imageIndex].color = color;
}
P.S 如果您希望它仅在最后可见,您可以创建一个为画布启用 =(true 或 false)的方法。因此,您将其保留为 false,直到关卡结束为止,而当您想要显示时,则将其设为 true,而在每次回答后,您根据结果调用 ChangeColor。 为了方便起见,您可以使用:
NameOfScript variableName = FindObjectOfType<NameOfScript>();
然后你就打电话
variableName.ChangeColor(level - 1, Color.green);
将脚本放在哪里并不重要。我会在场景中制作某种管理器(空游戏对象)并将其放在那里。
尝试创建一个新的 C# 脚本并向其中添加以下代码。
using UnityEngine;
using UnityEngine.UI;
public class QuizManager : MonoBehaviour
{
public Texture2D referenceImage; // Reference image texture
public GameObject buttonPrefab; // Prefab for answer buttons
public Transform buttonsParent; // Parent transform for answer buttons
private void Start()
{
RawImage rawImage = GetComponentInChildren<RawImage>();
rawImage.texture = referenceImage;
// Replace this with your actual data or questions
string[] questions = { "Question 1", "Question 2", "Question 3" };
bool[] answers = { true, false, true }; // true for correct, false for incorrect
// Create buttons dynamically
for (int i = 0; i < questions.Length; i++)
CreateButton(questions[i], answers[i]);
}
private void CreateButton(string question, bool isCorrect)
{
GameObject button = Instantiate(buttonPrefab, buttonsParent);
button.GetComponentInChildren<Text>().text = question;
button.GetComponent<Button>().onClick.AddListener(() => OnButtonClick(isCorrect, button));
}
private void OnButtonClick(bool isCorrect, GameObject button)
{
// Change button color based on correctness
if (isCorrect)
button.GetComponent<Image>().color = Color.green;
else
button.GetComponent<Image>().color = Color.red;
}
}