code VR

Unity×VR(7)

スポンサーリンク

//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);
}
}

}
}

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, VR
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.