Merge branch 'WES-56-users' into 'development'

Implement simple user creation system

See merge request wesign/unity-application!19
This commit was merged in pull request #19.
This commit is contained in:
Victor Mylle
2023-03-08 10:13:11 +00:00
31 changed files with 2503 additions and 140 deletions

View File

@@ -2,6 +2,9 @@ kind: pipeline
name: sonarcube
type: docker
clone:
retries: 10
steps:
- name: code-analysis
pull: if-not-exists

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,212 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &6263784958278830195
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6263784958278830196}
- component: {fileID: 6263784958278830198}
- component: {fileID: 6263784958278830197}
m_Layer: 5
m_Name: Image
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &6263784958278830196
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784958278830195}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 6263784960026409965}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6263784958278830198
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784958278830195}
m_CullTransparentMesh: 1
--- !u!114 &6263784958278830197
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784958278830195}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 0}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &6263784960026409964
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6263784960026409965}
- component: {fileID: 6263784960026409952}
- component: {fileID: 6263784960026409967}
- component: {fileID: 6263784960026409966}
m_Layer: 5
m_Name: Avatar
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &6263784960026409965
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784960026409964}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 6263784958278830196}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6263784960026409952
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784960026409964}
m_CullTransparentMesh: 1
--- !u!114 &6263784960026409967
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784960026409964}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!114 &6263784960026409966
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6263784960026409964}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 6263784960026409967}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 0}
m_TargetAssemblyTypeName:
m_MethodName:
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName:
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c09ef5423059da444ad7743174e9f077
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -286,6 +286,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2823887524698191631, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
--- !u!224 &289602555 stripped
@@ -919,8 +923,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0fac0bf527487ad48835aff400b1f762, type: 3}
m_Name:
m_EditorClassIdentifier:
item_container: {fileID: 461792789}
item_prefab: {fileID: 4959898007614330357, guid: baf585123c6364d40a17b89676eb350d, type: 3}
itemContainer: {fileID: 461792789}
itemPrefab: {fileID: 4959898007614330357, guid: baf585123c6364d40a17b89676eb350d, type: 3}
numberOfItems: 100
itemTitle:
- les XYZ
@@ -1577,6 +1581,10 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 906197777}
m_Modifications:
- target: {fileID: 8299246693487308512, guid: 3bccdf365a4fbea4d8fa1aa461d3dc5c, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
- target: {fileID: 8299246693487308514, guid: 3bccdf365a4fbea4d8fa1aa461d3dc5c, type: 3}
propertyPath: m_Name
value: ButtonBack

View File

@@ -1071,8 +1071,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0fac0bf527487ad48835aff400b1f762, type: 3}
m_Name:
m_EditorClassIdentifier:
item_container: {fileID: 1832508913}
item_prefab: {fileID: 4959898007614330357, guid: baf585123c6364d40a17b89676eb350d, type: 3}
itemContainer: {fileID: 1832508913}
itemPrefab: {fileID: 4959898007614330357, guid: baf585123c6364d40a17b89676eb350d, type: 3}
numberOfItems: 100
itemTitle:
- minigame ABC
@@ -1708,6 +1708,10 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 1380805077}
m_Modifications:
- target: {fileID: 8299246693487308512, guid: 3bccdf365a4fbea4d8fa1aa461d3dc5c, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
- target: {fileID: 8299246693487308514, guid: 3bccdf365a4fbea4d8fa1aa461d3dc5c, type: 3}
propertyPath: m_Name
value: ButtonBack
@@ -2109,6 +2113,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2823887524698191631, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
--- !u!224 &1840395957 stripped

View File

@@ -282,6 +282,10 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 906197777}
m_Modifications:
- target: {fileID: 8299246693487308512, guid: 3bccdf365a4fbea4d8fa1aa461d3dc5c, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
- target: {fileID: 8299246693487308514, guid: 3bccdf365a4fbea4d8fa1aa461d3dc5c, type: 3}
propertyPath: m_Name
value: ButtonBack
@@ -695,6 +699,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2823887524698191631, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
--- !u!224 &2035873552 stripped

View File

@@ -775,6 +775,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2823887524698191631, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_StringArgument
value: Common/Scenes/StartScreen
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 53b0d00ac817ebf43b61bea2d160dd66, type: 3}
--- !u!224 &790881345 stripped

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 401cfc17c01f88847b434260a6a93933
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 56c7d643409f9c446bcb1014f14ac1dd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3f3d6d68c3c3db64e91cf5ec9537ccda, type: 3}
m_Name: UserList
m_EditorClassIdentifier:
userTemplate: {fileID: 11400000, guid: 9195fb19688f1834fac39d356f965c20, type: 2}
users: []

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 072bec636a40f7e4e93b0ac624a3bda2
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3c6c5919d9f747143b377c2bc34cd28b, type: 3}
m_Name: UserTemplate
m_EditorClassIdentifier:
username:
avatar: {fileID: 21300000, guid: 0c9156aa9168241eaacc01c996d6aa97, type: 3}
playtime: 0
courses: []
minigames: []

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9195fb19688f1834fac39d356f965c20
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dc83c2f6756fa3e43976e529562dd622
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
[Serializable]
public class Progress
{
[Serializable]
// Helper class to serialize into byte[]
protected class DataEntry
{
public string key;
public List<byte> bytes = new List<byte>();
public DataEntry(string key, byte[] data)
{
this.key = key;
this.bytes = new List<byte>(data);
}
}
// TODO: use inheritance to create seperate MinigameProgress and CourseProgress
[Header("Course or Minigame")]
// TODO: change to course/minigame ScriptableObject reference
// Index of item in courses/minigame list object
public int index;
[SerializeField]
// values belonging to a certain key, in List (which can be serialized)
private List<DataEntry> entries = new List<DataEntry>();
// Add new `key` := `value`, returns `true` if successful
public bool Add<T>(string key, T data)
{
if (data == null)
return false;
// Search for already existing key
foreach (DataEntry entry in entries)
{
if (entry.key == key)
{
return false;
}
}
// Hacky serialization stuff
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, data);
entries.Add(new DataEntry(key, ms.ToArray()));
return true;
}
}
// Get the value of type `T` belonging to `key`
public T Get<T>(string key)
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
// Find the correct key
foreach (DataEntry entry in entries)
{
if (entry.key == key)
{
// Hacky serialization stuff
byte[] data = entry.bytes.ToArray();
ms.Write(data, 0, data.Length);
ms.Seek(0, SeekOrigin.Begin);
return (T)bf.Deserialize(ms);
}
}
}
// Raise an exception when key is not found
throw new KeyNotFoundException();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 183eab19332f33a48a745b7b264611fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Create new Scriptable/User/User")]
public class User : ScriptableObject
{
[Header("Personal data")]
// User nickname
public string username;
// User avatar
public Sprite avatar;
[Header("Personal settings")]
// TODO: set personal settings and preferences
[Header("Progress")]
// Total playtime
public double playtime;
[SerializeField]
// List of courses a user started/completed
public List<Progress> courses = new List<Progress>();
[SerializeField]
// List of minigames a user played
public List<Progress> minigames = new List<Progress>();
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3c6c5919d9f747143b377c2bc34cd28b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,101 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UserCreationScreen : MonoBehaviour
{
// Max length of a username
private const int MAX_USERNAME_LENGTH = 12;
[Header("UI References")]
// Reference to the input text field for username
public TMP_InputField inputName;
// Reference to the avatar-list container
public Transform avatarsContainer;
[Header("Prefab")]
// Avatar prefab
public GameObject avatarPrefab;
// List of all sprites that are supported as avatars
public List<Sprite> sprites = new List<Sprite>();
[Header("Users List")]
// Reference to the UserList ScriptableObject
public UserList users;
[SerializeField]
// Current selected avatar
private int selectedAvatar = 0;
// List of references to avatar background sprites (so we can color them nicely)
private List<Image> avatars = new List<Image>();
void Start()
{
for (int i = 0; i < sprites.Count; i++)
{
// Create instance of prefab
GameObject instance = GameObject.Instantiate(avatarPrefab, avatarsContainer);
// 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(() => UpdateAvatar(x));
// Store reference to image for fancy coloring
Image background = instance.GetComponent<Image>();
avatars.Add(background);
// Set background color
background.color = selectedAvatar == i ? Color.blue : Color.gray;
// Find correct component for setting the sprite
foreach (Image img in background.GetComponentsInChildren<Image>())
if (img != background)
{
img.sprite = sprites[i];
break;
}
}
}
// Update the current selected avatar
private void UpdateAvatar(int newAvatar)
{
avatars[selectedAvatar].color = Color.gray;
selectedAvatar = newAvatar;
avatars[selectedAvatar].color = Color.blue;
}
// Check if a given string is a correct username (using Regex)
static public bool IsValidUsername(string username)
{
return new Regex($@"^[abcdefghijklmnopqrstuvwxyz]{{1,{MAX_USERNAME_LENGTH}}}$").IsMatch(username);
}
// Create a new user (will be called by button)
public void CreateUser()
{
string username = inputName.text;
if (IsValidUsername(username))
{
if (!users.GetUserByUsername(username))
{
// Create a new entry in the UserList ScriptableObject
users.CreateAndAddNewUser(username, sprites[selectedAvatar]);
// TODO: change scene, for now just change to StartScreen
SceneManager.LoadScene("Common/Scenes/StartScreen");
}
// TODO: give more feedback to user
// Warn user that username already exists
else Debug.LogWarning($"Username '{username}' already exists!");
}
// TODO: give more feedback to user
// Warn user that username is invalid
else Debug.LogWarning($"Invalid username '{username}'!");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fc3902e35c042b14f83b24498d31d587
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu(menuName = "Create new Scriptable/User/List")]
public class UserList : ScriptableObject
{
[Header("Template")]
// Reference to User template
public ScriptableObject userTemplate;
[Header("Users")]
// List of users
public List<User> users = new List<User>();
// Create a new User
public User CreateNewUser(string name, Sprite avatar)
{
User user = ScriptableObject.CreateInstance<User>();
user.username = name;
user.avatar = avatar;
return user;
}
// Create a new User and add to list
public User CreateAndAddNewUser(string name, Sprite avatar)
{
User user = CreateNewUser(name, avatar);
users.Add(user);
EditorUtility.SetDirty(this);
AssetDatabase.CreateAsset(user, $"Assets/Common/ScriptableObjects/Users/{name}.asset");
AssetDatabase.SaveAssets();
return user;
}
// Get user by username, returns `null` if no user can be found with such name
public User GetUserByUsername(string username)
{
foreach (User user in users)
if (user.username == username) return user;
return null;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3f3d6d68c3c3db64e91cf5ec9537ccda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Common/Tests.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9eb837b38adfbfb4f8f440b677afc7ee
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,166 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
public class TestProgress : MonoBehaviour
{
[Serializable]
// Dummy struct
private struct SerializableStruct
{
public int r, g, b;
public float x, y, z;
}
private struct NonSerializableStruct
{
public int r, g, b;
public float x, y, z;
}
// Helper method, returns true if `Progress.Get(...)` throws a `KeyNotFoundException`
private bool AddNonSerializableStruct()
{
Progress progress = new Progress();
NonSerializableStruct nss = new NonSerializableStruct();
try { progress.Add<NonSerializableStruct>("key", nss); }
catch (SerializationException) { return true; }
return false;
}
// Helper method, returns true if `Progress.Get(...)` throws a `KeyNotFoundException`
private bool AccessInvalidKey()
{
Progress progress = new Progress();
try { progress.Get<int>("non-existing key"); }
catch (KeyNotFoundException) { return true; }
return false;
}
// Helper method, returns true if `Progress.Get(...)` throws a `InvalidCastException`
private bool AccessInvalidType()
{
Progress progress = new Progress();
progress.Add<int>("key", 123456789);
try { progress.Get<double>("key"); }
catch (InvalidCastException) { return true; }
return false;
}
void Start()
{
TestNewProgress();
TestProgressAddInvalidData();
TestProgressAddDuplicateKey();
TestProgressAddInt();
TestProgressAddDouble();
TestProgressAddString();
TestProgressAddSerializableStruct();
TestProgressAddNonSerializableStruct();
TestProgressGetInvalidKey();
TestProgressGetInvalidType();
TestProgressGetInt();
TestProgressGetDouble();
TestProgressGetString();
TestProgressGetStruct();
}
public void TestNewProgress()
{
Progress progress = new Progress();
Debug.Assert(progress != null);
}
public void TestProgressAddInvalidData()
{
Progress progress = new Progress();
Debug.Assert(!progress.Add<GameObject>("key", null));
}
public void TestProgressAddDuplicateKey()
{
Progress progress = new Progress();
progress.Add<int>("key 1", 0);
Debug.Assert(!progress.Add<int>("key 1", 1));
}
public void TestProgressAddInt()
{
Progress progress = new Progress();
Debug.Assert(progress.Add<int>("key", 1));
}
public void TestProgressAddDouble()
{
Progress progress = new Progress();
Debug.Assert(progress.Add<double>("key", 1.0));
}
public void TestProgressAddString()
{
Progress progress = new Progress();
Debug.Assert(progress.Add<string>("key", "Hello World!"));
}
public void TestProgressAddSerializableStruct()
{
Progress progress = new Progress();
Debug.Assert(progress.Add<SerializableStruct>("key", new SerializableStruct()));
}
public void TestProgressAddNonSerializableStruct()
{
Debug.Assert(AddNonSerializableStruct());
}
public void TestProgressGetInvalidKey()
{
Debug.Assert(AccessInvalidKey());
}
public void TestProgressGetInvalidType()
{
Debug.Assert(AccessInvalidType());
}
public void TestProgressGetInt()
{
Progress progress = new Progress();
progress.Add<int>("key", 1);
Debug.Assert(progress.Get<int>("key") == 1);
}
public void TestProgressGetDouble()
{
Progress progress = new Progress();
progress.Add<double>("key", 1.0);
Debug.Assert(progress.Get<double>("key") == 1.0);
}
public void TestProgressGetString()
{
Progress progress = new Progress();
progress.Add<string>("key", "Hello World!");
Debug.Assert(progress.Get<string>("key") == "Hello World!");
}
public void TestProgressGetStruct()
{
Progress progress = new Progress();
int R = 1, G = 10, B = 100;
float X = 0.1f, Y = 0.01f, Z = 0.001f;
SerializableStruct data = new SerializableStruct { r = R, g = G, b = B, x = X, y = Y, z = Z };
progress.Add<SerializableStruct>("key", data);
SerializableStruct result = progress.Get<SerializableStruct>("key");
Debug.Assert(result.r == R);
Debug.Assert(result.g == G);
Debug.Assert(result.b == B);
Debug.Assert(result.x == X);
Debug.Assert(result.y == Y);
Debug.Assert(result.z == Z);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 49445c42f43d1bb488f588623826a39e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestUserCreationScreen : MonoBehaviour
{
void Start()
{
TestIsValidUsernameTrue();
TestIsValidUsernameFalse();
}
public void TestIsValidUsernameTrue()
{
foreach (char c in "abcdefghijklmnopqrstuvwxyz")
Debug.Assert(UserCreationScreen.IsValidUsername(c.ToString()));
Debug.Assert(UserCreationScreen.IsValidUsername("abcdefghijkl"));
}
public void TestIsValidUsernameFalse()
{
Debug.Assert(!UserCreationScreen.IsValidUsername(string.Empty));
foreach (char c in " \n\t0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-*/%_(){}[]\\")
Debug.Assert(!UserCreationScreen.IsValidUsername(c.ToString()));
Debug.Assert(!UserCreationScreen.IsValidUsername("abcdefghijklmnopqrstuvwxyz"));
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b6dabaf99e10900459274641f4cc9010
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,7 +4,7 @@
"com.unity.ide.rider": "3.0.18",
"com.unity.ide.visualstudio": "2.0.17",
"com.unity.ide.vscode": "1.2.5",
"com.unity.test-framework": "1.1.31",
"com.unity.test-framework": "1.1.33",
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.6.4",
"com.unity.ugui": "1.0.0",

View File

@@ -40,7 +40,7 @@
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.31",
"version": "1.1.33",
"depth": 0,
"source": "registry",
"dependencies": {