Skip to content

Instantly share code, notes, and snippets.

@neogeek
Last active July 2, 2020 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neogeek/4f253793fa026b829ee378f78c603fd4 to your computer and use it in GitHub Desktop.
Save neogeek/4f253793fa026b829ee378f78c603fd4 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CandyCoded
{
public class Form : MonoBehaviour
{
public Dictionary<string, string> GetFormValues()
{
return gameObject.GetComponentsInChildren<FormField>().Where(field => field.name != "")
.ToDictionary(field => field.name, field => field.value);
}
private EventSystem _eventSystem;
private void Awake()
{
_eventSystem = EventSystem.current;
}
private void Update()
{
if (!Input.GetKeyDown(KeyCode.Tab))
{
return;
}
var selectable = _eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
var next = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)
? selectable.FindSelectableOnUp()
: selectable.FindSelectableOnDown();
_eventSystem.SetSelectedGameObject(next.gameObject, null);
}
}
}
using UnityEngine;
namespace CandyCoded
{
public class FormField : MonoBehaviour
{
[SerializeField]
private string _name;
public new string name => _name;
public string value { get; internal set; }
public void SetValue(string value)
{
this.value = value;
}
public void SetValue(int value)
{
this.value = value.ToString();
}
}
}
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CandyCoded
{
public class FormLabel : MonoBehaviour, IPointerClickHandler
{
[SerializeField]
private Selectable _selectable;
private EventSystem _eventSystem;
private void Awake()
{
_eventSystem = EventSystem.current;
}
public void OnPointerClick(PointerEventData eventData)
{
_eventSystem.SetSelectedGameObject(_selectable.gameObject, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment