Level Selection related stuff added
This commit is contained in:
16
Assets/Scripts/UI/LevelButton.cs
Normal file
16
Assets/Scripts/UI/LevelButton.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Level;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LevelButton : MonoBehaviour
|
||||
{
|
||||
private TMP_Text text = null;
|
||||
|
||||
private void Awake() => text = GetComponentInChildren<TMP_Text>();
|
||||
|
||||
public void SetLevel(string levelName) => text.text = levelName;
|
||||
public void StartLevel() => LevelManager.Instance.SwitchToLevel(text.text);
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/LevelButton.cs.meta
Normal file
11
Assets/Scripts/UI/LevelButton.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 159a88a420256634c8758e2b1501c29a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
55
Assets/Scripts/UI/LevelSelectionMenu.cs
Normal file
55
Assets/Scripts/UI/LevelSelectionMenu.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using Level;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LevelSelectionMenu : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Vector2Int maxGridSize = Vector2Int.one;
|
||||
private GameObject levelButtonPrefab = null;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
levelButtonPrefab = Resources.Load<GameObject>("UI/Level Button Variant");
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
LevelButton instance = null;
|
||||
|
||||
int rowIndex = 0;
|
||||
int columnIndex = 0;
|
||||
int i = 0;
|
||||
int count = LevelManager.Instance.Levels.Values.Count;
|
||||
|
||||
RectTransform rectTransform = levelButtonPrefab.transform.GetComponent<RectTransform>();
|
||||
float rowOffset = (int)(count / maxGridSize.x) * rectTransform.rect.height * 0.75f;
|
||||
float columnOffset = -maxGridSize.x * rectTransform.rect.width * 0.75f;
|
||||
if (maxGridSize.x % 2 == 1)
|
||||
columnOffset += rectTransform.rect.width * 0.75f;
|
||||
|
||||
RectTransform instanceRectTransform = null;
|
||||
Vector3 instancePosition = Vector3.zero;
|
||||
foreach (var level in LevelManager.Instance.Levels.Values)
|
||||
{
|
||||
if (i > maxGridSize.x * maxGridSize.y)
|
||||
break;
|
||||
|
||||
|
||||
rowIndex = i / maxGridSize.x;
|
||||
columnIndex = i % maxGridSize.x;
|
||||
|
||||
instance = Instantiate(levelButtonPrefab, transform.position, Quaternion.identity, transform).GetComponent<LevelButton>();
|
||||
instance.SetLevel(level.name);
|
||||
instanceRectTransform = instance.transform.GetComponent<RectTransform>();
|
||||
|
||||
instancePosition.y = rowOffset - rowIndex * instanceRectTransform.rect.height * 1.5f;
|
||||
instancePosition.x = columnOffset + columnIndex * instanceRectTransform.rect.width * 1.5f;
|
||||
instanceRectTransform.localPosition = instancePosition;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/LevelSelectionMenu.cs.meta
Normal file
11
Assets/Scripts/UI/LevelSelectionMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c861d2bd3b90214da0c159643ba1528
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -29,6 +29,8 @@ namespace UI
|
||||
|
||||
public void CloseAllCanvases()
|
||||
{
|
||||
Initialize();
|
||||
|
||||
foreach (Canvas canvas in canvases.Values)
|
||||
canvas.gameObject.SetActive(false);
|
||||
}
|
||||
|
Reference in New Issue
Block a user