C# 如何使文本显示所有8位数字,然后清除文本? 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类安全键盘系统:MonoBehavior { [标题(“参考”)] //而是让这个类控制显示文本 [SerializeField]私有TextMesh\u文本; [标题(“设置”)] //也可以让这个类控制代码的长度 [SerializeField]私有int_codeLength=8; [标题(“调试”)] [SerializeField]私有游戏对象[]\u键盘号; [SerializeField]私有列表_code=new List(); //一旦代码长度达到目标长度,就会调用该函数 公共事件行动的反完成; //在第一帧更新之前调用Start 私有void Start() { var KeyPadNumbersParent=GameObject.FindGameObjectWithTag(“KeypadParent”).GetComponentsInChildren(true); foreach(在KeyPadNumbersParent中转换子级) { if(child.gameObject.GetComponent()!=null) { var securityKeypadKeys=child.gameObject.GetComponent(); securityKeypadKeys.onKeyPressed-=HandleKeyPressed; securityKeypadKeys.onKeyPressed+=HandleKeyPressed; } } } 私有void OnDestroy() { //为了完整起见,您应该在不再需要回调时立即删除它们 //为了避免任何例外情况 foreach(var keyPadNumber in_keyPadNumber) { var securityKeypadKeys=keyPadNumber.GetComponent(); securityKeypadKeys.onKeyPressed-=HandleKeyPressed; } } //当按下小键盘键时,会调用此命令 private void HandleKeyPressed(int值) { //将值添加到列表中 _代码。添加(值); _text.text+=value.ToString(); //检查代码是否已达到目标长度 //如果不是什么都不做 如果(_code.Count

C# 如何使文本显示所有8位数字,然后清除文本? 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类安全键盘系统:MonoBehavior { [标题(“参考”)] //而是让这个类控制显示文本 [SerializeField]私有TextMesh\u文本; [标题(“设置”)] //也可以让这个类控制代码的长度 [SerializeField]私有int_codeLength=8; [标题(“调试”)] [SerializeField]私有游戏对象[]\u键盘号; [SerializeField]私有列表_code=new List(); //一旦代码长度达到目标长度,就会调用该函数 公共事件行动的反完成; //在第一帧更新之前调用Start 私有void Start() { var KeyPadNumbersParent=GameObject.FindGameObjectWithTag(“KeypadParent”).GetComponentsInChildren(true); foreach(在KeyPadNumbersParent中转换子级) { if(child.gameObject.GetComponent()!=null) { var securityKeypadKeys=child.gameObject.GetComponent(); securityKeypadKeys.onKeyPressed-=HandleKeyPressed; securityKeypadKeys.onKeyPressed+=HandleKeyPressed; } } } 私有void OnDestroy() { //为了完整起见,您应该在不再需要回调时立即删除它们 //为了避免任何例外情况 foreach(var keyPadNumber in_keyPadNumber) { var securityKeypadKeys=keyPadNumber.GetComponent(); securityKeypadKeys.onKeyPressed-=HandleKeyPressed; } } //当按下小键盘键时,会调用此命令 private void HandleKeyPressed(int值) { //将值添加到列表中 _代码。添加(值); _text.text+=value.ToString(); //检查代码是否已达到目标长度 //如果不是什么都不做 如果(_code.Count,c#,unity3d,C#,Unity3d,if (_code.Count <= _codeLength) return; 不返回。当您检查\u code.Count时,如果Count==\u code长度 if (_code.Count <= _codeLength) return; private void HandleKeyPressed(int value) { // add the value to the list _code.Add(value); _text.text += val

if (_code.Count <= _codeLength) return;

不返回。当您检查
\u code.Count时,如果Count==
\u code长度
if (_code.Count <= _codeLength) return;
private void HandleKeyPressed(int value)
{
    // add the value to the list
    _code.Add(value);

    _text.text += value.ToString();

    // Check if the code has reached the target length
    // if yes prcoess further
    if (_code.Count == _codeLength)
    {
      // if it reached the length combine all numbers into one int
      var exponent = _code.Count;
      float finalCode = 0;
      foreach (var digit in _code)
      {
          finalCode =digit * Mathf.Pow(10, exponent);
          exponent--;
      }

      // invoke the callback event
      OnCodeComplete?.Invoke((int)finalCode);

      // and reset the code
      ResetCode();
    }
}