88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using System;
|
|
using DanielLochner.Assets.SimpleScrollSnap;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class ItemSwapper : MonoBehaviour
|
|
{
|
|
#region Fields
|
|
|
|
[SerializeField]
|
|
protected GameObject m_panel, m_toggle;
|
|
|
|
private float m_toggleWidth;
|
|
private SimpleScrollSnap m_sss;
|
|
|
|
#endregion Fields
|
|
|
|
#region Methods
|
|
|
|
private void Awake()
|
|
{
|
|
m_sss = gameObject.FindComponentDownHierarchy<SimpleScrollSnap>();
|
|
if (m_toggle)
|
|
m_toggleWidth = m_toggle.GetComponent<RectTransform>().sizeDelta.x * (Screen.width / 2048f); ;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
for (int i = 0; i < 5; i++)
|
|
AddToBack();
|
|
}
|
|
|
|
public void AddToFront()
|
|
{
|
|
Add(0);
|
|
}
|
|
|
|
public void AddToBack()
|
|
{
|
|
Add(m_sss.NumberOfPanels);
|
|
}
|
|
|
|
private void Add(int index)
|
|
{
|
|
//Pagination
|
|
Instantiate(m_toggle, m_sss.pagination.transform.position + new Vector3(m_toggleWidth * (m_sss.NumberOfPanels + 1), 0, 0), Quaternion.identity, m_sss.pagination.transform);
|
|
m_sss.pagination.transform.position -= new Vector3(m_toggleWidth / 2f, 0, 0);
|
|
|
|
//Panel
|
|
m_panel.GetComponent<Image>().color = new Color(UnityEngine.Random.Range(0, 255) / 255f, UnityEngine.Random.Range(0, 255) / 255f, UnityEngine.Random.Range(0, 255) / 255f);
|
|
m_sss.Add(m_panel, index);
|
|
}
|
|
|
|
public void RemoveFromFront()
|
|
{
|
|
Remove(0);
|
|
}
|
|
|
|
public void RemoveFromBack()
|
|
{
|
|
if (m_sss.NumberOfPanels > 0)
|
|
{
|
|
Remove(m_sss.NumberOfPanels - 1);
|
|
}
|
|
else
|
|
{
|
|
Remove(0);
|
|
}
|
|
}
|
|
|
|
private void Remove(int index)
|
|
{
|
|
if (m_sss.NumberOfPanels > 0)
|
|
{
|
|
//Pagination
|
|
DestroyImmediate(m_sss.pagination.transform.GetChild(m_sss.NumberOfPanels - 1).gameObject);
|
|
m_sss.pagination.transform.position += new Vector3(m_toggleWidth / 2f, 0, 0);
|
|
|
|
//Panel
|
|
m_sss.Remove(index);
|
|
}
|
|
}
|
|
|
|
#endregion Methods
|
|
}
|
|
} |