- Home /
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?
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
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 ;)
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.
Your answer
Follow this Question
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