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 DrZarqawi · Jul 09, 2018 at 04:45 PM · 2dspritetexture2dsettingsimport settings

Save generated Texture2D as Texture type Sprite

Hi. I have an editor script that generates .PNG images based on set parameters. I then have a Scriptable Object as a data-container for my generated .PNG's, that object accepts sprites. I want my script to automatically generate the Scriptable Object after generating the PNG's. Here is the thing, i need to change the PNG's import settings to sprite for the Scriptable object to accept them.

I found a way to change import settings through script. But that only works as Editor extension, if i try to do the same not using Selection to find the PNG's get a null reference exception.

Snippets: How i save the Texture:

 foreach (RawSpriteData sprite in rawSprites)
         {
             CheckFolder(Application.dataPath + basePath + spriteName + "/" + postFix + "/"); //Function that creates the folder if it doesn't exist
             File.WriteAllBytes(Application.dataPath + basePath + spriteName + "/" + postFix + "/" + sprite.Name + ".PNG", sprite.EncodeToPNG);
             AssetDatabase.SaveAssets(); //In hopes forcing a save would remove the null reference exceprion
         }

How i assign the textures to the scriptable object:

         List<Sprite> textures = new List<Sprite>();
 foreach (RawSpriteData sprite in rawSprites)
         {
             Sprite s = (Sprite)AssetDatabase.LoadAssetAtPath("Assets" + basePath + spriteName + "/" + postFix + "/" + sprite.Name + ".PNG", typeof(Sprite));
             textures.Add(s);
         }
         scrObject.sprites = textures.ToArray();

How I try to change Import settings: (not working)

 public static void ChangeTextureTypeSettings(Texture2D asset, TextureImporterType newType)
     {
         string path = AssetDatabase.GetAssetPath(asset);
         TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
         //Debug.Log("path: " + path);
         textureImporter.textureType = newType;
         AssetDatabase.ImportAsset(path);
     }

The editor extension for changing import settings that i know works:

 static void SelectedChangeTextureTypeSettings(TextureImporterType newType)
     {
 
         Object[] textures = Selection.GetFiltered<Texture2D>(SelectionMode.Assets);
         Selection.objects = new Object[0];
         foreach (Texture2D texture in textures)
         {
             string path = AssetDatabase.GetAssetPath(texture);
             Debug.Log("path: " + path);
             TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
             textureImporter.textureType = newType;
             AssetDatabase.ImportAsset(path);
         }
     }

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 DrZarqawi · Jul 09, 2018 at 10:55 PM

I found the solution myself. What i did was correct my order was wrong. I tried to change import settings before saving it, for some reason...

This however gave me a null reference exception on the selected window, which didn't make any sense to me. but forcing an extra save and refresh after changing the import settings for each sprite solved that.

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

196 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 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 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 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

My sprite from sprite.create looks pixelated. 1 Answer

Texturing custom 2d sprite 0 Answers

Updating Sprite texture leaves red outline 1 Answer

Unity UpdateExternalTexture from MovieTexture windows crash 0 Answers

Is it impossible to swap the Texture2D of a Sprite Renderer at runtime? 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