Forum rules - please read before posting.

2D Highlighting - Changing Colour of Sprites?

edited October 2014 in Technical Q&A
One of the things I love most in AC is how the highlight script works in 3D - having now started a 2D project, though, I feel like either there's a functionality that isn't quite where I'd like it to be or that I'm not sure how to get working.

Basically, I can see that you have a choice of using either a texture or an icon. I have a feeling that what I want to do might be able to work via texture, but I'm not sure how to do it.

The effect I'd like to achieve would be the same as if I were to go into the sprite for the piece of set geometry and change its colour in the sprite renderer component default white (which just lets the object roll with its own colours) to a bright green, which essentially overrides the colour scheme and just makes it a full-on colourized green. I'm not describing it well but I think you all probably understand what I mean. That bright colourization is essentially what becomes the 'highlighting' of the object.

Is there a way to do this via a texture, or a way to alter the highlight script to have it do this? And to open it up, has anyone had any success with doing any other interesting things with 2D highlighting in AC? :)

Comments

  • The standard "glow" in 3D games is more of an example than anything.  The Highlight script is designed to be overridden with custom effects, should you want them.

    First, assign a Highlight script to your sprite, and link it to the Hotspot, in the normal way.  Then, in it's Inspector, uncheck Brighten materials.  This will stop the script from actually affecting anything, but it'll still keep a track of how "intense" such an effect would be.

    Through a custom script, you could then access it's public function GetHighlightAlpha (), which returns a float between 0 and 1.  You can then use this value to affect the sprite/model in whatever way you like.

    Inside a script attached to the same object:

    float intensity = GetComponent ().GetHighlightAlpha ();
  • Thanks Chris! I'll give it a shot! Gotta wade into some code sooner or later...!
  • Hey Chris,

    OK! Been trying to put this into action with a friend, and have gotten it working but with a snag. Hoping you can help.

    Here's the script we've made in accordance with your direction:

    using UnityEngine;
    using System.Collections;

    public class Newscript : MonoBehaviour {
        
        // Use this for initialization
        void Start () {
        
        }
        
        // Update is called once per frame
        void Update () {

            float colorAlpha = GetComponent<AC.Highlight>().GetHighlightAlpha ();
            gameObject.renderer.material.color = new Color(0, 255, 0, colorAlpha);

        }
    }

    The good news: It works! When I move my character into the hotspot, the sprite highlights with a full bright green.

    The bad news: When my character is not in the hotspot, the sprite completely disappears. I think maybe the alpha is essentially becoming zero when the object is not highlighting. :|

    Any thoughts? From Chris or from anyone. :)

  • edited October 2014
    I think you're confusing the highlight alpha with the *actual* alpha of the sprite.

    The GetHighlightAlpha () function simply returns how intense the highlighting effect should be.  Instead, you'll want to add on a new color that's a factor of the intensity:
    Color originalColor;
    void Start () {
    originalColor = renderer.material.color;
    }
    void Update ()
    {
    float colorAlpha = GetComponent ().GetHighlightAlpha ();
    renderer.material.color = originalColor + new Color (0f, 255f, 0f) * colorAlpha;
    }
  • I appreciate the quick feedback as always, Chris! I get what you mean conceptually, and I think I see how the script you've provided should do that.

    Unfortunately, it doesn't seem to work. The sprite appears as normal when I'm outside of the hotspot, and after entering the hotspot it doesn't seem to have any effect at all. The sprite does not recolor. Is it possible that the additional color that the highlight is adding is somehow not being added to the sprite, or is not visible on top of it, or...?

    Sorry this one has been so tricky.
  • I'll admit I didn't test it, but you see what I'm getting at in principle.  It may depend on your sprite's material, too.  I'd suggest asking on the main Unity forums - this is a more general question, after all, and I'm not necessarily the best to ask.
  • Fair enough - thanks again! I'll post it up here for others to make use of if I get it figured out.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.