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.
  2. Dismiss Notice

Question 2D Sprite Outline for URP?

Discussion in '2D' started by anikataylor, Apr 1, 2023.

  1. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    42
    Hey so I've searched the internet for so long and I can't find any good working solutions for a 2D sprite outline that works in Unitys URP.

    After trying so many different methods online the best one I could find is this one which kinda works but I think this is best suited to pixel art games (which my game isn't) because all my outlines turn my sprites jaggedy and pixelly and it doesn't look nice at all,

    so please if someone has the code for a shader or method for a shader graph that can give me a nice looking 2d sprite renderer outline for URP, it would be greatly appreciated, thank you.
     
  2. steven1022

    steven1022

    Joined:
    Sep 8, 2012
    Posts:
    21
    Have you tried duplicating the sprite, making the copy a bit larger and behind, and changing the color? I did that in a 2d game, felt hacky but worked great after my system was set up. With animations and all if you couple them it’s a solution.

    I was also running into shader compatibility issues on mobile though. Just a thought!
     
    anikataylor likes this.
  3. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    42
    Thanks for the idea, however yes I have tried this out and didn't get the desired effect I would like because firstly, on darker sprites, I can't change the sprite color to be lighter and it remains rather dark so I can't end up having the same color outline for all my sprites. And secondly I tried sizing up, but this doesn't give an equal outline all the way around my sprite.

    This is what I mean for example:


    Thanks for the help, but I think I may try and find another way.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    for an accurate size outline scaling up is not the way

    you have to create 8 copies of the sprite and then offset them in the 8 cardinal directions behind your sprite

    it feels hacky but it will give a correct outline as opposed to scaling

    Ofc you will have to change the color of the sprites that are behind the true sprite to a dark color otherwise it will look messed up

    otherwise, you can use Texture2D.SetPixel to draw an outline around your sprite, this follows the same line of thinking as drawing an outline in the 8 cardinal directions and you will have to pick faded colors for the edges if you want to avoid a jagged pixel look
     
    anikataylor likes this.
  5. steven1022

    steven1022

    Joined:
    Sep 8, 2012
    Posts:
    21
    I didn't need it perfect but karderos makes good points if you need perfection. I was fine with a simple scale up.

    To solve your issue on the color of the sprite, use a material that changes the color to all white AND then change the color to whatever you want. If you add a material to the outline copy like this simple one it will solve trying to recolor a lit sprite.

    Here are some pictures of the Material, the Sprite Outline object using the material, and the result. In any case you always have to find the best fit for your project. Good luck!

    upload_2023-4-2_12-9-4.png

    upload_2023-4-2_12-4-29.png upload_2023-4-2_12-8-53.png upload_2023-4-2_12-9-4.png
     
    anikataylor likes this.
  6. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    42
    Thanks I'll try both these methods out, I wish there was a more simple way to do this that doesn't seem as 'hacky' as you put it, but this may be my only options, so thank you!
     
  7. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    42
    Ah I never thought about changing the material to white to solve the color issue, will also try this out, thanks for the help guys.
     
  8. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    73

    Hi anikataylor,

    I think this video can help to achieve the outline.

     
    anikataylor likes this.