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(); list.Start(); LoadIndicies(); list.OnSelect.AddListener(SaveIndicies); list.OnDeselect.AddListener(SaveIndicies); } private void LoadIndicies() { if (PlayerPrefs.HasKey(Key)) { List 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 String2Indicies(string str) { if (str == string.Empty) { return new List(); } return str.Split(',').Select(int.Parse).ToList(); } private string Indicies2String(List indicies) { if (indicies == null || indicies.Count == 0) { return string.Empty; } return string.Join(",", indicies.Select((int x) => x.ToString()).ToArray()); } } }