我曾经编写过一个脚本,根据每个级别动态创建按钮。我所做的是在场景中创建第一个按钮,并基于第一个按钮添加其他按钮。以下是我的代码框架:
// 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;
}
我不确定这是否是正确的方法,因为它可能会根据不同的屏幕尺寸和画布而产生意外的结果,但希望它能让您了解动态创建对象的想法。
//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);
同时,脚本放在哪里并不重要。我会在场景中创建一种管理器(空游戏对象),然后把它放在那里。
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;
}
}