//tips
//ダッシュボートボタンでHoseを操作してみる
作成したダッシュボートボタンでstandard assetsにあるHoseを操作してみる。
まず、Hoseにアタッチされているscriptを無効化する。
Start ButtonとStop Buttonのインスペクターからon clickのプラスアイコンを押し、water showerパーティクルをnone部分にドラッグする。
Functionを押して、particle systemのplay(),stop()をそれぞれ設定すると、イベント発生時にこれらの関数を呼び出すことができるようになる。
きちんとボタンを押すことでwatershowerが反応するかスクリプトから確認する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ButtonExecuteTest : MonoBehaviour
{
private GameObject startButton, stopButton;
private bool on = false;
private float timer = 5.0f;
void Start()
{
startButton = GameObject.Find("StartButton");
stopButton = GameObject.Find("StopButton");
}
void Update()
{
timer -= Time.deltaTime;
if(timer < 0.0f)
{
on = !on;
timer = 5.0f;
PointerEventData data = new PointerEventData(EventSystem.current);
if (on)
{
ExecuteEvents.Execute<IPointerClickHandler>(startButton, data, ExecuteEvents.pointerClickHandler);
}
else
{
ExecuteEvents.Execute<IPointerClickHandler>(stopButton, data, ExecuteEvents.pointerClickHandler);
}
}
}
}
ホースが閉じているか開いているかを確認し、5秒ごとにスイッチが切り替わるようにしている。
スクリプト内のみで使用されるものにprivateを設定、エディターや他のスクリプトから参照される場合はpublicで設定する。
startButtonとstopButtonはGameobject.Find()で取得している。
ExecuteEvents.Executeでボタンに対してonClick()イベントが発生したという情報を送る。
ExecuteEvents.Execute<IPointerClickHandler>(StartButton,data,ExecuteEvents.pointerClickhandler)を呼び出す。
型は
public static bool Execute (GameObject target, EventSystems.BaseEventData eventData, EventFunction<T> functor);
eventData:実行イベントに関連付けられたデータ
functor:ゲームオブジェクトのコンポーネント上で実行される関数
となっており、data部分はポインタ(マウス/タッチ)イベントに関連するイベントの情報データPointerEventDataを現在のEventSystemの状態で生成。
ExecuteEvents.pointerClickhandlerでボタンのonClickイベントを実行する。
https://docs.unity3d.com/ja/2018.4/ScriptReference/EventSystems.ExecuteEvents.Execute.html
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.BaseEventData.html
今度は、ユーザー視点でボタン操作をさせたいので、ユーザーの視線をレイにして、ボタンを見ている場合にハイライトするように設定してみる。
レイをボタンに感知させる必要があるのでボタンの下に新たにボタンタグをつけたsphereを作成し、レイとの接触を感知させる空間を作成する。球体のデザインを表示するmesh renderは不要なのでチェックを外しておく。
新たにスクリプトを記載して、カメラからレイを飛ばして、Buttonとタグづけされたオブジェクトと接触するか確認し、接触していたらhitButtonとして、認知する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ButtonExecute : MonoBehaviour
{
private GameObject currentButton;
void Update()
{
//Transform camera = Camera.main.transform;
//Ray ray = new Ray(camera.position, camera.rotation * Vector3.forward);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
GameObject hitButton = null;
PointerEventData data = new PointerEventData(EventSystem.current);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.tag == "Button")
{
hitButton = hit.transform.parent.gameObject;
}
}
if(currentButton != hitButton) {
if(currentButton != null)
{
ExecuteEvents.Execute<IPointerExitHandler>(currentButton, data, ExecuteEvents.pointerExitHandler);
}
currentButton = hitButton;
if(currentButton != null)
{
ExecuteEvents.Execute<IPointerEnterHandler>(currentButton, data, ExecuteEvents.pointerEnterHandler);
}
}
}
}