Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

【Unity】ボタン非活性時のマスク対応

Posted at

以前Unityに慣れていない方に対応方法を聞かれたことがあったのと、自分用のメモとして記載しておきます。

Buttonの非活性

uGUIのButtonはInteractableでボタンの活性、非活性を変更できます。
その際Transitionの設定で非活性状態のときのColorをDisabled Colorで設定できます。
スクリーンショット 2025-07-28 153628.png

ただ本体画像の色味や透明度に手を加えたくない場合もあると思います。
またボタンが有効ではないが、押したときに有効ではない旨のダイアログを出したい場合も多いのでそういったときはInteractableを有効なままにする必要があります。

マスク対応

Imageオブジェクトを作成し、ボタンと同じ画像をアタッチしてのカラーを黒にして透明度を少し下げればいい感じのマスクになります。
スクリーンショット 2025-07-28 223814.png

Imageの表示優先度をボタンより高くし、同じ大きさ・位置にすれば上にのっかる形になります。
あとはImageを参照してSetActiveで切り替えるだけです。
RaycastTargetをOnにすれば当たり判定がついてボタンをタップすることができなくなりますし、Offにすればボタンのタップは生きた状態になるので、内部で状態に応じて処理を切り替えれば活性・非活性それぞれの処理を行えます。
スクリーンショット 2025-07-28 222505.png

スクリーンショット 2025-07-28 222556.png

株式会社ONE WEDGE

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?