Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by empika · Mar 16, 2017 at 08:24 PM · spritesassetdatabasespritesheettextureimporter

How can I programatically set the SpriteMeshType of sprites?

Hey

I have a lot of sprites to import, so have written a script using TextureImportSettings so set the size, filter mode etc. The problem I am facing is that the mesh type (tight/rect) can only be set using Sprite.Create(...), I like it to be Rect rather than the default Tight.

How can I load all my sprites, recreate them with the new SpriteMeshType and then save them out?

Here's my code so far:

         ...create all our meta data etc
         textureImporter.spritesheet = spiteMetaData.ToArray();
         textureImporter.SaveAndReimport();
         AssetDatabase.Refresh();

         string spriteSheet = AssetDatabase.GetAssetPath(theTexture);
         Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(spriteSheet).OfType<Sprite>().ToArray();

         for (int x = 0; x < sprites.Length; x++)
         {
             sprites[x] = Sprite.Create(theTexture, sprites[x].rect, sprites[x].pivot, 1, 2, SpriteMeshType.FullRect);

             // Save sprite here?
         }

         // Or reassign the sprites here?

I think my major hurdle is that it's a bit confusing how sprite data is saved with the texture. You create a bunch of SpriteMetaData, but you get Sprites back. So when you load a sprite, it's created on the fly from the texture and the sprite meta data? If that's the case, how do you set the SpriteMeshType?

I'm unable to see how to save the sprites through any AssetDatabase functions or anything.

Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by talecrafter · Apr 01, 2017 at 07:32 AM

Hi. You can set the SpriteMeshType through SetTextureSettings on the TextureImporter. Here is some sample code from my own utilities.

 public static bool SetTexture2DSpriteMeshType(ref Texture2D texture, SpriteMeshType meshType)
 {
     if (texture == null)
     {
         return false;
     }
 
     string assetPath = AssetDatabase.GetAssetPath(texture);
     TextureImporter importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
 
     if (importer == null)
     {
         return false;
     }
 
     TextureImporterSettings textureSettings = new TextureImporterSettings();
     importer.ReadTextureSettings(textureSettings);
 
     if (textureSettings.spriteMeshType != meshType)
     {
         textureSettings.spriteMeshType = meshType;
 
         importer.SetTextureSettings(textureSettings);
         importer.SaveAndReimport();
 
         return true;
     }
 
     return false;
 }
Comment
Add comment · Show 2 · 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 empika · Apr 05, 2017 at 06:45 PM 0
Share

Perfect! This seems to be undocumented and has been logged as an issue.

avatar image Vadol empika · Nov 09, 2017 at 03:10 AM 0
Share

But "TextureImporterSettings" only works in editor not build

So this is another way without using TextureImporterSettings

         Texture2D tex2d = new Texture2D (GetComponent<Image> ().mainTexture.width, GetComponent<Image> ().mainTexture.height);
         tex2d = GetComponent<Image> ().mainTexture as Texture2D;
         GetComponent<Image> ().sprite = Sprite.Create (tex2d, GetComponent<Image> ().sprite.rect, new Vector2 (0.5f, 0.5f),100,0,Sprite$$anonymous$$eshType.FullRect);
 


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Sprite Editor : Include blank/empty cells when slicing? 4 Answers

Sprite Atlas to save memory 2 Answers

Non-Rectangular sprites in the sprite editor 0 Answers

How to access a child sprite of a sprite sheet from a directory? 0 Answers

Lines between Tiles when using Tile Palette (2021) 1 Answer


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