Highlighting materials at mouse over

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.

Getting ready

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.

How to do it...

To highlight a material at mouse over, follow these steps:

  1. Import the Unity package and open the highlightScene scene.
  2. In the Hierarchy view, select the 3D object to be highlighted (in our case, the object named highlightCube).
  3. The Inspector view should display the game object's material. Using the drop-down menu, change its Shader option from Diffuse to VertexLit.
  4. Apply the baseBox texture to the Base texture of the material.
  5. Note that the VertexLit shader has a property called Emissive Color, which should be black as default. If you want a preview of what is going to happen later in the recipe, change that to green (but be sure to change it back to black).
  6. We need to create a script. In the Project view, click on the Create drop-down menu and choose C# Script. Rename it HighlightObject and open it in your editor.
  7. Replace everything with the following code:
    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);
        }
    }
  8. Save your script and apply it to the highlightCube game object (in the Hierarchy view).
  9. Select the highlightCube game object and, in the Inspector view, set Highlight Color to dark green (R: 0, G: 100, B: 0) and Mousedown Color to light green (R:0, G: 255, B: 0):
    How to do it...
  10. Add a box collider to the highlightCube game object by navigating to Component | Physics | Box Collider.
  11. Test the scene. The box should be highlighted when the mouse is over it (and even more when clicked).

How it works...

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.

There's more...

You can achieve other interesting results using other shaders, but be sure to address the changes to their particular material properties.

Highlighting with self-illuminated shaders

Self-illuminated shaders will work if you replace _Emission with _Color in the script.

Using transparent shaders

Transparent shaders can be an interesting option. Remember you can change the transparency of the material by changing the Alpha value of its main color (which should be addressed as _Color in the script).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.232.11