我有一個圖庫我有4個UI圖像的圖像從我的精靈數組中選擇隨機精靈並在隨機時間後分配它。我的問題是,我的4個UI圖像Unity:Gallery Images隨機重複
-1
A
回答
0
您每次調用MakeRandomNumber時都會得到一個隨機項。 您需要隨機播放圖像列表,並在完成循環時再次洗牌。 我無法測試代碼,但確定如果出現小錯誤,您可以修復。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Gallery : MonoBehaviour
{
public float timer;
public Sprite[] gallerySprites;
public Image currentImage;
public int ImagesLoopIndex = 0;
public int newRandomNummer;
int lastRandomNumber;
int min = 0;
int max;
public Gallery[] galleries;
public List<int> val = new List<int>();
// Use this for initialization
void Start()
{
ShuffleImagesList();
max = gallerySprites.Length;
timer = MakeRandomNumber(8, 18);
}
private void ShuffleImagesList()
{
for (int i = 0; i < galleries.lenght -1; i++)
{
Gallery[] temp = galleries[i];
int randomIndex = Random.Range(i, galleries.lenght-1);
galleries[i] = galleries[randomIndex];
galleries[randomIndex] = temp;
}
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
StartCoroutine(fadeImages());
timer = MakeRandomNumber(8, 18);
}
}
IEnumerator fadeImages()
{
if (ImagesLoopIndex >= galleries.lenght)
{
ShuffleImagesList();
ImagesLoopIndex = 0;
}
else
{
currentImage.CrossFadeAlpha(0, 1f, false);
yield return new WaitForSeconds(1f);
currentImage.CrossFadeAlpha(1, 1f, false);
currentImage.sprite = gallerySprites[ImagesLoopIndex];
ImagesLoopIndex = ImagesLoopIndex +1;
}
}
0
這聽起來像你想從列表中拉出一個隨機圖像,並顯示在一些圖像對象。您需要將精靈列表(圖像)放在單獨的對象中,然後使用Sprite GetSprite()
方法,該方法僅返回尚未發送的精靈。
一種方法是有2個數組Sprite[] NotUsed
和Sprite[] Used
,當您從NotUsed中隨機抽取一個時,將其移除並將其添加到另一個列表中。這可確保您只會返回列表中的唯一精靈。
希望這是你正在尋找。
相關問題
- 1. 消除隨機重複?
- 2. 隨機顏色重複
- 3. 不重複的隨機數
- 4. 非重複的隨機數
- 5. 隨機數字與重複
- 6. 隨機數字不重複
- 7. 重複隨機抽樣SQL
- 8. 重複的隨機數
- 9. 隨機數組重複
- 10. 我每次都需要重複在Sqlite中隨機重複數據android我想要重複隨機數據
- 11. 隨機複數
- 12. 顯示不重複的隨機圖像
- 13. 隨機,非重複2D列表Python
- 14. 沒有重複的隨機字符串
- 15. 隨機圖像不重複php
- 16. 隨機圖像加載沒有重複
- 17. 生成隨機數字而不重複
- 18. 指定隨機夥伴而不重複?
- 19. 生成非重複的隨機數
- 20. 選擇隨機行不重複
- 21. 如何隨機數據不重複
- 22. 重複約束的隨機包
- 23. 沒有重複的隨機函數
- 24. Python - 隨機問題繼續重複
- 25. [AS3]隨機做一些不重複
- 26. 隨機行爲:重複相同數量
- 27. 生成沒有重複的隨機數
- 28. 隨機數發生器沒有重複
- 29. 隨機非重複編號Xcode
- 30. 隨機分區列表無重複
嘿,它不工作,是的,有錯誤,但我修復它們,但它仍然是重複的。有任何想法嗎? –
@ loue13'currentImage.sprite = gallerySprites [MakeRandomNumber(0,max)];'應該是'currentImage.sprite = gallerySprites [ImagesLoopIndex];'來解決它。此外,其他代碼應該每次運行,而不是在別的。 –
修復了錯誤,謝謝 – joreldraw