SendMessage没有启动功能(HoloLens / Unity / C#)

时间:2017-06-01 12:11:02

标签: c# unity3d hololens hololens-emulator

目标:使用审核控件从一个场景更改为另一个场景。

问题:在HoloLens仿真器中启动应用程序时,会打开第一个场景。当说"下一步"时,HoloLens确实识别了句子,但sendMessage没有打开OnNextStep()功能。

感谢您的帮助! :)

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Diagnostics;
using UnityEngine.SceneManagement;

public class KeywordManager : MonoBehaviour {

    KeywordRecognizer keywordRecognizer = null;
    Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();

    // Use this for initialization
    void Start () {
        keywords.Add("Next Step", () =>
        {
            SendMessage("OnNextStep", SendMessageOptions.DontRequireReceiver);
        });

        // Tell the KeywordRecognizer about our keywords.
        keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());

        // Register a callback for the KeywordRecognizer and start recognizing!
        keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
        keywordRecognizer.Start();
    }

    private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        System.Action keywordAction;
        if(keywords.TryGetValue(args.text, out keywordAction))
        {
            keywordAction.Invoke();
        }
    }

    void OnNextstep()
    {
        UnityEngine.Debug.Log(this);
        SceneManager.LoadScene("FirstStepScene");
    }

    // Update is called once per frame
    void Update () {

    }
}

1 个答案:

答案 0 :(得分:4)

Unity的SendMessage函数在调用函数时区分大小写。

您的函数名称为OnNextstep,但您正在调用OnNextStep

SendMessage("OnNextStep", SendMessageOptions.DontRequireReceiver);

注意大写和非大写“S”。修复此问题,假设存在其他隐藏问题,则应解决您的问题。

注意

避免在Unity中使用SendMessage。如果要从另一个脚本调用函数,请使用GameObject.Find查找 GameObject 然后GetComponent以获取该脚本然后调用其函数。您也可以使用events and delegates执行此操作。