Highlighting an interactive object

In a game where you select / drag / drop / etc objects (like an RTS or a building game etc) you’d want to highlight the currently selected object. Be it a stationary selection (until you select something else) or a temporary selection (while you’re clicking/dragging it) or even just mouse-over, you’d want to highlight the object to show the player what’s currently being interacted with.

What’s a good (efficient) way of implementing this highlighting visual effect? so that
(pseudocode)

when (mouse is over object OR mouse clicked on object)
then HighlightObject()

I’m just a noob so i mostly have no idea what i’m talking about, but i’d imagine the first solution that would come to mind is just change the material to something lighter, or add a material that lights up the object a bit more, or somesuch.

But i have a feeling changing materials isnt the most efficient solution. I further suspect the way its sometimes done is to paint the 2D shape of the rendered object with some diffuse color, or somesuch.

Suggestions would be greatly appreciated.

P.S. come to talk about “2D shape of a rendered object”, does Unity have a picking/selecting method by way of color code rendering? Isn’t that more efficient than ray cast picking (especially if i’m doing mouse-over raycasting tests every frame) ?

uh… should i get the impression nobody understood what i’m talking about?

I have a feeling not to many people are currently IN this section.

Other than that I think we have a firm understanding of what you mean.

As far as I know, you can use javascript as a base and use the function OnMouseDown

example:

function OnMouseDown () {
     (insert some sort of material thing here)
}

As for the section on changing it to a lighter color or such, I’ll take a look into that and get back to you in a few.

I tried to get the same thing working yesterday. Here’s what I did -

in the OnMouseOver and OnMouseExit I set a _hovering flag true/false respectively.

Then in the update method I do this:

alpha = _renderer.material.color.a
if (_hovering):
    if (alpha < 1.0):
        alpha += 0.05
        _renderer.material.color = Color(1, 1, 1, alpha)
else:
    if (alpha > 0.1):
        alpha -= 0.05
        _renderer.material.color = Color(1, 1, 1, alpha)

This fades to full alpha on an object that’s under the mouse and leaves other objects at 0.2 alpha.

I tried setting the _renderer.material.color.a value directly but it had no effect for some reason. However if you assign a completely new Color to the material, then it works (there’s probably something I’m missing here, so if anybody knows why I can’t set the maeterial.color.a directly, I’d be glad to hear it)

(edit: of course, in your game, you might want to assign a premade highlight material maybe, or maybe set a non-transparent color that gets blended with your model textures)

Much appreciated guys!

Thanks ExKage, i’ll be checking back.

You can’t assign the color directly because Color is a struct, so you are effectively only changing a copy.