FM/Assets/Scripts/Base/Keyboard/Keyboard_Chin.cs

81 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using NPinyin;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace HK.Keyboard
{
public class Keyboard_Chin : MonoBehaviour
{
public bool isChin;
public bool isSelect;
public GameObject item;
public Action<string> selectedAction;
List<GameObject> items = new List<GameObject>();
private char[] array;
private void Awake()
{
}
void CheckIsShow()
{
var count = array.Length - items.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var o = GameObject.Instantiate(item, item.transform.parent);
var button = o.GetComponent<Button>();
button.onClick.AddListener(() => { OnClickItem(o.GetComponent<TMP_Text>().text); });
items.Add(o);
}
}
for (var i = 0; i < array.Length; i++)
{
var o = items[i];
o.SetActive(true);
var tmpText = o.GetComponent<TMP_Text>();
tmpText.text = array[i].ToString();
}
}
private void OnClickItem(string str)
{
isSelect = true;
selectedAction?.Invoke(str);
gameObject.SetActive(false);
isSelect = false;
}
private void OnDisable()
{
foreach (var o in items)
if (o.activeSelf)
o.SetActive(false);
}
[ContextMenu("test")]
public void PY(string str)
{
isSelect = false;
var strings = Pinyin.GetChineseText(str);
if (!string.IsNullOrEmpty(strings))
{
array = strings.ToCharArray();
CheckIsShow();
gameObject.SetActive(true);
}
else
{
gameObject.SetActive(false);
}
}
}
}