c# - 如何在 C# for Unity 中为骰子制作分数计数器?
问题描述
我正在尝试为骰子计数器制作记分牌,例如,如果我掷出 1,则您在记分牌上获得 1 分,如果您掷出 6,则在记分牌上增加 6 分。这是我的掷骰子代码:
using System.Collections;
using UnityEngine;
public class Dice : MonoBehaviour
{
// Array of dice sides sprites to load from Resources folder
private Sprite[] diceSides;
// Reference to this sprite renderer to change sprites
private SpriteRenderer rend;
// Use this for initialization
private void Start()
{
// Assign Sprite Renderer component
rend = GetComponent<SpriteRenderer>();
// Load dice sides sprites to array from DiceSides subfolder of Resources folder
diceSides = Resources.LoadAll<Sprite>("DiceSides/");
}
// If you left click over the dice then RollTheDice coroitine is started
private void OnMouseDown()
{
StartCoroutine("RollTheDice");
}
//coroutine that rolls the dice
private IEnumerator RollTheDice()
{
//variable to contain random dice side number
//it needs to be assigned, let it be 0 initially
int randomDiceSide = 0;
//Final side or value that dice reads in the end of coroutine
int finalSide = 0;
//loop to switch dice sides randomly
//before final side appears, 20 itterations here
for (int i = 0; i <= 20; i++)
{
// Pick up random value from 0 to 5 (all inclusive)
randomDiceSide = Random.Range(0,5);
//set sprite to upper face of dice from array according to random value
rend.sprite = diceSides[randomDiceSide];
// Pause before next itteration
yield return new WaitForSeconds(0.05f);
}
// Assigning final side so you can use this value later in your game
// for player movement for example
finalSide = randomDiceSide + 1;
// Show final dice value in console
Debug.Log(finalSide);
//Attempt to add to the score board in game
}
}
我还有一个记分牌代码,我只是不知道如何将它与“骰子面”联系起来,这里是:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour
{
public static int scoreValue = 0;
Text score;
void Start()
{
score = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
score.text = "Score:" + scoreValue;
}
}
解决方案
因此,如果我完全正确,您所要求的就是,如果您首先掷骰子并显示 1,然后再次掷骰子,现在骰子显示 6。您想将它们加在一起,以便记分牌显示7? 如果是这样,您可以添加:
Scoreboard.scoreValue += finalSide;
在脚本的底部,如下所示:
using System.Collections;
using UnityEngine;
public class Dice : MonoBehaviour
{
// Array of dice sides sprites to load from Resources folder
private Sprite[] diceSides;
// Reference to this sprite renderer to change sprites
private SpriteRenderer rend;
// Use this for initialization
private void Start()
{
// Assign Sprite Renderer component
rend = GetComponent<SpriteRenderer>();
// Load dice sides sprites to array from DiceSides subfolder of Resources folder
diceSides = Resources.LoadAll<Sprite>("DiceSides/");
}
// If you left click over the dice then RollTheDice coroitine is started
private void OnMouseDown()
{
StartCoroutine("RollTheDice");
}
//coroutine that rolls the dice
private IEnumerator RollTheDice()
{
//variable to contain random dice side number
//it needs to be assigned, let it be 0 initially
int randomDiceSide = 0;
//Final side or value that dice reads in the end of coroutine
int finalSide = 0;
//loop to switch dice sides randomly
//before final side appears, 20 itterations here
for (int i = 0; i <= 20; i++)
{
// Pick up random value from 0 to 5 (all inclusive)
randomDiceSide = Random.Range(0, 5);
//set sprite to upper face of dice from array according to random value
rend.sprite = diceSides[randomDiceSide];
// Pause before next itteration
yield return new WaitForSeconds(0.05f);
}
// Assigning final side so you can use this value later in your game
// for player movement for example
finalSide = randomDiceSide + 1;
// Show final dice value in console
Debug.Log(finalSide);
Scoreboard.scoreValue += finalSide;
//Attempt to add to the score board in game
}
}
推荐阅读
- react-native - 在不使用输入字段的情况下确定在 React Native 上按下了哪个键
- java - 检索保存到数据库中的 pdf 文件,以便在 pdfbox 中进行操作。Springbot 和休眠应用程序
- jquery - Rails 6 Webpack JQuery Yarn - Bootstrap 汉堡菜单不起作用的问题
- postgresql - Postgres - 创建一个列(更改表)作为其他两列的计算
- python - 有没有办法分配多个变量而无需手动编写它们?
- memory - 当最差匹配成功时,最佳匹配内存分配是否总是成功?
- java - 步长大于 3 时楼梯问题中的 Stackoverflow 错误
- log4j - Log4j1 在 Log4j2 中的 OnlyOnceErrorHandler 等效项
- angular - 无法删除 Firebase 数据库的节点
- python - Google Sheets API v4 删除不清除行