using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// ChangeUserScreen scene manager /// public class ChangeUserScreen : MonoBehaviour { /// /// Prefab of the user selection /// public GameObject userPrefab; /// /// Reference to the container to list all users /// public Transform usersContainer; /// /// UI Reference to the error GameObject to display an error message /// public GameObject error; /// /// Index of the current selected user in the UserList /// private int currentUserIndex; /// /// List of references to avatar background sprites (so we can color them nicely) /// private List userBackgrounds = new List(); /// /// Start is called before the first frame update /// void Start() { PersistentDataController.GetInstance().Load(); error.SetActive(false); DisplayUsers(); } /// /// Add all users to the container to display them /// private void DisplayUsers() { foreach (Transform child in usersContainer) Destroy(child.gameObject); List users = UserList.GetUsers(); currentUserIndex = UserList.IndexOf(UserList.GetCurrentUser().GetUsername()); for (int i = 0; i < users.Count; i++) { User user = users[i]; // Create instance of prefab GameObject instance = GameObject.Instantiate(userPrefab, usersContainer); // Store value of i so we can use it the callback (else it would get the value of sprites.Count) int x = i; // Add user content UserCard card = instance.GetComponent(); card.user = user; card.selectUser = () => UpdateSelection(x); card.updateUserCardContainer = DisplayUsers; card.displayError = () => error.SetActive(true); // Store reference to image for fancy coloring Image background = instance.GetComponent(); userBackgrounds.Add(background); // Set background color background.color = i == currentUserIndex ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray; } } /// /// Update the current selected user /// /// Index to the user in the this.userBackgrounds list private void UpdateSelection(int index) { userBackgrounds[currentUserIndex].color = Color.gray; currentUserIndex = index; userBackgrounds[currentUserIndex].color = new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f); } /// /// Select the current selected user and return to the StartScreenScene /// public void IChooseYou() { UserList.ChangeCurrentUser(currentUserIndex); SystemController.GetInstance().BackToPreviousScene(); } /// /// Callback to load the UserCreationScreen scene /// public void GotoUserCreation() { SystemController.GetInstance().LoadNextScene("Accounts/Scenes/UserCreationScreen"); } }