Search Unity

How do I specify my mip maps manually?

Discussion in 'iOS and tvOS' started by hippocoder, Apr 9, 2012.

  1. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Interested in fake fog.
     
  2. Poya

    Poya

    Joined:
    Jul 3, 2011
    Posts:
    51
    There are a few posts about this on this forum which you should search for...from what I understand there is a "DDS" file format which allows you to manually include all the mipmaps. From what I read, this may or may not work on iOS (if that's what you need). I ended up going a different route, which is to do this in script during the Unity texture import process. The code looks something like this:


    Code (csharp):
    1. public class TexturePostProcessor : AssetPostprocessor
    2. {
    3.     public void OnPostprocessTexture(Texture2D texture)
    4.     {
    5.         if (assetPath.Contains("Resources/Textures/"))
    6.         {
    7.             SetMipMap(texture, 1, "-medium");
    8.             SetMipMap(texture, 2, "-small");
    9.            
    10.             texture.mipMapBias = -1.0f;
    11.         }
    12.     }
    13.    
    14.     public void SetMipMap(Texture2D texture, int mipLevel, string mipPostScript)
    15.     {
    16.         string name = assetPath.Split('/').Last();
    17.         name = name.Remove(name.IndexOf(".png"));
    18.         var mipMap = AssetDatabase.LoadAssetAtPath("Assets/TextureMipMaps/" + name + mipPostScript + ".png", typeof(Texture)) as Texture2D;
    19.         if (mipMap != null)
    20.         {
    21.             texture.SetPixels(mipMap.GetPixels(), mipLevel);
    22.         }
    23.     }
    24. }
    A few notes:
    • You probably don't want to put the "mip maps" in the Resources folder, because you are not actually including them in the final build. They should be somewhere else in your Assets folder.
    • Using GetPixels/SetPixels requires an uncompressed texture. In my case I use that straight in the game because I only use very few textures. However it *should* be possible to compress the texture after this step...you'll need to check the Unity documentation on this.
     
    SudoCat likes this.
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thanks! that was very helpful!