Hello Guest

Author Topic: Source texture dimensions  (Read 6961 times)

dakeese

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Source texture dimensions
« on: December 30, 2012, 04:07:03 am »
I've run into a few issues when I import textures to Unity to be used as sprites.

Ideally, a texture should be set to Non Power of 2 = none in its import settings, and it should not be distorted.

Here are problems I've run into. Wondering if there are work-arounds:

1. If the texture is non-square but does have power-of-two dimensions (for example, 128x256), then Unity forces it to be square (the NonPowerOf2 settings is grayed out and set to ToNearest), and it will be distorted into a square in the SpriteCollection. This may be more of a Unity problem than anything else.

2. This is just an annoyance. Most of the time, non-power-of-two textures are imported with the Texture Type set to Texture, so I have to manually change it to Advanced and change the Non Power of Two setting to None. Can this be automated somehow by tk2d? Sometimes new textures do get imported with the settings I want, but I'm not sure why.  For now I'm just going to write a menu command that fixes texture settings for me.

dakeese

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Source texture dimensions
« Reply #1 on: December 30, 2012, 04:23:45 am »
Editor script for fixing number 2 by batch applying settings. Pretty easy. I don't know if some of these settings matter to tk2d, but I figure turning off mipmap generation might speed things up for Unity:

Code: [Select]
[MenuItem("Tools/Fix Sprite Source Texture Import Settings", false, 47)]
static void FixSpriteImportSettings ()
{
TextureImporterSettings textureSettings = new TextureImporterSettings ();
textureSettings.npotScale = TextureImporterNPOTScale.None;
textureSettings.mipmapEnabled = false;
textureSettings.normalMap = false;
textureSettings.readable = true;

Object[] textures = Selection.GetFiltered (typeof(Texture2D), SelectionMode.DeepAssets);
Selection.objects = new Object[0];
foreach (Texture2D texture in textures) {
string path = AssetDatabase.GetAssetPath (texture);
TextureImporter textureImporter = AssetImporter.GetAtPath (path) as TextureImporter;
textureImporter.SetTextureSettings(textureSettings);
AssetDatabase.ImportAsset (path);
}
Selection.objects = textures;
}
« Last Edit: December 30, 2012, 04:25:29 am by dakeese »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Source texture dimensions
« Reply #2 on: December 30, 2012, 08:40:06 am »
It does do this (except turning off mipmaps), but on first commit. Are you having an issue after committing?

dakeese

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Source texture dimensions
« Reply #3 on: December 31, 2012, 01:26:33 am »
 :-[ It's working fine! I just didn't think to try hitting Commit before I had the issue fixed.  My usual workflow is to solve problems when I notice them before proceeding so they don't snowball, so I stopped once I saw the distorted texture in the Collection editor.

So you found a workaround to override the power of two setting for non-square textures that are already power of two?  When I run my above script on a texture like that, it just creates a host of editor errors.

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Source texture dimensions
« Reply #4 on: December 31, 2012, 06:18:13 am »
No, but there isn't any error with a non-square po2 texture going into a sprite collection when I tested it. I tried 128x64 and 256x128. Was there an actual error in your sprite collection atlas texture?

dakeese

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Source texture dimensions
« Reply #5 on: January 01, 2013, 06:25:11 pm »
    No, the atlas was fine once I clicked Commit.

    Here's the problem I had:
    • Imported 256x128 png into Unity. Unity stretched it to a square and grayed out the Power of 2 setting in the Advanced mode of the texture import settings for the file.
    • Running my own script above created a bunch of errors. Deleted file and imported a new one with a different name but same dimensions. Unity still streteches it to a square.
    • Dragged it into my sprite collection. It appears as a stretched square.
    • Clicked Commit. Sprite is no longer stretched in the sprite collection or in the Unity editor, although the import settings still show the Power of 2 setting grayed out (but now set to none instead of nearest).

    Doesn't really matter since you already have it fixed. I was just curious what you did to get around that issue.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Source texture dimensions
« Reply #6 on: January 02, 2013, 09:19:11 am »
The code which switches the texture settings is in tk2dSpriteCollectionBuilder.cs, ConfigureSpriteTextureImporter
That is exactly what gets run after importing for the first time.