はじめに
Canvasの上にButtonとTextを配置し、ボタンを押下すると、Textの文字を変更するサンプルを作ります。
準備
手順
1. Canvas上にボタンを配置する
今回は、PosX, PosY, PosZ = (0,20,0)に配置しました。
また、ボタン名は「Push Me!」としました。

2. Canvas上にテキストを配置する
今回は、PosX, PosY, PosZ = (0,-100,0)に配置しました。

3. イベント用のオブジェクトを作成する
Hierarchy上で「Create Empty」し、オブジェクト名を「GameController」とします。
4. GameControllerにイベント用のコンポーネントを追加する
Assets > OVRInputSelection > Scripts > UIInteractionを追加します。

5. Textとソースコードを関連付けする
GameControllerのOut Textに2.で配置したTextを登録します。

ここで、UIInteractionクラスのoutTextとHierarchy上のTextが関連づけられるようです。
UIInteraction.cs
using UnityEngine;
using UnityEngine.SceneManagement;
public class UIInteraction : MonoBehaviour {
public UnityEngine.UI.Text outText;
public void OnButtonClicked() {
if (outText != null) {
outText.text = "<b>Last Interaction:</b>\nUI Button clicked";
}
}
public void OnSliderChanged(float value) {
if (outText != null) {
outText.text = "<b>Last Interaction:</b>\nUI Slider value: " + value;
}
}
public void OnToggleChanged(bool value) {
if (outText != null) {
outText.text = "<b>Last Interaction:</b>\nUI toggle value: " + value;
}
}
public void OnClearText() {
if (outText != null) {
outText.text = "";
}
}
public void OnBackToMenu() {
SceneManager.LoadScene("main", LoadSceneMode.Single);
}
}
6. ButtonにOnClickイベントを追加する
項目名 | 値 |
---|---|
Object | GameController |
Function | UIInteraction > OnButtonClicked |
そのままだと味気ないため、テキストを下記のように変更しました。
UIInteraction.cs
public void OnButtonClicked() {
if (outText != null) {
outText.text = "ボタンを押しました。";
}
7. 実行
ボタンを押下すると、テキストの文字が変わりました。