60 lines
1.3 KiB
C#
60 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UIWidgets;
|
|
using UnityEngine;
|
|
|
|
namespace UIWidgetsSamples
|
|
{
|
|
[RequireComponent(typeof(ListViewBase))]
|
|
public class ListViewSaveIndicies : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
public string Key = "Unique Key";
|
|
|
|
[SerializeField]
|
|
private ListViewBase list;
|
|
|
|
private void Start()
|
|
{
|
|
list = GetComponent<ListViewBase>();
|
|
list.Start();
|
|
LoadIndicies();
|
|
list.OnSelect.AddListener(SaveIndicies);
|
|
list.OnDeselect.AddListener(SaveIndicies);
|
|
}
|
|
|
|
private void LoadIndicies()
|
|
{
|
|
if (PlayerPrefs.HasKey(Key))
|
|
{
|
|
List<int> list = String2Indicies(PlayerPrefs.GetString(Key));
|
|
list.RemoveAll((int x) => !this.list.IsValid(x));
|
|
this.list.SelectedIndicies = list;
|
|
}
|
|
}
|
|
|
|
private void SaveIndicies(int index, ListViewItem component)
|
|
{
|
|
PlayerPrefs.SetString(Key, Indicies2String(list.SelectedIndicies));
|
|
}
|
|
|
|
private List<int> String2Indicies(string str)
|
|
{
|
|
if (str == string.Empty)
|
|
{
|
|
return new List<int>();
|
|
}
|
|
return str.Split(',').Select(int.Parse).ToList();
|
|
}
|
|
|
|
private string Indicies2String(List<int> indicies)
|
|
{
|
|
if (indicies == null || indicies.Count == 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
return string.Join(",", indicies.Select((int x) => x.ToString()).ToArray());
|
|
}
|
|
}
|
|
}
|