How to highlight a 3D object in Unity?

Highlighting 3d objects
 


What is Emission?

Emission is a parameter of the default unlit/Lit shader that allows us to make it appear as the object is a light source. In our case we want to use it as a way to give feedback to the player that they have selected this object and can interact with it.

 


Problem - Objects Usually have more than 1 material on them:

Usually an object can have multiple parts (could objects) or multiple materials on them.

 

Each of the child objects: bunBottom, butTop, cheese… has its own material.

To highlight the whole object we need to enable emission and set the specific color on each of those materials.

We can create a custom script to deal with it for us:

Ex: Highlight.cs

 
public class Highlight : MonoBehaviour
{
    //we assign all the renderers here through the inspector
    [SerializeField]
    private List<Renderer> renderers;
    [SerializeField]
    private Color color = Color.white;

    //helper list to cache all the materials ofd this object
    private List<Material> materials;

    //Gets all the materials from each renderer
    private void Awake()
    {
        materials = new List<Material>();
        foreach (var renderer in renderers)
        {
            //A single child-object might have mutliple materials on it
            //that is why we need to all materials with "s"
            materials.AddRange(new List<Material>(renderer.materials));
        }
    }

    public void ToggleHighlight(bool val)
    {
        if (val)
        {
            foreach (var material in materials)
            {
                //We need to enable the EMISSION
                material.EnableKeyword("_EMISSION");
                //before we can set the color
                material.SetColor("_EmissionColor", color);
            }
        }
        else
        {
            foreach (var material in materials)
            {
                //we can just disable the EMISSION
                //if we don't use emission color anywhere else
                material.DisableKeyword("_EMISSION");
            }
        }
    }
}

How to use it:

 

All we need to do is to add the component Highlight.cs onto the object that we want to select and assign the renderers that we have manually (or we can use GetComponentsInChildren :) )

Now all you need is to get a reference to the Highlight.cs and use its Toggle(bool val) method:

    hit.collider.GetComponent<Highlight>()?.ToggleHighlight(false);
 

I hope that you will find this method helpful! :) Here is my Youtube series about picking up objects that uses this method youtu.be/pzaxC-P3sgs

Want to learn more about Unity ?

Check out my OOP for Unity Devs video course!

 
 

You can also support me through Patreon:

 

If you agree or disagree let me know by joining the Sunny Valley Studio discord channel :)

Thanks for reading!

Peter

Previous
Previous

How to add sprite preview to the Inspector window

Next
Next

How to select 2D objects using mouse in Unity?