如何使图像弹出时,我的球击中打击或备用

本文关键字:备用 我的 图像 何使 | 更新日期: 2023-09-27 18:24:38

大家好,我正在制作一款保龄球游戏。我想要的是当球击中好球时弹出好球的图像,或者当击中备用球时弹出备用图像。请帮帮我,我们将不胜感激。请尽快回答我。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class ScoreDisplay : MonoBehaviour {
public  Image strick;
public Text[] rollTexts, frameTexts;

public void FillRolls (List<int> rolls) {
    string scoresString = FormatRolls (rolls);
    for (int i = 0; i < scoresString.Length; i++) {
        rollTexts[i].text = scoresString[i].ToString();
    }
}
public void FillFrames (List<int> frames) {
    for (int i = 0; i < frames.Count; i++) {
        frameTexts[i].text = frames[i].ToString();
    }
}
public static string FormatRolls (List<int> rolls) {
    string output = "";
    for (int i = 0; i < rolls.Count; i++) {
        int box = output.Length + 1;                            // Score box 1 to 21 
        if (rolls[i] == 0) {                                    // Always enter 0 as -
            output += "-";
            strick.enabled = true;
            Debug.LogError("Better Luck Next Time");
        } else if (box % 2 == 0 && rolls[i-1]+rolls[i] == 10) { // SPARE anywhere
            output += "/";  
            Debug.LogError("Its Spare");
        } else if (box >= 19 && rolls[i] == 10) {               // STRIKE in frame 10
            output += "X";
            Debug.LogError("Congo you got Strick");
        } else if (rolls[i] == 10) {                            // STRIKE in frame 1-9
            output += "X ";
            Debug.LogError("Its Strick");
        } else {
            output += rolls[i].ToString();                      // Normal 1-9 bowl
            Debug.LogError("Try Your Hard");
        }
    }
    return output;
}
}

如何使图像弹出时,我的球击中打击或备用

您可以将图像作为空gameobject的子级,这将把它变成一个gameObject,您可以使用Instantiate(object,position,rotation)函数在代码中轻松地实例化它。您可以声明一个公共的GameObject,将您的预制件(Gameobject和您的图像作为子级)拖到检查器中,然后在上面使用Instantiate,我建议您这样做,因为在gameobject实例化后,您可以对其进行更多的控制。

for (int i = 0; i < rolls.Count; i++) {
    int box = output.Length + 1;                            // Score box 1 to 21 
    if (rolls[i] == 0) {                                    // Always enter 0 as -
        output += "-";
        Instantiate(GameObjectYouWantToInstantiate, location, rotation);  
        //strick.enabled = true;
        Debug.LogError("Better Luck Next Time");
    } else if (box % 2 == 0 && rolls[i-1]+rolls[i] == 10) { // SPARE anywhere
        output += "/";  
        Debug.LogError("Its Spare");
    } else if (box >= 19 && rolls[i] == 10) {               // STRIKE in frame 10
        output += "X";
        Debug.LogError("Congo you got Strick");
    } else if (rolls[i] == 10) {                            // STRIKE in frame 1-9
        output += "X ";
        Debug.LogError("Its Strick");
    } else {
        output += rolls[i].ToString();                      // Normal 1-9 bowl
        Debug.LogError("Try Your Hard");
    }
}

当您想要销毁它时,请使用Destroy(Strick)