基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
150
Assets/Modern UI Pack/Scripts/Input Field/CustomInputField.cs
Normal file
150
Assets/Modern UI Pack/Scripts/Input Field/CustomInputField.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
using TMPro;
|
||||
#if !ENABLE_LEGACY_INPUT_MANAGER
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[RequireComponent(typeof(TMP_InputField))]
|
||||
[RequireComponent(typeof(Animator))]
|
||||
public class CustomInputField : MonoBehaviour
|
||||
{
|
||||
[Header("Resources")]
|
||||
public TMP_InputField inputText;
|
||||
[SerializeField] private Animator inputFieldAnimator;
|
||||
|
||||
[Header("Settings")]
|
||||
public bool processSubmit = false;
|
||||
public bool clearOnSubmit = true;
|
||||
[Tooltip("Set the current event system object as null.")]
|
||||
[SerializeField] private bool setEventSystem = false;
|
||||
|
||||
[Header("Events")]
|
||||
public UnityEvent onSubmit = new UnityEvent();
|
||||
|
||||
// Hidden variables
|
||||
private float cachedDuration = 0.5f;
|
||||
private string inAnim = "In";
|
||||
private string outAnim = "Out";
|
||||
private string instaInAnim = "Instant In";
|
||||
private string instaOutAnim = "Instant Out";
|
||||
private bool isActive = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Initialize();
|
||||
|
||||
inputText.onSelect.AddListener(delegate { AnimateIn(); });
|
||||
inputText.onEndEdit.AddListener(delegate { HandleEndEdit(); });
|
||||
inputText.onValueChanged.AddListener(delegate { UpdateState(); });
|
||||
|
||||
UpdateStateInstant();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (inputText == null || inputFieldAnimator == null) { Initialize(); }
|
||||
inputText.ForceLabelUpdate();
|
||||
UpdateStateInstant();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!processSubmit || string.IsNullOrEmpty(inputText.text) || EventSystem.current.currentSelectedGameObject != inputText.gameObject)
|
||||
return;
|
||||
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
if (Input.GetKeyDown(KeyCode.Return))
|
||||
{
|
||||
onSubmit.Invoke();
|
||||
|
||||
if (clearOnSubmit)
|
||||
{
|
||||
inputText.text = "";
|
||||
UpdateState();
|
||||
}
|
||||
}
|
||||
#elif ENABLE_INPUT_SYSTEM
|
||||
if (Keyboard.current.enterKey.wasPressedThisFrame)
|
||||
{
|
||||
onSubmit.Invoke();
|
||||
|
||||
if (clearOnSubmit)
|
||||
{
|
||||
inputText.text = "";
|
||||
UpdateState();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
if (inputText == null) { inputText = gameObject.GetComponent<TMP_InputField>(); }
|
||||
if (inputFieldAnimator == null) { inputFieldAnimator = gameObject.GetComponent<Animator>(); }
|
||||
}
|
||||
|
||||
public void AnimateIn()
|
||||
{
|
||||
if (inputFieldAnimator.gameObject.activeInHierarchy && !isActive)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
|
||||
isActive = true;
|
||||
inputFieldAnimator.enabled = true;
|
||||
inputFieldAnimator.Play(inAnim);
|
||||
}
|
||||
}
|
||||
|
||||
public void AnimateOut()
|
||||
{
|
||||
if (inputFieldAnimator.gameObject.activeInHierarchy && inputText.text.Length == 0 && isActive)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
|
||||
isActive = false;
|
||||
inputFieldAnimator.enabled = true;
|
||||
inputFieldAnimator.Play(outAnim);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateState()
|
||||
{
|
||||
if (inputText.text.Length == 0) { AnimateOut(); }
|
||||
else { AnimateIn(); }
|
||||
}
|
||||
|
||||
public void UpdateStateInstant()
|
||||
{
|
||||
inputFieldAnimator.enabled = true;
|
||||
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
|
||||
if (inputText.text.Length == 0) { isActive = false; inputFieldAnimator.Play(instaOutAnim); }
|
||||
else { isActive = true; inputFieldAnimator.Play(instaInAnim); }
|
||||
}
|
||||
|
||||
void HandleEndEdit()
|
||||
{
|
||||
if (setEventSystem && string.IsNullOrEmpty(inputText.text) && !EventSystem.current.alreadySelecting && EventSystem.current.currentSelectedGameObject == inputText.gameObject)
|
||||
{
|
||||
EventSystem.current.SetSelectedGameObject(null);
|
||||
}
|
||||
|
||||
AnimateOut();
|
||||
}
|
||||
|
||||
IEnumerator DisableAnimator()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(cachedDuration);
|
||||
inputFieldAnimator.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c65c7917835d8a04b94c8b906234b09e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: e956b4f7a85075c43a444a9f05cc765a, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Input Field/CustomInputField.cs
|
||||
uploadId: 628721
|
||||
Reference in New Issue
Block a user