这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

72 行
1.7 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIPaginationFiller : MonoBehaviour
{
[SerializeField] private Image _imagePaginationPrefab= default;
[SerializeField] private Transform _parentPagination = default;
[SerializeField] private Sprite _emptyPagination = default;
[SerializeField] private Sprite _filledPagination = default;
private List<Image> _instantiatedImages = default;
private void Start()
{
_instantiatedImages = new List<Image>();
}
public void SetPagination(int paginationCount, int selectedPaginationIndex)
{
if (_instantiatedImages == null)
_instantiatedImages = new List<Image>();
//instanciate pagination images from the prefab
int maxCount = Mathf.Max(paginationCount, _instantiatedImages.Count);
if (maxCount > 0)
{
for (int i = 0; i < maxCount; i++)
{
if (i >= _instantiatedImages.Count)
{
Image instantiatedImage = Instantiate(_imagePaginationPrefab, _parentPagination);
_instantiatedImages.Add(instantiatedImage);
}
if (i < paginationCount)
{
_instantiatedImages[i].gameObject.SetActive(true);
}
else
{
_instantiatedImages[i].gameObject.SetActive(false);
}
}
SetCurrentPagination(selectedPaginationIndex);
}
}
public void SetCurrentPagination(int selectedPaginationIndex)
{
if (_instantiatedImages.Count > selectedPaginationIndex)
for (int i = 0; i < _instantiatedImages.Count; i++)
{
if (i == selectedPaginationIndex)
{
_instantiatedImages[i].sprite = _filledPagination;
}
else
{
_instantiatedImages[i].sprite = _emptyPagination;
}
}
else
Debug.LogError("Error in pagination number");
}
}