Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果启动游戏时父对象首先未处于活动状态,如何注册到事件?_C#_Unity3d - Fatal编程技术网

C# 如果启动游戏时父对象首先未处于活动状态,如何注册到事件?

C# 如果启动游戏时父对象首先未处于活动状态,如何注册到事件?,c#,unity3d,C#,Unity3d,剧本: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class SecurityKeypadSystem : MonoBehaviour { [Header("References")] // rather let this class control the display text [Seri

剧本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SecurityKeypadSystem : MonoBehaviour
{
    [Header("References")]
    // rather let this class control the display text
    [SerializeField] private TextMesh _text;

    [Header("Settings")]
    // also rather let this class control the length of a code
    [SerializeField] private int _codeLength = 8;

    [Header("Debugging")]
    [SerializeField] private GameObject[] _keyPadNumbers;
    [SerializeField] private List<int> _code = new List<int>();

    // This will be invoked once the code length has reached the target length
    public event Action<int> OnCodeComplete;

    // Start is called before the first frame update
    private void Start()
    {
        _keyPadNumbers = GameObject.FindGameObjectsWithTag("Keypad");

        // register a callback to each key that handles the numbers
        foreach (var keyPadNumber in _keyPadNumbers)
        {
            // It is save to remove an event even if it hasn't been added yet
            // this makes sure it is only added exactly once
            // only adding this here for the case you later have to move this again to Update for some reason ;)
            var securityKeypadKeys = keyPadNumber.GetComponent<SecurityKeypadKeys>();
            securityKeypadKeys.onKeyPressed -= HandleKeyPressed;
            securityKeypadKeys.onKeyPressed += HandleKeyPressed;
            securityKeypadKeys.onKeyPressed += SecurityKeypadKeys_onKeyPressed;
        }
    }

    private void SecurityKeypadKeys_onKeyPressed(int value)
    {
        string gethere = "";
    }

    private void OnDestroy()
    {
        // just for completeness you should always remove callbacks as soon as they are not needed anymore
        // in order to avoid any exceptions
        foreach (var keyPadNumber in _keyPadNumbers)
        {
            var securityKeypadKeys = keyPadNumber.GetComponent<SecurityKeypadKeys>();
            securityKeypadKeys.onKeyPressed -= HandleKeyPressed;
        }
    }

    // this is called when a keypad key was pressed
    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 not do nothing
        if (_code.Count < _codeLength) return;

        // 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();
    }

    // Maybe you later want an option to clear the code field from the outside as well
    public void ResetCode()
    {
        _code.Clear();
        _text.text = "";
    }

    // also clear the input if this gets disabled
    private void OnDisable()
    {
        ResetCode();
    }
}
但这返回0,因为当我运行游戏时,带有“小键盘”标签的游戏对象尚未激活,但它们在游戏后期会变得活跃

以下是附加了此脚本的游戏对象的屏幕截图,以及未激活的关键立方体游戏对象:


这是否回答了您的问题?由于您只是在
Start
中的
SecurityKeypadKeys
对象上连接事件处理程序,因此我会让每个
SecurityKeypadKeys
对象在
Start
oneable
中触发事件,然后让
SecurityKeypadSystem
通过连接键上的事件处理程序来响应这些事件。
_keyPadNumbers = GameObject.FindGameObjectsWithTag("Keypad");