2017-08-25 77 views
0

所以我即將製作一個幸運之輪,並且有獎品清單和獎品圖片清單,所以我想在獎品結尾展示獎品。我設法顯示獎品,但不是獎品圖像。 (顯示器是激活一個面板上,當車輪停止旋轉)如何顯示兩個列表中的兩個元素?

這裏是腳本:

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]); 

    } 
    } 
} 

我會很高興,如果有人能幫助!

+0

你的變量'旋轉'看起來像是浪費空間。由於'Update'只被調用一次,它實際上並沒有做任何事情。如果'Update'和'SpinTheWheel'同時運行,我可以看到它是如何有用的。但是,這也需要改變你使用它的方式。 –

+0

是ImagenPremio您的圖像獎勵列表?他們是否都隱藏着附有精靈的gameobjects?爲什麼不使用'ImagenPremio [itemnumber] .SetActive(true);' – ryeMoss

+0

我不認爲我理解你的問題。簡單地把'setActive'部分放在'itemNumberDisplay.text;' – ryeMoss

回答

0

使用字典存儲獎品和圖像數據。 你可以得到這樣的值:

public Dictionary<string, GameObject> Premio; 


private void Show(int itemNumber) 
{ 
    Debug.Log("Premio: " + 
       Premio.Keys.ToList()[itemNumber] + 
       Premio[Premio.Keys.ToList()[itemNumber]); 

} 

private void ShowAll() 
{ 
    foreach(var key in Premio.Keys) 
    { 
      Debug.Log("Premio: " + key + Premio[key]); 
    } 
} 
相關問題