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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

PSD Importer: Add spacing to generated sprite sheet and sprite meshes?

Discussion in '2D Experimental Preview' started by mertlow, Apr 21, 2021.

  1. mertlow

    mertlow

    Joined:
    Jul 26, 2017
    Posts:
    3
    Hi!

    I am trying to use some shaders that allow for border/glow effects around the outside of sprites. They work fine for single sprites, because it's easy enough to add additional transparent space around the sprite to account for the additional border that is added through the shader.

    I am wondering if there is any way to add transparent space around the individual sprites in a sprite sheet generated by the PSD Importer? Similarly, is there a way to generate meshes in the skinning editor that are expanded, so that they include transparent space? I can edit the individual mesh vertices, but having to drag them all further away from the sprite perimeter would be a hassle.

    Also, I've tried changing the extrude value of the imported PSB prefab, but that doesn't seem to affect anything.

    Thanks,
    Matt
     
    Last edited: Apr 21, 2021
    znagler likes this.
  2. NullQubit

    NullQubit

    Joined:
    Jun 1, 2015
    Posts:
    11
    Hey did you find a solution to this?
     
  3. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Currently, this is not supported in the PSD Importer package. We are, however, looking at adding the ability to specify a "Sprite padding" value on Sprite generation in a future version of the PSD Importer.
     
    ihgyug likes this.
  4. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    try highlights Plus in asset store for glow around sprites
     
  5. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,398
    Seconding this request with another usecase:
    Sometimes you can save trianglecount by placing the vertex slightly away from the actual outline and that's not possible when there is the hard border of the sprite in the way.
     
    Ted_Wikman likes this.
  6. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
  7. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    An update on the Sprite padding request. This is now available in PSD Importer (8.x) for Unity 2022.2.
    (See Sprite Padding in the manual).

    Give it a go if you have the time and let us know if it solves your issues.
    Thanks for the feedback!
     
    JonPQ and DragonCoder like this.
  8. AdamBebkoSL

    AdamBebkoSL

    Joined:
    Dec 9, 2021
    Posts:
    33
    @Ted_Wikman
    Is there any way to have this functionality in 2021!? this is a must-have for glow effects.
     
  9. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    No sorry, we will not backport this functionality.
     
  10. AdamBebkoSL

    AdamBebkoSL

    Joined:
    Dec 9, 2021
    Posts:
    33
    :( that's sad, but at least y'all are putting it in! Thanks for the quick response.
     
    Ted_Wikman likes this.
  11. AdamBebkoSL

    AdamBebkoSL

    Joined:
    Dec 9, 2021
    Posts:
    33
    For future users, I created a script that at runtime will add a transparent padding around textures. This allows my outline shaders to function properly.

    Code (CSharp):
    1. public class OutlineTexture : MonoBehaviour
    2. {
    3.  
    4.     SpriteRenderer _spriteRenderer;
    5.     [SerializeField] int _expandAmt = 40;
    6.  
    7.     void Awake()
    8.     {
    9.         _spriteRenderer = GetComponent<SpriteRenderer>();
    10.     }
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         Texture2D texture = _spriteRenderer.sprite.texture;
    16.         var pixels = texture.GetPixels32();
    17.  
    18.         int newWidth = texture.width + _expandAmt * 2;
    19.         int newHeight = texture.height + _expandAmt * 2;
    20.        
    21.         Color32[] expandedPixels = new Color32[newWidth * newHeight];
    22.         Color32 color = Color.clear;
    23.         for (int i = 0; i < expandedPixels.Length; i++)
    24.         {
    25.             expandedPixels[i] = color;
    26.         }
    27.         Texture2D newTexture = new Texture2D(newWidth, newHeight);
    28.         newTexture.SetPixels32(expandedPixels);
    29.         newTexture.SetPixels32(_expandAmt,_expandAmt, texture.width,texture.height, pixels);
    30.         newTexture.Apply();
    31.  
    32.         Sprite newSprite = Sprite.Create(newTexture,
    33.             new Rect(Vector2.zero, new Vector2(newWidth, newHeight)),
    34.             new Vector2(.5f,0.5f),
    35.             100,0, SpriteMeshType.FullRect); // full rect is required.
    36.         _spriteRenderer.sprite = newSprite;
    37.         _spriteRenderer.drawMode = SpriteDrawMode.Sliced; // not sure why this is needed but it is.
    38.         ]
    39.     }
    40.  
    41. }
    I'm sure it has a performance hit. But at least in my app it's not measurably worse for about 5 outlined sprites in my scene. It does increase the initial load time slightly so there's a frame of stutter when they turn on where the fps drops to about 50. Not sure if this script is the cause though since for now that's acceptable. Might try to optimize that a bit in the future.