Alpha Raycaster

by [email protected]

Detail this asset from Unity Store: Original Link

This is a paid asset, but now you can download Alpha Raycaster Free.

Alpha Raycaster v1.4

Download Now

Alpha Raycaster is a custom raycaster for UI texture-based elements built upon default Graphic Raycaster. It checks texture transparency, before deciding whether to send input events for targeted object, which allows a precise interaction with objects that have a non-rect shapes (like circles, triangles, map regions, text, etc).

WEBGL DEMO • DOCUMENTATION • SUPPORT FORUM

Features:
• Make buttons and any other interactable UI objects of any shape
• Compatible with both mouse and touch input
• Handles any sprite transformations
• Supports all the sprite modes (including filled, tiled, sliced) and sprite atlases
• Works with orthographic and perspective camera modes
• Demo scene with examples included

Be aware, that the plugin only works with Image, RawImage and Text UnityEngine.UI (uGUI) components.

Introduction
The default raycaster in Unity 4.6 GUI (Graphic Raycaster) uses just the canvas bounds to check for ray hits. That’s fine while the targets element texture is of a rectangular shape and mostly opaque. However, as soon as you want to make, say, a circle button, you will have a trouble: it will react to user input (like pointer hovering and clicks) even in transparent areas, which is obviously wrong.

Using Alpha Raycaster, you will be able to solve such problems with ease: custom raycaster component will check if alpha value of the target texture in a specific place is below certain threshold and will act accordingly, resulting in fine-tuned input handling.

Getting started
Import the package from the asset store. The “AlphaRaycaster” directory will appear in the Assets folder of your project. You are free to move that folder to any place in the Assets hierarchy.

After that, there are two ways to setup the plugin:

Selective Mode: in case you want to alpha test specific UI objects, add an Alpha Check component to each;
Raycaster Mode: in case you want to alpha test all the UI objects inside a canvas (root of UI hierarchy), add an Alpha Raycaster component to that canvas. Be aware though, that this setup has more performance cost.
To perform alpha testing, target texture has to be readable. Alpha Check component will check for this and offer to automatically configure the texture, if needed.

Configuring the plugin
Alpha Raycaster component has all the properties of the default Graphic Raycaster, plus some specific for alpha testing.

For the default properties, you may consult the Unity documentation for the Graphic Raycaster – they work just the same.

The custom properties are:

Alpha Threshold — set this in 0.0 to 1.0 range to control the level of transparency at which the raycaster will ignore the target element. For example, if you have a fully opaque circle texture and the area outside of the circle is fully transparent — you may set the 1.0 for this property. However, if you have some effect around the circle (like glow) which is semi-transparent and you want the object to react to input when user is hovering areas with that effect — you have to set Alpha Threshold a bit lower, to the value of the transparency the effect have;
Include Material Alpha — enable, if you want the objects tint color to affect the alpha threshold. May be useful, if you change the objects transparency in the editor and want these changes be reflected in the alpha testing process;
Selective Mode — in this mode all the above settings will be ignored and the alpha test will happen only on the objects, which have the “Alpha Check” component. The Alpha Threshold and Include Material Alpha properties should be set for each such object;
Show Texture Warnings — show warning in the Unity console when you try to alpha test an object, which texture was not set to be readable.

Using buttons with a child text objects
In case you want to alpha-check a button, which contains a text object, please make sure that the canvas of the text object is not blocking transparent areas of the buttons texture. Otherwise, it will consume user input in those areas.

You may also consider adding a CanvasGroup component to the text object and disabling the “Blocks Raycast” property, so that it won’t consume user input, allowing the plugin to properly handle interaction with the button.

Preparing text font to use with Alpha Raycaster
In order to use Alpha Raycaster with text, you need to make font texture readable.

Find the font you want to use and set any character mode except ‘Dynamic’ (make sure the resulting texture will contain all the character you need). Then press the cog button and select ‘Create Editable Copy’.

Related Posts

Leave a Comment