Search Unity

Question Slicing UI button in multiple areas

Discussion in 'UGUI & TextMesh Pro' started by uvviesigma, Jan 12, 2023.

  1. uvviesigma

    uvviesigma

    Joined:
    Jul 28, 2020
    Posts:
    3
    I have this button (or any button for that matter, that isn't continuously stretchable between the sides):


    Upon slicing it with the Sprite Editor, it understandably resizes the button by also stretching the middle area:





    Is there by chance a workaround to keeping the middle area intact, or a means to only stretch multiple areas like below?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    I don't think it could do that natively... maybe fake it by two images side-by-side and your button only having HALF of its current width, then flip it on the other side. If you made a scalable prefab out of that, it might be pretty close to what you want.

    Otherwise you could always get the source for
    Image.cs
    and hack it to support your spiffy little five-slice method!

    The generate sliced sprite code starts on line 970 here:

    https://github.com/Unity-Technologi...4051da525913c/UnityEngine.UI/UI/Core/Image.cs

    Looks like 982 gets the inner / outer rects, padding and border, then just calculates the verts and streams them out in a loop.
     
  3. uvviesigma

    uvviesigma

    Joined:
    Jul 28, 2020
    Posts:
    3
    Thanks for the input, I eventually found a script that allows a button to have the two halves as multiple target graphics, so far this was the most satisfying workaround... unless another problem shows up in the future :p

    I've also had a good look at that method, but it looks more complicated than I thought (and beyond my level of programming skill). Would it be enough if I just changed the way sliced sprites are made in that method, given there's just a single border? Maybe it would also affect how other sprites are being sliced too
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Oh that's a perfect workaround. Thanks for writing back.

    You should post the link to the script for others.

    Good question... I kinda like your solution more though. Every line of code that I don't have to write! :)
     
  5. uvviesigma

    uvviesigma

    Joined:
    Jul 28, 2020
    Posts:
    3
    Kurt-Dekker likes this.