Unityでは、Physics.Raycast
を使ってマウスがオブジェクト上にあるかどうか調べることができます。
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
// 処理したい内容
}
ただし、上のスクリプトはUIでは使えません。
マウスがUIのオブジェクト上にあるかどうか調べたい場合はUnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject
を使うと良いそうです。
using UnityEngine.EventSystems;
if (EventSystem.current.IsPointerOverGameObject ()) {
// 処理したい内容
}
リンク
Unity – スクリプトリファレンス: EventSystems.EventSystem.IsPointerOverGameObject
https://docs.unity3d.com/jp/current/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html
関連記事
UIとGameObjectが重なっている場合の判定方法
Raycastを使ってGameObjectの選択をしているプログラムで、UIとGameObjectが重なっている場合に使える方法。
using UnityEngine.EventSystems
EventSystem.current.IsPointerOverGameObject()
リンク
How to make UI block raycats (mobile) | Un...
UnityのUIでDrag and Drop
IBeginDragHandler, IDragHandler, IDropHandlerを継承するとOnBeginDrag, OnDrag, OnDropが使えようになるそうです。
public class Example : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler
{
public void OnBe...
オブジェクト位置に合わせてCanvas内に表示をする方法
Unityでオブジェクトの位置に応じてCanvas内に表示をしてみようと思って方法を探してみました。
Canvasに「Screen Space - Overlay」を設定している場合
RectTransformUtility.WorldToScreenPointを使うと簡単にできるようです。
具体的には、
Vector3 position;
GetComponent (...
TextMesh ProにSpriteを表示する方法
TextMesh ProはUI上にテキストを表示するAssetですが、タグを使って画像を表示することもできます。
手順としては次のような感じです。
1. 表示したい画像ファイルを読み込み、Sprite ModeをMultipleに設定します。
2. Spriteを右クリックしてCreate » TextMeshPro » Sprite Assetを実行します。
...