Highlighting an object is a very effective way of letting players know they can interact with it. This is very useful in a number of game genres such as puzzles and point-and-click adventures, and it can also be applied to create 3D user interfaces.
For this recipe, you'll need a 3D model and a 2D texture map. If you don't have them, please import the highlight
package, available in the 0423_03_08
folder, into your project.
To highlight a material at mouse over, follow these steps:
highlightScene
scene.highlightCube
).baseBox
texture to the Base texture of the material.HighlightObject
and open it in your editor.using UnityEngine; using System.Collections; public class HighlightObject : MonoBehaviour { public Color initialColor; public Color highlightColor; public Color mousedownColor; private bool mouseon = false; void OnMouseEnter() { mouseon = true; renderer.material.SetColor("_Emission", highlightColor); } void OnMouseExit() { mouseon = false; renderer.material.SetColor("_Emission", initialColor); } void OnMouseDown() { renderer.material.SetColor("_Emission", mousedownColor); } void OnMouseUp() { if (mouseon) renderer.material.SetColor("_Emission", highlightColor); else renderer.material.SetColor("_Emission", initialColor); } }
highlightCube
game object (in the Hierarchy view).The box collider detects the mouse pointer over the object, working as a trigger for the emissive color value change. The mouseon
Boolean variable is used to detect if the mouse button is released within or out of the box collider, changing its color accordingly.
18.188.37.136