Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 joshrs926 · Feb 09 at 08:49 PM · scripting problemeditoreditor-scriptingtexture2dformat

[Solved] How can I change Texture2D format in Editor Script?

I want to change a texture format to RGBA32 so I can then call Texture2D.GetRawTextureData and expect 4 byte pixels. This is for an editor script. I know about getting the TextureImporter and the GetPlatformTextureSettings and SetPlatformTextureSettings methods. But these methods ask for a platform string and "Default" is not a legal option. How can I change the default platform texture format through code?

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 joshrs926 · Feb 09 at 09:52 PM

Ok I figured it out! You simply call TextureImporter.GetDefaultPlatformTextureSettings() to get a TextureImporterPlatformSettings instance with the current settings from the texture2d, adjust its fields how you want, then feed it back into TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings platformSettings). Here is a useful class for adjusting settings of a Texture2D asset in the Editor.

 using UnityEngine;
 using UnityEditor;
 
 public class TextureReimporter
 {
     TexAsset[] assets;
 
     class TexAsset
     {
         public Texture2D texture;
         public TextureImporter importer;
         public TextureImporterSettings original;
         public TextureImporterSettings copy;
         public TextureImporterPlatformSettings originalPS;
         public TextureImporterPlatformSettings copyPS;
         public bool isValid;
 
         public TexAsset(Texture2D texture)
         {
             isValid = AssetDatabase.IsMainAsset(texture);
             if (isValid)
             {
                 this.texture = texture;
                 importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
                 original = new TextureImporterSettings();
                 copy = new TextureImporterSettings();
                 originalPS = new TextureImporterPlatformSettings();
                 copyPS = new TextureImporterPlatformSettings();
                 importer.ReadTextureSettings(original);
                 original.CopyTo(copy);
                 originalPS = importer.GetDefaultPlatformTextureSettings();
                 originalPS.CopyTo(copyPS);
             }
         }
     }
 
     public TextureReimporter(params Texture2D[] textures)
     {
         assets = new TexAsset[textures.Length];
         for (int i = 0; i < textures.Length; i++)
         {
             assets[i] = new TexAsset(textures[i]);
         }
     }
 
     public void ImportWithSettings(
         bool isReadable = true,
         TextureImporterType type = TextureImporterType.Default,
         TextureImporterFormat format = TextureImporterFormat.RGBA32)
     {
         foreach (var asset in assets)
         {
             if (asset.isValid)
             {
                 asset.copy.readable = isReadable;
                 asset.copy.textureType = type;
                 asset.copyPS.format = format;
                 asset.importer.SetTextureSettings(asset.copy);
                 asset.importer.SetPlatformTextureSettings(asset.copyPS);
                 if (AssetDatabase.WriteImportSettingsIfDirty(asset.importer.assetPath))
                 {
                     asset.importer.SaveAndReimport();
                 }
             }
         }
     }
 
     public void Reset()
     {
         foreach (var asset in assets)
         {
             if (asset.isValid)
             {
                 asset.importer.SetTextureSettings(asset.original);
                 asset.importer.SetPlatformTextureSettings(asset.originalPS);
                 if (AssetDatabase.WriteImportSettingsIfDirty(asset.importer.assetPath))
                 {
                     asset.importer.SaveAndReimport();
                 }
             }
         }
     }
 }

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

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

Editor class "Texture Importer" question (applying settings to multiple texture assets). 2 Answers

How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer

How to get the currently selected object in the project window only 2 Answers

How to use an Editor script? 0 Answers

How can I check if a key is down in scene view? 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