我有一个图库,其中我有4个UI图像选择随机精灵形成我的精灵阵列并在随机时间后分配它。我的问题是我的4个UI图像
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
听起来您想要从列表中提取随机图像并将其显示在一些Image对象中。您需要在单独的对象中包含精灵(图像)列表,然后使用Sprite GetSprite()
方法,该方法仅返回尚未发送的精灵。
一种方法是拥有2个数组Sprite[] NotUsed
和Sprite[] Used
,当您从NotUsed中提取一个数组时,将其删除并将其添加到另一个列表中。这可以确保您只返回列表中的唯一精灵。
希望这就是你要找的东西。