Skip to content

Instantly share code, notes, and snippets.

@jackyli-work
Created February 29, 2020 07:16
Show Gist options
  • Save jackyli-work/9bc1a52ddfc27b0c6e5c8db52cbebf8a to your computer and use it in GitHub Desktop.
Save jackyli-work/9bc1a52ddfc27b0c6e5c8db52cbebf8a to your computer and use it in GitHub Desktop.
Native Input Utility
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace JFun.Gameplay.CreateWorld.UI
{
public class CWCustomInputField : InputField, ICancelHandler
{
// Action others can subsribe
// argument 2 : true: enter key, false: cancel key
public event System.Action<string, bool> OnCustomEndEdit = null;
public Button _submitButton;
//private System.Collections.Generic.List<RaycastResult> _list = new System.Collections.Generic.List<RaycastResult>();
//private GraphicRaycaster _caster = null;
private bool _isbtnclicked = false;
//public override void OnPointerClick(PointerEventData eventData)
//{
// base.OnPointerClick(eventData);
// if (eventData.clickCount > 2)
// {
// SelectAll();
// }
//}
// Need to presss 'enter' twice in order to receive this event. NOT USING IT!
//public override void OnSubmit(BaseEventData eventData)
//{
// base.OnSubmit(eventData);
//}
#if UNITY_EDITOR
public override void OnDeselect(BaseEventData eventData)
{
if (OnCustomEndEdit != null)
{
OnCustomEndEdit(this.text, _isbtnclicked);
}
else
base.OnDeselect(eventData);
}
#endif
// Need to presss 'esc' twice in order to receive this event. NOT USING IT!
public void OnCancel(BaseEventData eventData)
{
}
protected override void Awake()
{
onEndEdit.AddListener(OnEndEdit);
}
private void Update()
{
if (!_submitButton)
_submitButton = GetComponentInChildren<Button>(true);
//if (!_caster)
// _caster = GetComponentInParent<GraphicRaycaster>();
}
protected override void OnEnable()
{
base.OnEnable();
this.ActivateInputField();
this.Select();
}
// This event happens before Unity calls the Update() funcion
public void OnEndEdit(string value)
{
if (OnCustomEndEdit == null)
return;
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
{
OnCustomEndEdit(this.text, true);
//Debug.Log("Enter Key: " + this.text);
}
else if (Input.GetKeyDown(KeyCode.Escape))
{
OnCustomEndEdit(this.text, false);
//Debug.Log("Esc Key");
}
else
{
if (isFocused)
OnCustomEndEdit(this.text, _isbtnclicked);
}
#else
//CheckClick();
OnCustomEndEdit(this.text, !wasCanceled);
#endif
}
private void CheckClick()
{
_isbtnclicked = false;
//if (_submitButton && _caster)
//{
// PointerEventData pointData = new PointerEventData(EventSystem.current);
// pointData.pressPosition = Input.mousePosition;
// pointData.position = Input.mousePosition;
// _list.Clear();
// _caster.Raycast(pointData, _list);
// if (_list.Exists(result => result.gameObject.Equals(_submitButton.gameObject)))
// {
// _isbtnclicked = true;
// //SendOnSubmit();
// }
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JFun.Framework
{
public static class DelayDoHelper
{
private const long TicksPerSecond = TimeSpan.TicksPerMillisecond * 1000;
private static Dictionary<float, WaitForSeconds> _waitCache = new Dictionary<float, WaitForSeconds>();
public static WaitForSeconds GetWaitForSeconds(float delaySec)
{
WaitForSeconds waitCache;
if (!_waitCache.TryGetValue(delaySec, out waitCache))
{
waitCache = new WaitForSeconds(delaySec);
_waitCache.Add(delaySec, waitCache);
}
return waitCache;
}
public static Coroutine DelayDoAction(Action action, IEnumerator retFunc)
{
if (Singleton.Helper)
{
return Singleton.Helper.StartCoroutine(IDelayDoAction(action, retFunc));
}
return null;
}
public static Coroutine DelayDoAction(Action action, float delaySec)
{
if (Singleton.Helper)
{
return Singleton.Helper.StartCoroutine(IDelayDoAction(action, delaySec));
}
return null;
}
public static Coroutine DelayDoAction(Action action, float delaySec, float intervalSec)
{
if (Singleton.Helper)
{
return Singleton.Helper.StartCoroutine(IDelayDoAction(action, delaySec, intervalSec));
}
return null;
}
public static Coroutine DelayDoAction(Action action)
{
if (Singleton.Helper)
{
return Singleton.Helper.StartCoroutine(IDelayDoAction(action, null));
}
return null;
}
public static void StopDelayDo(Coroutine coroutine)
{
if (Singleton.Helper)
{
Singleton.Helper.StopCoroutine(coroutine);
}
}
public static IEnumerator IDelayDoAction(Action action, int delayMilliSec)
{
yield return IDelayDoAction(action, (float)delayMilliSec / 1000.0f);
}
public static IEnumerator IDelayDoAction(Action action, float delaySec)
{
yield return GetWaitForSeconds(delaySec);
yield return IDelayDoAction(action, null);
}
public static IEnumerator IDelayDoAction(Action action, float delaySec, float intervalSec)
{
long ticks = DateTime.Now.Ticks;
long nextTick = DateTime.Now.Ticks + (long)(delaySec * TicksPerSecond);
long period = (long)(intervalSec * TicksPerSecond);
yield return GetWaitForSeconds(delaySec);
while (true)
{
yield return IDelayDoAction(action, null);
nextTick += period;
var interval = nextTick - DateTime.Now.Ticks;
if (interval > 0)
{
yield return GetWaitForSeconds(interval / (float)TicksPerSecond);
}
}
}
public static IEnumerator IDelayDoAction(Action action, IEnumerator retFunc)
{
yield return retFunc;
if (action != null)
{
action();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment