ユーザ用ツール

サイト用ツール

wiki:unity:tips:097

以前のリビジョンの文書です


GameObjectが重なっている時uGUIで配置したボタンだけタッチに反応させる

uGUIのボタンにSpriteなど他のGameObject重なっている時、両方のタッチ処理に反応してしまうのは都合が悪いので、 uGUIのUIオブジェクトが何かしら重なっている時は、その他のタッチ処理を無効にして通らない様にしてみます。 この対応にはEventSystem.current.IsPointerOverGameObject()を使えば良いです。

using UnityEngine.EventSystems;
 
//-------------------------------------------------------------
// EventSystemのGameObjectにマウスオーバーしているか?
//-------------------------------------------------------------
public bool IsPointerOverGameObject()
{
    EventSystem current = EventSystem.current;
    if (current != null)
    {
        if (current.IsPointerOverGameObject())
        {
            return true;
        }
 
        foreach (Touch t in Input.touches)
        {
            if (current.IsPointerOverGameObject(t.fingerId))
            {
                return true;
            }
        }
    }
    return false;
}

使い方はシンプルに

//-------------------------------------------------------------
//! タッチしたか.
//-------------------------------------------------------------
public bool IsTouchPress()
{
    if (Input.GetMouseButtonDown(0))
    {
        if (!IsPointerOverGameObject())
        {
        	return true;
        }
    }
    return false;
}

こんな感じでタッチ処理をラップしてIsTouchPressを使う様にしてあげれば良いです。

Permalink wiki/unity/tips/097.1425507332.txt.gz · 最終更新: 2015/03/04 22:15 by step

oeffentlich