You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.6 KiB
129 lines
3.6 KiB
using DG.Tweening; |
|
using OneP.InfinityScrollView; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
public class SlotSpin : MonoBehaviour |
|
{ |
|
RectTransform rect; |
|
CanvasGroup canvasGroup; |
|
public InfinityScrollView infinityScroll; |
|
public Transform content; |
|
RectTransform contentRectTransform; |
|
public Animation animation; |
|
|
|
bool INIT = false; |
|
System.Action<Sprite> OnSelect; |
|
/// <summary> |
|
/// 슬롯을 초기화 합니다 |
|
/// </summary> |
|
/// <param name="sprites">스프라이트 리스트 필요</param> |
|
/// <param name="idxSlot">-1 = Random</param> |
|
/// <param name="OnSelect">스핀 종료 이벤트</param> |
|
public void OnInitialized(List<Sprite> sprites, int idxSlot, System.Action<Sprite> OnSelect) |
|
{ |
|
if (contentRectTransform == null) |
|
contentRectTransform = content.GetComponent<RectTransform>(); |
|
|
|
if(rect == null) |
|
rect = GetComponent<RectTransform>(); |
|
if(canvasGroup == null) |
|
canvasGroup = GetComponent<CanvasGroup>(); |
|
|
|
this.idxSlot = idxSlot < sprites.Count ? idxSlot : sprites.Count - 1; |
|
this.OnSelect = OnSelect; |
|
this.sprites.Clear(); |
|
spritesOrigin = new List<Sprite>(sprites); |
|
max = sprites.Count; |
|
|
|
StartCoroutine(Initialized()); |
|
} |
|
public List<SlotSpinItem> items; |
|
IEnumerator Initialized() |
|
{ |
|
yield return null; |
|
|
|
if (!INIT) |
|
{ |
|
SlotSpinItem item = null; |
|
for (int i = 0; i < infinityScroll.listItem.Count; i++) |
|
{ |
|
item = infinityScroll.listItem[i].GetComponent<SlotSpinItem>(); |
|
item.OnGeted = DataGet; |
|
|
|
items.Add(item); |
|
} |
|
|
|
yield return null; |
|
|
|
INIT = true; |
|
} |
|
|
|
LoadAsync(); |
|
} |
|
void LoadAsync() |
|
{ |
|
int idx = 0; |
|
|
|
if (idxSlot == -1) |
|
{ |
|
for (int i = 0; i < listCount; i++) |
|
sprites.AddRange(spritesOrigin); |
|
|
|
idx = Random.Range((int)(sprites.Count * percent), sprites.Count - max); |
|
idxSlot = idx % max; |
|
} |
|
else |
|
{ |
|
//idx = sprites.Count - (max * 2) + idxSlot; |
|
//idxSlot = idx % max; |
|
|
|
for (int i = 0; i < listCount - 1; i++) |
|
sprites.AddRange(spritesOrigin); |
|
|
|
idx = sprites.Count + spritesOrigin.Count - 1; |
|
|
|
Sprite sprite = spritesOrigin[idxSlot]; |
|
spritesOrigin.Remove(sprite); |
|
Util.ListShuffle<Sprite>.Shuffle(spritesOrigin); |
|
spritesOrigin.Add(sprite); |
|
|
|
sprites.AddRange(spritesOrigin); |
|
} |
|
|
|
contentRectTransform.anchoredPosition = Vector2.zero; |
|
if (animation != null) |
|
Util.AnimationStop(animation, animation.clip.name); |
|
|
|
content.transform.DOLocalMoveY(-(infinityScroll.itemSize * idx + infinityScroll.itemSize / 2f), duration).SetEase(ease).OnComplete(() => |
|
{ |
|
if (animation != null) |
|
animation.Play(); |
|
|
|
if (OnSelect != null) |
|
OnSelect(sprites[idxSlot]); |
|
}); |
|
|
|
infinityScroll.Setup(sprites.Count); |
|
infinityScroll.InternalReload(); |
|
} |
|
|
|
List<Sprite> spritesOrigin; |
|
public List<Sprite> sprites;//보유 스킬 목록 |
|
[Space(10)] |
|
[Header("리스트 총 갯수 = 3 * listCount")] |
|
public int listCount = 8; |
|
int max = 0; |
|
float percent = 0.2f; |
|
[Range(0, 5)] |
|
public float duration = 1f; |
|
public AnimationCurve ease; |
|
Sprite DataGet(int i) |
|
{ |
|
return sprites[i]; |
|
} |
|
[Space(10)] |
|
public int idxSlot = 0; |
|
}
|
|
|