Changing the color of an object at runtime can be a very effective way of letting players know that 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 used to create 3D user interfaces.
For this recipe, we'll use objects created directly in Unity. Alternatively, you can use any 3D model you like.
To highlight a material at mouse-over, follow these steps:
HighlightMaterial
.135
, G: 135
, B: 135
), its Emission intensity to 1
, as shown in the screenshot below, and it's Emission color to R: 1
, G: 1
, B: 1
:HighlightObject
and open it in your editor.using UnityEngine; using System.Collections; public class HighlightObject : MonoBehaviour{ private Color initialColor; public bool noEmissionAtStart = true; public Color highlightColor = Color.red; public Color mousedownColor = Color.green; private bool mouseon = false; private Renderer myRenderer; void Start() { myRenderer = GetComponent<Renderer>(); if (noEmissionAtStart) initialColor = Color.black; else initialColor = myRenderer.material.GetColor("_EmissionColor"); } void OnMouseEnter(){ mouseon = true; myRenderer.material.SetColor("_EmissionColor", highlightColor); } void OnMouseExit(){ mouseon = false; myRenderer.material.SetColor("_EmissionColor",initialColor); } void OnMouseDown(){ myRenderer.material.SetColor("_EmissionColor", mousedownColor); } void OnMouseUp(){ if (mouseon) myRenderer.material.SetColor("_EmissionColor", highlightColor); else myRenderer.material.SetColor("_EmissionColor", initialColor); } }
The cube is automatically sent the mouse enter/exit/down/up events as the user moves the mouse pointer over and away from the part of the screen where the cube is visible. Our script adds a behavior to the cube when these events are detected. The Start()
method gets a reference to the Renderer
component of the GameObject that the script has been added to, and stores it in the variable myRenderer
(note that 'renderer' already has a meaning in Unity so it is not appropriate as a private variable name for this script). The Boolean variable called mouseon
records whether or not the mouse pointer is currently over the object. When the mouse button is released, we use the mouseon
variable to decide whether to change the cube back to its initial color (mouseon
FALSE, so the mouse pointer is away from the cube), or back to its highlight color (mouseon
TRUE, so the mouse pointer is over the cube).
The reason we needed to change the material's original Emission color to ultra, dark gray is that leaving it black would have caused Unity to optimize the Shader by removing the Emission property from the material. Our script wouldn't have worked if this had happened.
You can achieve other interesting results by changing the other properties of your material (by changing the _EmissionColor
script to _Color
or "_SpecularColor
, for instance). For a full list of properties, select your material, and, in the Inspector view, click on the Edit button, at the side of the Shader drop-down menu.
3.135.190.182