Rohit Vishwakarma
Rohit Vishwakarma

Reputation: 530

How to make inverted mask and allow clicks to work on masked area and not work on the remainiing Unity?

I'm working on making interactive tutorials for my game, where for example a hand image will guide the user where to click next and at the same time will also show some info about the game rules.

enter image description here

For example, in the attached image I created an inverted mask following this tutorial by code monkey https://www.youtube.com/watch?v=XJJl19N2KFM&t=80s

over button 1, I want the clicks to work on the circled area only. How can I achieve this?

Buttons are on a different canvas and all masking stuff are on another canvas.

Upvotes: 0

Views: 3850

Answers (2)

Nk Pstl
Nk Pstl

Reputation: 1

For those who might wonder why suggestions from the Code Monkey video do not work in Unity 2021.3 - the code requires a few minor tweaks:

  1. SetInt has since been deprecated
  2. _StencilComp property - made into a float.
  3. Use SetFloat and cast as float instead

Final result:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Rendering;

public class UIMaskFilter : Image {
    public override Material materialForRendering {
        get {
            Material material = new Material(base.materialForRendering);
            material.SetFloat("_StencilComp", (float)CompareFunction.NotEqual);
            return material;
        }
    }
}

Upvotes: 0

IShix
IShix

Reputation: 196

I recommend using UnmaskForUGUI. It is a free plugin and can be achieved by using UnmaskRaycastFilter. I actually use it in my projects for the same purpose as yours.

enter image description here

Upvotes: 1

Related Questions