在运行时动态添加图像 - Unity 5

3
在我的基于Unity的Android游戏中,我希望根据每个关卡中问题的数量动态添加图像。该图像用于参考。每个正确答案将以绿色标记,错误答案将以红色标记。我是Unity的新手,并且在努力寻找实现此目标的步骤。
如果能提供一个示例来满足这一需求,那将是非常有帮助的。
图片链接:enter image description here
3个回答

2

我曾经编写过一个脚本,根据每个级别动态创建按钮。我所做的是在场景中创建第一个按钮,并基于第一个按钮添加其他按钮。以下是我的代码框架:

// 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;
}

我不确定这是否是正确的方法,因为它可能会根据不同的屏幕尺寸和画布而产生意外的结果,但希望它能让您了解动态创建对象的想法。


在canvas还是panel中附加这个脚本? - undefined
只要你在检视器中设置了tutorialButton和levelButton对象,脚本附加的位置实际上并不重要。它不使用脚本所附加的Gameobject的任何属性。(在我的情况下,我有一个处理所有游戏逻辑的脚本,它附加在一个空的Gameobject上,并且这段代码在Start()函数中,所以在每个关卡(场景)加载时,它会被调用一次,并创建所需的按钮)。 - undefined

1
我不确定这是否有帮助,但如果您将场景中的所有图像放在一个画布下,那么您只需要在脚本中拖动画布并使用以下代码:
//level-1 用于保留数组符号 FindObjectOfType.ChangeColor(level-1,Color.green);
或者您也可以使用以下代码:
//level-1 用于保留数组符号 FindObjectOfType.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;
}

附言:如果您希望它仅在最后可见,可以创建一个方法,在其中为画布启用 =(true或false)。因此,您将其保持为假,直到关卡结束,并在要显示时将其设置为真,而在每个答案之后,您都会根据结果调用ChangeColor。

为了使它更容易,您可以使用:

NameOfScript variableName = FindObjectOfType<NameOfScript>();

然后你只需要调用它。
variableName.ChangeColor(level - 1, Color.green);

同时,脚本放在哪里并不重要。我会在场景中创建一种管理器(空游戏对象),然后把它放在那里。


0
尝试创建一个新的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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接