如何在C#for Unity中为骰子制作记分器?
我正在尝试为骰子计数器制作记分板,例如,如果我掷1,你将在记分板上获得1分,如果你掷6,则在记分板上增加6分。这是我掷骰子的代码:如何在C#for Unity中为骰子制作记分器?,c#,unity3d,C#,Unity3d,我正在尝试为骰子计数器制作记分板,例如,如果我掷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
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
}
}
使用系统集合;
使用UnityEngine;
公共类骰子:单一行为
{
//要从资源文件夹加载的骰子边精灵数组
私人雪碧【】侧边;
//引用此精灵渲染器以更改精灵
私人喷泉人;
//用于初始化
私有void Start()
{
//指定精灵渲染器组件
rend=GetComponent();
//从资源文件夹的DiceSides子文件夹将dice sides精灵加载到阵列
diceSides=Resources.LoadAll(“diceSides/”);
}
//如果在骰子上单击鼠标左键,则开始滚动骰子
私有void OnMouseDown()
{
开始例行程序(“滚动的冰”);
}
//掷骰子的协同程序
私有IEnumerator RollTheDice()
{
//变量包含随机骰子边数
//需要对其进行赋值,初始值为0
int=0;
//骰子在协同程序结束时读取的最后一面或值
int finalSide=0;
//循环以随机切换骰子边
//在最后一方出现之前,这里有20个
对于(int i=0;i,如果我理解正确,那么你想要的是,如果你先掷骰子,骰子显示为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
}
}
使用系统集合;
使用UnityEngine;
公共类骰子:单一行为
{
//要从资源文件夹加载的骰子边精灵数组
私人雪碧【】侧边;
//引用此精灵渲染器以更改精灵
私人喷泉人;
//用于初始化
私有void Start()
{
//指定精灵渲染器组件
rend=GetComponent();
//从资源文件夹的DiceSides子文件夹将dice sides精灵加载到阵列
diceSides=Resources.LoadAll(“diceSides/”);
}
//如果在骰子上单击鼠标左键,则开始滚动骰子
私有void OnMouseDown()
{
开始例行程序(“滚动的冰”);
}
//掷骰子的协同程序
私有IEnumerator RollTheDice()
{
//变量包含随机骰子边数
//需要对其进行赋值,初始值为0
int=0;
//骰子在协同程序结束时读取的最后一面或值
int finalSide=0;
//循环以随机切换骰子边
//在最后一方出现之前,这里有20个
对于(inti=0;我不知道,我对这件事还不熟悉,idk我在做什么,好吧:)嗯,UnityScript是为Unity创建的Javascript派生,但现在它被弃用,取而代之的是C。
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
}
}