简体   繁体   English

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

[英]SendMessage doesn't launch function (HoloLens/Unity/C#)

Goal: Changing from one scene to another using auditive controls. 目标:使用听觉控制从一个场景切换到另一个场景。

Problem: When launching the application in the HoloLens Emulator, the first scene opens. 问题:在HoloLens仿真器中启动应用程序时,将打开第一个场景。 When saying "Next Step", the HoloLens does recognize the sentence, but the sendMessage doesn't open the OnNextStep() function. 当说“下一步”时,HoloLens会识别该句子,但sendMessage不会打开OnNextStep()函数。

Thanks for trying to help! 感谢您的帮助! :) :)

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 () {

    }
}

Unity's SendMessage function is case sensitive when it comes to calling functions. Unity的SendMessage函数在调用函数时区分大小写。

Your function name is OnNextstep but you are calling OnNextStep : 您的函数名称是OnNextstep但您正在调用OnNextStep

SendMessage("OnNextStep", SendMessageOptions.DontRequireReceiver);

Notice the capitalized and non capitalized "S" . 注意大写和非大写的“S” Fix that and your problem should be fixed assuming there is other hidden problems. 解决这个问题,假设存在其他隐藏问题,应该修复问题。

Note : 注意

Avoid using SendMessage in Unity. 避免在Unity中使用SendMessage If you want to call a function from another script, use GameObject.Find to find the GameObject then GetComponent to get that script then call its function. 如果你想从另一个调用脚本功能,使用GameObject.Find找到游戏对象 ,然后GetComponent得到那么脚本中调用其功能。 You can also use events and delegates to do this. 您还可以使用事件和委托来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM