Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by CrownR · Mar 02, 2016 at 05:02 PM · spriteborder

How to modify unity sprite border by script?

Hi all, is here any way to modify sprite border by script?

I am trying to write a script to modify border parameter with opening Sprite Editor,

but cannot find any Editor API to do that, is it possible?

If not, could I write a function to open Sprite Editor directly?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mcsamuel · May 03, 2016 at 04:34 AM

As far as I know you can't. However, you can recreate a sprite and then redefine its border using the Sprite.Create function. Once you have the new sprite you can replace the existing one.

For example if you have an existing sprite simply do something like:

 Vector4 newBorder = new Vector4 ( 2, 2, 2, 2);
 Rect rect = new Rect( 0,0, oldSprite.texture.width, oldSprite.texture.height);
 Sprite newSprite= Sprite.Create(oldSprite.texture, rect, new Vector2(0.5f,0.5f),  100, 1, SpriteMeshType.FullRect, newBorder );

Hopefully this helps you get closer to what you are trying to do.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by AD110 · Oct 10, 2019 at 11:38 AM

newSprite.border.Set(x, y, z, w);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by UsefulYdyot · Jun 19, 2021 at 01:12 AM

With AssetImporter it works. Got the idea (and main parts of the code) from https://answers.unity.com/questions/943797/editor-script-to-slice-sprites.html

In this example code it works by selecting one ore more sprites in the project window and executing the menu command below.

 [MenuItem("Tools/Textures/Modify Sprite Border")]
 public static void ModifySpriteBorder()
 {
         Sprite[] sprites = Selection.GetFiltered<Sprite>(SelectionMode.Deep);
         MoveTextureSprites(sprites[0].texture, sprites);
 }
 
 static void ModifyTextureSprites(Texture2D texture, Sprite[] sprites)
 {
         string path = AssetDatabase.GetAssetPath(texture);
         var importer = AssetImporter.GetAtPath(path) as TextureImporter;
 
         importer.textureType = TextureImporterType.Sprite;
         importer.spriteImportMode = SpriteImportMode.Multiple;
         importer.mipmapEnabled = false;
         importer.filterMode = FilterMode.Point;
         importer.spritePivot = Vector2.down;
         importer.textureCompression = TextureImporterCompression.Uncompressed;
 
         TextureImporterSettings textureSettings = new TextureImporterSettings();
         importer.ReadTextureSettings(textureSettings);
         textureSettings.spriteMeshType = SpriteMeshType.Tight;
         textureSettings.spriteExtrude = 0;
 
         string[] spriteNames = new string[sprites.Length];
         for (int i = 0; i < sprites.Length; i++)
                 spriteNames[i] = sprites[i].name;
 
         SpriteMetaData[] spritesheet = importer.spritesheet;
         for (int i=0; i<importer.spritesheet.Length; i++)
         {
                 SpriteMetaData data = importer.spritesheet[i];
 
                 if (!spriteNames.Contains(data.name))
                         continue;
 
                 // HERE MODIFY  data.border
                 spritesheet[i] = data;
         }
 
         importer.spritesheet = spritesheet;
 
         AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
         return;
 }

Not sure, if the importer configuration part is needed.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I resize a sprite using the borders? 2 Answers

How can I add a border to my sprites? 1 Answer

How to Get Image edge size 2D?? 1 Answer

How to create an outline or border for sprites? 2 Answers

How to change 2D sprite border ?,How to change 2D spite border ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges