開発メモ[1-2](音声入力の起動を設定するために必要になるコード)
目的
「コントローラーのTriggerを押したとき」という条件で音声認識システムを起動する。
使用システム
UnityEngine(SendMessage,EventSystems)
コード参考元
参考コード(方向キーによるオブジェクトの移動)
-----------------------------------------------------------------------------------------
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MyObject : MonoBehaviour {
- void Update () {
- // 左に移動
- if (Input.GetKey (KeyCode.LeftArrow)) {
- this.transform.Translate (-0.1f,0.0f,0.0f);
- }
- // 右に移動
- if (Input.GetKey (KeyCode.RightArrow)) {
- this.transform.Translate (0.1f,0.0f,0.0f);
- }
- // 前に移動
- if (Input.GetKey (KeyCode.UpArrow)) {
- this.transform.Translate (0.0f,0.0f,0.1f);
- }
- // 後ろに移動
- if (Input.GetKey (KeyCode.DownArrow)) {
- this.transform.Translate (0.0f,0.0f,-0.1f);
- }
- }
- }
-----------------------------------------------------------------------------------------
参考コード(音声コード呼び出しSendMessage)
-----------------------------------------------------------------------------------------
呼び出し元
-----------------------------------------------------------------------------------------
- ///〈Summary〉
- //SendMessageで処理が呼び出されるメソッドです。
- ///〈/Summary〉
- public class SendMassageCaller : MonoBehaviour
- {
- public GameObject targetObj;
- void Start()
- {
- targetObj.SendMessage("ShowLog");
- }
- }
-----------------------------------------------------------------------------------------
呼び出し先
-----------------------------------------------------------------------------------------
- ///〈Summary〉
- //SendMessageで処理が呼び出されるメソッドです。
- ///〈/Summary〉
- public class SendMassageTarget : MonoBehaviour
- {
- ///〈Summary〉
- //SendMessageで処理が呼び出されるメソッドです。
- ///〈/Summary〉
- public void ShowLog()
- {
- // ログを表示します。
- Debug.Log("SendMessageが通知された!");
- }
- }
-----------------------------------------------------------------------------------------
参考コード(音声コード呼び出しEventSystem)
-----------------------------------------------------------------------------------------
呼び出し元
-----------------------------------------------------------------------------------------
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- ///〈Summary〉
- //EventSystemsを使用して他のスクリプトのメソッドを呼び出します。
- ///〈/Summary〉
- public class EventSystemCaller : MonoBehaviour
- {
- public GameObject targetObj;
- void Start();
- {
- DoMyEvent();
- }
- ///〈Summary〉
- //EventSystemsを使用して他のスクリプトのメソッドを呼び出します。
- ///〈/Summary〉
- void DoMyEvent()
- {
- NotifyEvent(targetObj);
- }
- ///〈Summary〉
- //対象のオブジェクトにイベントを通知します。
- ///〈/Summary〉
- ///〈param name="targetObj"〉対象のオブジェクト〈/param〉
- void callMyEvent(IEventCaller inf, BaseEventData eventData)
- {
- inf.Eventcall();
- }
- }
-----------------------------------------------------------------------------------------
呼び出し先
-----------------------------------------------------------------------------------------
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Eventsystems;
- ///〈Summary〉
- //EventSystemを使用してメソッドを呼び出されるスクリプトです。
- ///〈/Summary〉
- public interface IEventCaller : IEventSystemHandler
- {
- //イベントを呼び出すメソッドです。
- void Eventcall();
- }
-----------------------------------------------------------------------------------------