【发布时间】:2017-08-25 12:29:21
【问题描述】:
所以我制作了一个幸运之轮,并且有一个奖品列表和奖品图像列表,所以我想在最后显示奖品和奖品图像。我设法显示奖品,但没有显示奖品图像。 (显示屏在车轮停止旋转时激活的面板上)
这是脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SpinWheel : MonoBehaviour
{
public List<string> Premio = new List<string>();
public List<AnimationCurve> animationCurves;
public List<GameObject> ImagenPremio = new List<GameObject>();
public Text itemNumberDisplay;
private bool spinning;
private float anglePerItem;
private int randomTime;
private int itemNumber;
public GameObject RoundEndDisplay;
void Start()
{
spinning = false;
anglePerItem = 360 / Premio.Count;
itemNumberDisplay.text = "";
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0) && !spinning)
{
randomTime = Random.Range(1, 4);
itemNumber = Random.Range(0, Premio.Count);
float maxAngle = 360 * randomTime + (itemNumber * anglePerItem);
StartCoroutine(SpinTheWheel(5 * randomTime, maxAngle));
}
}
IEnumerator SpinTheWheel(float time, float maxAngle)
{
spinning = true;
float timer = 0.0f;
float startAngle = transform.eulerAngles.z;
maxAngle = maxAngle - startAngle;
int animationCurveNumber = Random.Range(0, animationCurves.Count);
Debug.Log("Animation Curve No. : " + animationCurveNumber);
while (timer < time)
{
//to calculate rotation
float angle = maxAngle *
animationCurves[animationCurveNumber].Evaluate(timer / time);
transform.eulerAngles = new Vector3(0.0f, 0.0f, angle + startAngle);
timer += Time.deltaTime;
yield return 0;
}
transform.eulerAngles = new Vector3(0.0f, 0.0f, maxAngle + startAngle);
spinning = false;
Debug.Log("Premio: " + Premio[itemNumber]);
if (spinning == false)
{
yield return new WaitForSeconds(1);
RoundEndDisplay.SetActive(true);
itemNumberDisplay.text = ("Premio: " + Premio[itemNumber]);
}
}
}
如果有人可以提供帮助,我会很高兴!
【问题讨论】:
-
您的变量
spinning似乎是在浪费空间。由于Update只被调用一次,它现在实际上并没有做任何事情。我可以看到如果Update和SpinTheWheel并行运行它会有多大用处。但这也需要改变你使用它的方式。 -
ImagenPremio 是您的图像奖励列表吗?它们都是带有精灵的隐藏游戏对象吗?为什么不直接使用
ImagenPremio[itemnumber].SetActive(true); -
我不认为我理解你的问题。简单地将
setActive部分放在itemNumberDisplay.text;之后 -
@ryemoss 非常感谢!我为每个奖品制作了一个不同的面板,带有图像和文字,因此它会同时显示两者,你只是救了我的命,我浪费了 3 天时间寻找一种如此简单的方法!!!
-
两个列表?一个列表和一个自定义类怎么样?
List<Prize>...,public class Prize { string name; Sprite image; }