Highlighting materials at mouse over

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.

Getting ready

For this recipe, we'll use objects created directly in Unity. Alternatively, you can use any 3D model you like.

How to do it...

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

  1. Create a new 3D project, and add a Cube to the scene (from the Hierarchy view, navigate to Create | 3D Object | Cube).
  2. From the Project view, click the Create drop-down menu and choose Material. Name it HighlightMaterial.
  3. Select HighlightMaterial, and, from the Inspector view, change its Albedo color to gray (R: 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:
    How to do it...
  4. Assign HighlightMaterial to the Cube that you previously created.
  5. From the Project view, click on the Create drop-down menu and choose C# Script. Rename it HighlightObject and open it in your editor.
  6. Replace everything with the following code:
    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);
      }
    }
  7. Save your script and attach it to the Cube.
  8. Select the Cube, and, in the Inspector view, set the Highlight Color and Mousedown Color values to any colors that you would like:
    How to do it...
  9. If you are using the script with your own imported 3D mesh, please make sure you add a Collider component to your object.
  10. Test the scene. The Cube will be highlighted red when the mouse is over it (and green when clicked on).

How it works...

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.

There's more...

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.

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

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