オブジェクトにマウスカーソルを合わせると、画像が表示されるしくみを作りましょう。
今回の例では、Cubeにカーソルの乗せると、右上に画像が出るようにしています。

マウスカーソルを合わせると表示
UI > 画像を作成します。

Imageを右上に配置しました。

インポートした画像は、スプライト(2DとUI)に変更しましょう。

Imageのソース画像に、画像データを入れます。

Imageのチェックを外し、非表示にしておきます。

Cubeを作成します。

空のオブジェクトを作成します。
PhotoScript.csを作成し、GamgeObject(空のオブジェクト)に追加します。

PhotoScript.csを記述します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System.Collections; using System.Collections.Generic; using Microsoft.Unity.VisualStudio.Editor; using UnityEngine; public class PhotoScript : MonoBehaviour { [SerializeField] GameObject photoImage; void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if(hit.collider.name == "Cube") { photoImage.SetActive(true); } } } } |
PhotoImageのフィールドに、Imageを入れます。

ゲームプレイしてみましょう。
Cubeにカーソルを合わせると、画像が表示されます。
