Search Unity

TextureImporter and sprites

Discussion in '2D' started by bakman, Oct 19, 2014.

  1. bakman

    bakman

    Joined:
    Dec 16, 2012
    Posts:
    6
    Hello,
    I would like to configure a sprite in multiple mode on runtime by this way:

    Code (CSharp):
    1.  public void ConfigTexture()
    2.     {
    3.         TextureImporter importer = TextureImporter.GetAtPath("Assets/Resources/idle.png") as TextureImporter;
    4.         importer.textureType = TextureImporterType.Advanced;
    5.         importer.maxTextureSize = 2048;
    6.         importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
    7.         importer.isReadable = true;
    8.         importer.filterMode = FilterMode.Bilinear;
    9.         importer.npotScale = TextureImporterNPOTScale.None;
    10.         importer.wrapMode = TextureWrapMode.Clamp;
    11.         importer.mipmapEnabled = false;
    12.         importer.spriteImportMode = SpriteImportMode.Multiple;
    13.  
    14.         UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(importer.assetPath, typeof(Texture2D));
    15.         if (asset)
    16.         {
    17.             EditorUtility.SetDirty(asset);
    18.  
    19.         }
    20.         else
    21.         {
    22.             importer.textureType = TextureImporterType.Advanced;
    23.         }
    24.     }
    The problem is, the asset in Resource folder look like Texture2D not a multiple Sprite with left arrow who permit to see sprites. How can I configure an image to a sprite properly in runtime ?
    Thank you.
     
  2. bakman

    bakman

    Joined:
    Dec 16, 2012
    Posts:
    6
    I found it!
    I added this :
    Code (CSharp):
    1. AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
    complete code if anybody want to use it :
    Code (CSharp):
    1.     public void ConfigTexture()
    2.     {
    3.         TextureImporter importer = TextureImporter.GetAtPath("Assets/Resources/idle.png") as TextureImporter;
    4.         importer.textureType = TextureImporterType.Advanced;
    5.         importer.maxTextureSize = 2048;
    6.         importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
    7.         importer.isReadable = true;
    8.         importer.filterMode = FilterMode.Bilinear;
    9.         importer.npotScale = TextureImporterNPOTScale.None;
    10.         importer.wrapMode = TextureWrapMode.Clamp;
    11.         importer.mipmapEnabled = false;
    12.         importer.spriteImportMode = SpriteImportMode.Multiple;
    13.  
    14.         UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(importer.assetPath, typeof(Texture2D));
    15.         AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
    16.         if (asset)
    17.         {
    18.             EditorUtility.SetDirty(asset);
    19.             Debug.Log("dirty");
    20.         }
    21.         else
    22.         {
    23.             importer.textureType = TextureImporterType.Advanced;
    24.             Debug.Log("not dirty");
    25.         }
    26.     }
    See ya :)
     
  3. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,047
    Hi I have an issue where the importer always comes back null even though the file is in the directory . Any suggestions?

    Also can it only run in Unity Editor script? or at normal run time as well?
     
  4. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,047
    I figured out my issue. I was using the full path to my asset when the path should be relative to your project.