Refactored LanguageSelectViewBehaviour

This commit is contained in:
2018-09-18 17:40:10 +02:00
parent fe6ac87282
commit bd9a9150d9
6 changed files with 377 additions and 447 deletions

View File

@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -13,34 +14,41 @@ namespace RothenburgAR.UI
[SerializeField] private GameObject FlagPrefab;
public void Start()
{
CreateLanguageSelectionFlags();
}
private void CreateLanguageSelectionFlags()
{
foreach (var language in LanguageManager.Instance.AvailableLanguages)
{
var newFlag = GameObject.Instantiate(FlagPrefab, FlagContainer.transform);
var image = newFlag.GetComponentInChildren<Image>();
string path = language.FlagImagePath;
string name = language.Name;
if (File.Exists(path))
{
var bytes = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(4, 4, TextureFormat.RGBA32, false);
texture.LoadImage(bytes);
Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f));
image.sprite = sp;
image.preserveAspect = true;
}
newFlag.GetComponentInChildren<TextMeshProUGUI>().SetText(name);
var newFlag = InstantiateFlag(language);
newFlag.GetComponentInChildren<Button>().onClick.AddListener(() => { SwitchLanguage(language.Key); });
newFlag.SetActive(true);
}
}
private GameObject InstantiateFlag(LanguageDescription language)
{
var newFlag = GameObject.Instantiate(FlagPrefab, FlagContainer.transform);
newFlag.SetActive(true);
if (File.Exists(language.FlagImagePath))
{
var bytes = File.ReadAllBytes(language.FlagImagePath);
Texture2D texture = new Texture2D(4, 4, TextureFormat.RGBA32, false);
texture.LoadImage(bytes);
var image = newFlag.GetComponentInChildren<Image>();
image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
image.preserveAspect = true;
}
newFlag.GetComponentInChildren<TextMeshProUGUI>().SetText(language.Name);
return newFlag;
}
public void SwitchLanguage(string languageCode)
{
Debug.Log("Switching Language to '" + languageCode + "'");

View File

@@ -1,43 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RothenburgAR.UI
{
/// <summary>
/// Assuming that all children of this object are anchored to the center, this behaviour will move the object away from any border to have the specified padding
/// </summary>
public class PercentualPaddingBehaviour : MonoBehaviour
{
public float Padding = 0.01f;
void Start()
{
DisplayManager.Instance.OnOrientationChange.AddListener(RecalculateTransform);
RecalculateTransform();
}
void OnDestroy()
{
DisplayManager.Instance.OnOrientationChange.RemoveListener(RecalculateTransform);
}
public void RecalculateTransform()
{
var transform = this.GetComponent<RectTransform>();
float desiredPadding = Mathf.Max(Screen.height, Screen.width) * Padding;
var size = Mathf.Min(transform.rect.width, transform.rect.height);
// set padding when applicable
if (transform.anchorMax.x >= 0.99f) transform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, desiredPadding, size);
if (transform.anchorMin.x <= 0.01f) transform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, desiredPadding, size);
if (transform.anchorMax.y >= 0.99f) transform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, desiredPadding, size);
if (transform.anchorMin.y <= 0.01f) transform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, desiredPadding, size);
// make object square when needed (doesn't work as intended)
//transform.ForceUpdateRectTransforms();
//if (transform.anchorMin.x > 0.01f && transform.anchorMax.x < 0.99f) transform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, transform.rect.xMin, size);
//if (transform.anchorMin.y > 0.01f && transform.anchorMax.y < 0.99f) transform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, transform.rect.yMin, size);
}
}
}

View File

@@ -1,13 +0,0 @@
fileFormatVersion: 2
guid: fa4122a56a9a4e84a923d9e38fba6296
timeCreated: 1535990629
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: