開発メモ[1-2](音声入力の起動を設定するために必要になるコード)

目的

「コントローラーのTriggerを押したとき」という条件で音声認識システムを起動する。

使用システム

UnityEngine(SendMessage,EventSystems)

コード参考元

www.sejuku.net

 

ekulabo.com

参考コード(方向キーによるオブジェクトの移動)

-----------------------------------------------------------------------------------------

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MyObject : MonoBehaviour {
  5.  
  6.         void Update () {
  7.  
  8.                // 左に移動
  9.                if (Input.GetKey (KeyCode.LeftArrow)) {
  10.                    this.transform.Translate (-0.1f,0.0f,0.0f);
  11.                }
  12.                // 右に移動
  13.                 if (Input.GetKey (KeyCode.RightArrow)) {
  14.                     this.transform.Translate (0.1f,0.0f,0.0f);
  15.                 }
  16.                 // 前に移動
  17.                 if (Input.GetKey (KeyCode.UpArrow)) {
  18.                     this.transform.Translate (0.0f,0.0f,0.1f);
  19.                 }
  20.                 // 後ろに移動
  21.                 if (Input.GetKey (KeyCode.DownArrow)) {
  22.                     this.transform.Translate (0.0f,0.0f,-0.1f);
  23.                 }
  24.         }
  25. }

-----------------------------------------------------------------------------------------

参考コード(音声コード呼び出しSendMessage)

-----------------------------------------------------------------------------------------

呼び出し元

-----------------------------------------------------------------------------------------

  1. ///〈Summary〉
  2. //SendMessageで処理が呼び出されるメソッドです。
  3. ///〈/Summary〉
  4. public class SendMassageCaller : MonoBehaviour
  5. {
  6.         public GameObject targetObj;
  7.  
  8.         void Start()
  9.         {
  10.                targetObj.SendMessage("ShowLog");
  11.         }
  12. }

-----------------------------------------------------------------------------------------

呼び出し先

-----------------------------------------------------------------------------------------

  1. ///〈Summary〉
  2. //SendMessageで処理が呼び出されるメソッドです。
  3. ///〈/Summary〉
  4. public class SendMassageTarget : MonoBehaviour
  5. {
  6.        ///〈Summary〉
  7.        //SendMessageで処理が呼び出されるメソッドです。
  8.        ///〈/Summary〉
  9.        public void ShowLog()
  10.        {
  11.                // ログを表示します。
  12.                Debug.Log("SendMessageが通知された!");
  13.        }
  14. }

-----------------------------------------------------------------------------------------

参考コード(音声コード呼び出しEventSystem)

-----------------------------------------------------------------------------------------

呼び出し元

-----------------------------------------------------------------------------------------

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5.  
  6. ///〈Summary〉
  7. //EventSystemsを使用して他のスクリプトのメソッドを呼び出します。
  8. ///〈/Summary〉
  9. public class EventSystemCaller : MonoBehaviour
  10. {
  11.        public GameObject targetObj;
  12.  
  13.        void Start();
  14.        {
  15.               DoMyEvent();
  16.         }
  17.  
  18.         ///〈Summary〉
  19.         //EventSystemsを使用して他のスクリプトのメソッドを呼び出します。
  20.         ///〈/Summary〉
  21.         void DoMyEvent()
  22.         {
  23.                NotifyEvent(targetObj);
  24.          }
  25.  
  26.          ///〈Summary〉
  27.          //対象のオブジェクトにイベントを通知します。
  28.          ///〈/Summary〉
  29.          ///〈param name="targetObj"〉対象のオブジェクト〈/param〉
  30.          void callMyEvent(IEventCaller inf, BaseEventData eventData)
  31.          {
  32.                 inf.Eventcall();
  33.           }
  34. }

-----------------------------------------------------------------------------------------

呼び出し先

-----------------------------------------------------------------------------------------

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Eventsystems;
  5.  
  6. ///〈Summary〉
  7. //EventSystemを使用してメソッドを呼び出されるスクリプトです。
  8. ///〈/Summary〉
  9. public interface IEventCaller : IEventSystemHandler
  10. {
  11.        //イベントを呼び出すメソッドです。
  12.        void Eventcall();
  13. }

-----------------------------------------------------------------------------------------