LoginSignup
19
19

More than 5 years have passed since last update.

Unityの新GUIでマウスオーバー判定サンプル

Last updated at Posted at 2015-04-04

Unity uGUIでマウスオーバー

スマートフォン上でも動作します。

サンプルコード

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[RequireComponent( typeof( Image ) )]
public class TapOverComponent : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public Image image { get { return GetComponent<Image>(); } }

    // オブジェクトの範囲内にマウスポインタが入った際に呼び出されます。
    // this method called by mouse-pointer enter the object.
    public void OnPointerEnter( PointerEventData eventData )
    {
        image.color = Color.red;
    }

    // オブジェクトの範囲内からマウスポインタが出た際に呼び出されます。
    // 
    public void OnPointerExit( PointerEventData eventData )
    {
        image.color = Color.white;
    }
}

主な要素

要素 概要
IPointerEnterHandler OnPointerEnterが定義されている
OnPointerEnter uGUIオブジェクトの範囲内にマウスポインタが入った際に呼び出される
IPointerExitHandler OnPointerExitが定義されている
OnPointerExit uGUIオブジェクトの範囲内からマウスポインタが出た際に呼び出される
19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19