Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Highlight

Discussion in 'Scripting' started by andrewc, Jul 22, 2009.

  1. andrewc

    andrewc

    Joined:
    Aug 20, 2008
    Posts:
    180
    Is there a simple way to make objects highlighted. LIke if you mouse over an object and want it to light up. This seems like something that might be used a lot but I can't find a simple way to do it.
     
  2. mayekol

    mayekol

    Joined:
    Oct 23, 2007
    Posts:
    115
    You could change the shader of the object to a different one you wanted to use for the selection.

    Or you could have a separate that only renders in a selection layer (so it would only light that object) although this would involve changing the current layer the object is in while it was selected which might cause other issues depending on the project.
     
  3. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    You could have a separate object with its renderer turned off as the glow effect. When the mouse moves over the object, the renderer turns on. When the mouse moves off the object, the renderer turns off.
     
  4. slgooding

    slgooding

    Joined:
    Jan 12, 2009
    Posts:
    112
    Maybe a little late, but here you go...

    Code (csharp):
    1.  
    2. // This script will highlight an object on mouseover and hover a gui object if enabled
    3. // Some components from here:
    4. // [url]http://forum.unity3d.com/viewtopic.php?t=8420&highlight=click+object[/url]
    5. //
    6. // Attach to object you want to highlight
    7. // This script will spawn a GUI component based on clicking an object
    8. // You can also setup any type of response
    9.  
    10. var holdTime = 0.5;
    11. var activeCamera : Camera;  // attach main camera, or active camera here
    12. var buttonRect : Rect;
    13. var showGUI = false;
    14. var fade : float = 0.0;
    15. var myText = "Hover Text";
    16. var GUISizeX = 200;
    17. var GUISizeY = 90;
    18. private var timer : float = 0.0;
    19. private var originalColor;
    20. var highlightMultiply = 1.50;
    21.  
    22. function Start() {
    23.         originalColor = renderer.material.color;   
    24. }
    25.  
    26. // When clicking on an object
    27. function OnMouseOver() {
    28.     if (!Application.isPlaying)
    29.         return;
    30.     timer += Time.deltaTime;
    31.     if(timer >=0.25)    {  
    32.         buttonPosn = activeCamera.WorldToScreenPoint(gameObject.transform.position);
    33.        
    34.         //Set position of GUI based on click
    35.         buttonRect = Rect(buttonPosn.x, buttonPosn.y, GUISizeX, GUISizeY);
    36.         showGUI = true;
    37.         timer = 0.0;
    38.     }
    39.    
    40.     if((timer != 0) )   {
    41.         renderer.material.color.r = originalColor.r*highlightMultiply;
    42.         renderer.material.color.g = originalColor.g*highlightMultiply;
    43.         renderer.material.color.b = originalColor.b*highlightMultiply;
    44.         renderer.material.color.b = renderer.material.color.b*highlightMultiply;
    45.     }  
    46. }
    47.  
    48. function OnMouseDown() {
    49.     if (!Application.isPlaying)
    50.         return;
    51.        
    52.     var clickedObject : GameObject = gameObject;
    53.     Debug.Log("Clicked Object =", clickedObject);
    54. }
    55.  
    56. function OnMouseExit() {
    57.     if (!Application.isPlaying)
    58.         return;
    59.        
    60.     showGUI = false;
    61.     timer = 0.0;
    62.     yield new WaitForSeconds(0.1);
    63.    
    64.     // Set color back to original
    65.     renderer.material.color.r = originalColor.r*highlightMultiply;
    66.     renderer.material.color.g = originalColor.g*highlightMultiply;
    67.     renderer.material.color.b = originalColor.b*highlightMultiply;
    68.     renderer.material.color.b = renderer.material.color.b/highlightMultiply;
    69.     renderer.material.color = originalColor;
    70. }
    71.  
    72. function OnGUI() {
    73.   var saveColor= GUI.color;
    74.  
    75.   if(showGUI) {
    76.         // save the color (if we had multiple GUI objects being drawn)
    77.         saveColor= GUI.color;
    78.         GUI.color.a = fade;
    79.         GUI.Box (buttonRect, myText);
    80.    
    81.         //GUILayout.Label("Fade Me");
    82.         GUI.color = saveColor;
    83.         FadeIn();
    84.    }
    85.    else  {
    86.         // save the color (if we had multiple GUI objects being drawn)
    87.         saveColor= GUI.color;
    88.         GUI.color.a = fade;
    89.         GUI.Box (buttonRect, myText);
    90.    
    91.         //GUILayout.Label("Fade Me");
    92.         GUI.color = saveColor;
    93.         FadeOut();
    94.    }
    95. }
    96.  
    97. function FadeIn() {
    98.    // fade out over 2 seconds
    99.    while(fade < 1.0)   {
    100.       fade += Time.deltaTime / 2;
    101.       //fade += 0.001;
    102.       yield;
    103.    }
    104.    fade = 1;
    105. }
    106.  
    107. function FadeOut() {
    108.    // fade out over 2 seconds
    109.    while(fade > 0.0)   {
    110.       fade -= Time.deltaTime / 2;
    111.       yield;
    112.    }
    113.    fade = 0;
    114. }
    115.  
     
  5. slgooding

    slgooding

    Joined:
    Jan 12, 2009
    Posts:
    112
    here is another set of code for highlighting based on a multiple of the objects material. This will let you highlight multiple objects at once. This is done by creating a tag and assigning it to the objects you want to highlight.

    Code (csharp):
    1.  
    2. // Attach to Main Camera
    3. // Add tag to objects you want to highlight
    4. // call functions to add/remove the highlight
    5.  
    6. private var timer : float = 0.0;
    7. private var originalColor;
    8. var highlightMultiply = 1.50;
    9.  
    10. var highlightTag = "OPSM";
    11. private var gos;
    12.  
    13. function Start() {
    14.     gos = GameObject.FindGameObjectsWithTag(highlightTag);
    15.     if(gos) {
    16.         originalColor = gos[0].renderer.material.color;
    17.     }
    18. }
    19.  
    20.  function addHighlight() {
    21.     timer += Time.deltaTime;
    22.     if((timer != 0)  (gos)) {
    23.         for(var go in gos) {   
    24.             go.renderer.material.color.r = originalColor.r*highlightMultiply;
    25.             go.renderer.material.color.g = originalColor.g*highlightMultiply;
    26.             go.renderer.material.color.b = originalColor.b*highlightMultiply;
    27.             go.renderer.material.color.b = go.renderer.material.color.b*highlightMultiply;
    28.         }
    29.     }
    30. }  
    31.  
    32. function removeHighlight() {
    33.     timer = 0.0;
    34.     // Set color back to original
    35.     if(gos) {
    36.         yield new WaitForSeconds(0.1);
    37.         for(var go in gos)
    38.         {  
    39.             go.renderer.material.color = originalColor;
    40.         }
    41.     }
    42. }
    43.  
    44. // Demo Buttons
    45. function OnGUI() {
    46.     if(GUI.Button(Rect(10,10,60,20),"Highlight")) {
    47.         addHighlight();
    48.     }
    49.     if(GUI.Button(Rect(10,35,60,20),"Dull")) {
    50.         removeHighlight();
    51.     }
    52. }
     
    twobob likes this.
  6. KrankyBoy

    KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Just found this post as I was looking for something similiar. I will add this modified script. It allows highlighting on MouseOver.

    Code (csharp):
    1.  
    2.     private float blueMultiply = 3.50f;
    3.     private float redGreenMultiply = 0.50f;
    4.  
    5.     private Color originalColor;
    6.  
    7.     private void Start()
    8.     {
    9.         originalColor = gameObject.renderer.material.color;
    10.     }
    11.  
    12.     void OnMouseEnter()
    13.     {
    14.         AddHighlight();
    15.     }
    16.  
    17.     void OnMouseExit()
    18.     {
    19.         RemoveHighlight();
    20.     }
    21.  
    22.     private void  AddHighlight()
    23.     {
    24.         float red = originalColor.r * redGreenMultiply;
    25.         float blue = originalColor.b * blueMultiply;
    26.         float green = originalColor.g * redGreenMultiply;
    27.  
    28.         gameObject.renderer.material.color = new Color(red, green, blue);
    29.     }
    30.  
    31.     private void RemoveHighlight()
    32.     {
    33.         gameObject.renderer.material.color = originalColor;
    34.     }
    35.  
    Make sure you attach a collider and this script to the object. This script needs to be attached to the object where the renderer is attached.
     
    twobob likes this.
  7. slgooding

    slgooding

    Joined:
    Jan 12, 2009
    Posts:
    112
    This is a modification that does GUITexture swapping.

    This script will swap a GUITexture on the crosshair when you mouse over an object. The script is placed on the object you want to have trigger the change.

    You need assign the default crosshair and the highlight crosshair through the unity editor. The script will look for a gameObject named "crosshair" that has a GUITexture component. It works by changing the texture when triggered. You should be able to take this code and put it in the highlight code.

    Make sure your gameObject this is attached to has a collider, otherwise mouse events will not work.

    Code:

    using UnityEngine;
    using System.Collections;

    public class SwapGUITexture: MonoBehaviour {

    public Texture2D highlightTexture;
    public Texture2D baseTexture;
    GUITexture guiCrosshair;

    // Use this for initialization
    void Start ()
    {
    guiCrosshair = (GUITexture) GameObject.Find("crosshair").GetComponent(typeof(GUITexture)); // Finds a gameObject named "crosshair" and gets its component GUITexture
    }

    void OnMouseEnter()
    {
    guiCrosshair.texture = highlightTexture;
    }

    void OnMouseExit()
    {
    guiCrosshair.texture = baseTexture;
    }

    }
     
  8. Doucettej

    Doucettej

    Joined:
    Jan 18, 2010
    Posts:
    85
    These scripts work great, just one question... how can I go about highlighting an object that contains many shaders. This script only changes the first shader on my object, any help would be greatly appreciated...

    Also is there a way to make the highlight a little more noticeable? Maybe a glow if that's possible? sorry I'm not a coder/scripter so I appreciate all your help and your awesome scripts! :)

    Cheers,
    Jason
     
  9. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    Yeah this would be useful. Generally I think the effect is the lighting of the object changes to be full bright light, and it's given an outline effect.
     
  10. Doucettej

    Doucettej

    Joined:
    Jan 18, 2010
    Posts:
    85
    can you apply an outline effect to the whole object or is that just a per shader thing and also can you script that type of effect? that would do the trick I think.

    I just want to avoid changing any geometry and so would want a scriptable highlight effect.

    Cheers,
    Jason
     
  11. KrankyBoy

    KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    I believe for mine you would need to iterate through all the materials for that object and add a "tint" as I did to each one, no? You can play with those color values to add more or less "tinting".

    As for blur, outlining, glow. I am not sure this is possible. As far as I can see you would need a camera (at the exact same position and rotation as your main) that used a Pro effect on a specific layer, with your objects that you want highlighted in THAT layer. Then you would have to enable/disable the effect on mouseover - in this example there would be a noticeable lag when swaping your main camera to this one and back.

    Hopefully someone else has more experience with adding "glow" Pro effects in this manner.
     
  12. Doucettej

    Doucettej

    Joined:
    Jan 18, 2010
    Posts:
    85
    Ok, you mention with your code you'd have to add the code for each shader for the object correct? I want to know how I define the other shader colors... I am not familiar with how the shader's color's are numbered or how to define the other colors. That's all I really need.

    Thanks!

    Jason
     
  13. KrankyBoy

    KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Gah sorry - I meant color the material not the shader. I don't think you can programmatically alter a shader.

    However I know you can SWAP shaders at runtime - so in effect it is possible.

    I don't know enough about shaders, but if you created two and assigned different colors to them, you could swap them in and out at runtime on a MouseOver().
     
  14. Doucettej

    Doucettej

    Joined:
    Jan 18, 2010
    Posts:
    85
    ok, fair enough. But in the following code
    Code (csharp):
    1.  
    2. if((timer != 0) )   {
    3.       renderer.material.color.r = originalColor.r*highlightMultiply;
    4.       renderer.material.color.g = originalColor.g*highlightMultiply;
    5.       renderer.material.color.b = originalColor.b*highlightMultiply;
    6.       renderer.material.color.b = renderer.material.color.b*highlightMultiply;
    7.  
    we see that this code does apply a color change to the object, but my point is that it only applies to to the uppermost shader in the stack of shaders... So I guess there's no way to affect all the shaders on the object? Seems odd that only the first shader could be altered... or color maybe...

    Thanks for the help so far though :)

    Jason
     
  15. KrankyBoy

    KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Not sure what you mean - I believe you can only have one shader per material. I think you are talking about the different color options on some shaders. Then yeah I think my changes only effect the Main Color property of a shader.
     
  16. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    I'm interested in trying this one out, but it throws a bunch of errors when I use it as its own script. Is it supposed to be attached to one of the other scripts in this thread?

    Thanks for your help in sharing these scripts. They are of tremendous help.
     
  17. KrankyBoy

    KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Yeah it's not the entire script as I do other things (like change the mouse pointer icon on mouse over a useable object as well).

    You place this script on the item to glow. Make sure it has a mesh renderer. What are the errors?
     
  18. Elliot123

    Elliot123

    Joined:
    Nov 6, 2014
    Posts:
    2
    Hey man im trying to disable the renderer of the tagged objects on function Start and OnMouseEnter/ OnMouseExit as well as the addhighlight/remove highlight in OnMouseEnter but im having no look. Im new to code so any help would be much appreciated!

    Here what ive got:

    // Attach to Main Camera
    // Add tag to objects you want to highlight
    // call functions to add/remove the highlight

    private var timer : float = 0.0;
    private var originalColor;
    var highlightMultiply = 1.50;
    var highlightTag = "OPSM";
    private var gos;

    function Start() {
    gos = GameObject.FindGameObjectsWithTag(highlightTag);
    if(gos) {
    originalColor = gos[0].renderer.material.color;
    renderer.enabled = false;
    }
    }

    function addHighlight() {
    timer += Time.deltaTime;
    if((timer != 0) == (gos)) {
    for(var go in gos) {
    go.renderer.material.color.r = originalColor.r*highlightMultiply;
    go.renderer.material.color.g = originalColor.g*highlightMultiply;
    go.renderer.material.color.b = originalColor.b*highlightMultiply;
    go.renderer.material.color.b = go.renderer.material.color.b*highlightMultiply;
    }
    }
    }

    function removeHighlight() {
    timer = 0.0;
    // Set color back to original
    if(gos) {
    yield new WaitForSeconds(0.1);
    for(var go in gos)
    {
    go.renderer.material.color = originalColor;
    }
    }
    }

    function OnMouseEnter(){
    gos.renderer.enabled = true;
    addHighlight == true;

    }
    function OnMouseExit(){
    gos.renderer.enabled = false;
    removeHighlight == true;

    }