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 ptc-gkanai · Oct 28, 2019 at 10:00 AM · editortexturenon-power-of-two

How can you tell if a texture is NPOT or not, and what its NPOT import settings are

Hey,

I'm currently trying to implement a method, that returns true or false based on if it is NPOT (non-power of two) or it is POT. In the "Advanced" settings in the Unity editor there's a enum-value called Non-Power of 2 which I want to read from a script, but I can't seem to find a method similar to isReadeable to see what is selected. What should be the proper way to solve this problem?

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
3
Best Answer

Answer by Bonfire-Boy · Oct 28, 2019 at 11:15 AM

This is a common way to test if an integer is POT or not, which (was originally) the only question in the title and the first sentence of the body of the question.

 public static class NPOTExtensions 
 {
     public static bool IsPowerOfTwo(this int i)
     {
         return ((i & (i - 1)) == 0);
     }

     public static bool IsPowerOfTwo(this Texture t)
     {
         return t.width.IsPowerOfTwo() && t.height.IsPowerOfTwo();
     }
    
 }

In the body of the question you go on to talk about wanting to read the value of the Non-Power of Two import setting, which is a different thing.

That field only even appears if the raw data is non-power-of-two (which I would imagine the editor works out using a function like I've shown), and tells Unity how to handle the fact on import. It's not a property of the imported texture.

If you actually need to access the NPOT setting for a NPOT texture, you're probably best off explaining why, because that's a whole other ball game. I believe you could do something using an asset preprocessor and the UnityEditor.TextureImporter class (which defines the behaviour) but I've already gone further than necessary if all you actually need to do is handled by the code above

Comment
Add comment · Show 3 · 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 ptc-gkanai · Oct 28, 2019 at 12:54 PM 0
Share

Thank you for answering! So yes, I needed to access the NPOT setting, but I didn't know how, but running through the documentation the TextureImporter class does it things for me. What I did basically is :

  TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(SerializedObject.myImage));
     if(importer.npotScale != TextureImporterNPOTScale.None){
     // Do one thing
     }else{
     // Do another
     }

This solution is pretty straightforward and you can do any kind of changes with the help of it to the import settings of the texture for example making a GUI button and then pass in the importer object and set any kind of values you want to. But dont forget to call "SaveAndReimport" on the importer ;)

avatar image Bonfire-Boy ptc-gkanai · Oct 28, 2019 at 03:18 PM 0
Share

Happy to help and glad you found what you needed.

I've edited the title of the question to include the bit about NPOT import settings, to make it more likely that someone interested in that aspect will find what you've done. I'm sure it will prove useful to someone at some point.

avatar image ptc-gkanai Bonfire-Boy · Oct 28, 2019 at 03:21 PM 0
Share

Good idea!

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

179 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

Related Questions

Generate a gradient texture from editor script 0 Answers

How to make changes to a copied object 1 Answer

How to add multi texture on runtime as "window" array 0 Answers

Are textured/z-clipped gizmos possible? 0 Answers

All textures constantly changes "Texture Type" to "Advanced" leaving weird light issues 2 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