Resolve WES-56 "Account switching"
This commit is contained in:
committed by
Jelle De Geest
parent
0ea743354b
commit
b6b863183e
90
Assets/Accounts/Scripts/ChangeUserScreen.cs
Normal file
90
Assets/Accounts/Scripts/ChangeUserScreen.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ChangeUserScreen : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Prefab of the user selection
|
||||
/// </summary>
|
||||
public GameObject userPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the container to list all users
|
||||
/// </summary>
|
||||
public Transform usersContainer;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the user list
|
||||
/// </summary>
|
||||
public UserList userList;
|
||||
|
||||
/// <summary>
|
||||
/// Index of the current selected user in the UserList
|
||||
/// </summary>
|
||||
private int currentUserIndex;
|
||||
|
||||
/// <summary>
|
||||
/// List of references to avatar background sprites (so we can color them nicely)
|
||||
/// </summary>
|
||||
private List<Image> userBackgrounds = new List<Image>();
|
||||
|
||||
/// <summary>
|
||||
/// Start is called before the first frame update
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
List<User> users = userList.GetUsers();
|
||||
currentUserIndex = userList.GetCurrentUserIndex();
|
||||
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 onClick callback
|
||||
instance.GetComponent<Button>().onClick.AddListener(() => UpdateSelection(x));
|
||||
// Set username
|
||||
instance.GetComponentInChildren<TMP_Text>().text = user.username;
|
||||
|
||||
// Store reference to image for fancy coloring
|
||||
Image background = instance.GetComponent<Image>();
|
||||
userBackgrounds.Add(background);
|
||||
// Set background color
|
||||
background.color = i == currentUserIndex ? Color.blue : Color.gray;
|
||||
// Find correct component for setting the sprite
|
||||
foreach (Image img in background.GetComponentsInChildren<Image>())
|
||||
if (img != background)
|
||||
{
|
||||
img.sprite = user.avatar;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the current selected user
|
||||
/// </summary>
|
||||
/// <param name="index">Index to the user in the <c>this.userBackgrounds</c> list</param>
|
||||
private void UpdateSelection(int index)
|
||||
{
|
||||
userBackgrounds[currentUserIndex].color = Color.gray;
|
||||
currentUserIndex = index;
|
||||
userBackgrounds[currentUserIndex].color = Color.blue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Select the current selected user and return to the StartScreenScene
|
||||
/// </summary>
|
||||
public void IChooseYou()
|
||||
{
|
||||
userList.ChangeCurrentUser(currentUserIndex);
|
||||
userList.Save();
|
||||
SceneManager.LoadScene("Common/Scenes/StartScreen");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user