I’m trying to create a 9-patch Sprite programmatically using scripting. I’ve loaded my Texture2D (a single 32x32 pixel image) and used Sprite.Create() to generate a full size Sprite with Border set as (0.25, 0.25, 0.25, 0.25) which should give me a 8 x 16 x 8 pixels for both horizontal and vertical slicing - essentially 8x8 pixel blocks for each corner that stretch in other directions. When I do this in the Editor and set my SpriteRenderer to the Sprite, with DrawMode set to Sliced, it displays perfectly. However, when I try to do this via script it does not work. When I inspect the SpriteRenderer in the Editor and open the Sprite - I see that the Border is set to (0.01, 0.01, 0.01, 0.01), however when I log the Sprite.Border values, it is correctly set to (0.25, 0.25, 0.25, 0.25). For some reason, even thought the SpriteRender is set to a Sprite with accurate borders, it doesn’t seem to be respecting them. Am I doing something wrong here?
Adding some screenshots for context:
SpriteRenderer 
Sprite.Border from Debug.Log ![]()
SpriteRenderer Inspector
Sprite Inspector (see border incorrectly set) 
I’m now fairly convinced that this is a bug. Here are the repro steps:
Put the image below (slice.png) in the Assets/Resources folder of a new project.
![]()
Add this code to the Start() of a new script for the default scene.
Texture2D texture2D = Resources.Load("slice") as Texture2D;
Sprite sprite = Sprite.Create(texture2D, new Rect(0.0f, 0.0f, 32.0f, 32.0f), new Vector2(0.5f, 0.5f), 1.0f, 0, SpriteMeshType.FullRect, new Vector4(0.25f, 0.25f, 0.25f, 0.25f), false);
GameObject gameObject = new GameObject();
SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = sprite;
spriteRenderer.drawMode = SpriteDrawMode.Sliced;
spriteRenderer.size = new Vector2(64.0f, 128.0f);
Run and you will see that the SpriteRenderer is set up correctly, but the Sprite is using something without the correct border. If you Pause and manually change the Sprite in editor to a non-script generated sprite that has been sliced with this border, it will show up correctly. Can someone from @ please comment on where this may be wrong or if this is a bug?
Hi @MelvMay - I saw you active recently and wanted to doublecheck: Is SpriteRenderer.SpriteDrawMode.Sliced using a Sprite with Border available via scripting? I can’t seem to find a way to load them dynamically into the scene. Is there an alternative approach to using a 9-patch via code? Is there somewhere in the documentation that I’m missing? Apologies in advance if I’ve missed something in the manual.
I’ll pass your question onto the 2D team because I’m responsible for 2D physics exclusively.
Hi, apologies for the confusion; though the Inspector is showing the normalize value and the API docs did not mention it specifically, the border value is actually in pixel value i.e. for a 25% value in your case would be
Sprite.Create(texture2D, new Rect(0.0f, 0.0f, 32.0f, 32.0f), new Vector2(0.5f, 0.5f), 1.0f, 0, SpriteMeshType.FullRect, new Vector4(8,8,8,8), false);
We will look at including that information in the API docs and make it clearer in the Sprite’s inspector.
Thanks for the feedback on this!
