title: Loading a PNG image as a texture at runtime in Unity3d
body: <p>I'm using a series of PNG images as sprites to create animations on a plane for an augmented reality application in Unity. The PNG images are loaded as textures, and the textures are applied to a plane to create an animation sequence that tracks with an augmented reality target.</p>
  <p>Here is the script that is attached to the plane and controls the animation sequence.</p>
  <h3>File PNGSequence.js</h3>
  <pre><code>#pragma strict
  var tecture : Texture[];
  
  var changeInterval : float = 0.06;
  var isRunning = false;
  var isLooping = false;
  
  function Start () {
      var isRunning = true;
  }
  
  function Update () {
      if( isRunning == true) {
          Animation ();
      }
  }
  
  function Animation () {
      var index : int = Time.time/changeInterval;
      index = index % tecture.Length;
      renderer.material.mainTexture = tecture[index];
      if ( index == tecture.Length-1){
          if( isLooping == false){
              isRunning = false;
          }
             renderer.material.mainTexture = tecture[0];
      }
  }
  </code></pre>
  <p>Textures are assigned by drag-and-drop of PNG files in the editor and everything works fine if I limit the PNG count. As my texture count increases, however, I'm getting out of memory errors on Android.</p>
  <p>I would like to load the PNG files as textures at run time to limit the number memory, but I'm having trouble getting the code working.</p>
  <p>I've tried LoadImage.</p>
  <pre><code>var imageTextAsset : TextAsset = Resources.Load(&quot;test.png&quot;);
  var tex = new Texture2D (4, 4);
  tex.LoadImage(imageTextAsset.bytes);
  renderer.material.mainTexture = tex;
  </code></pre>
  <p>I've also tried loading the image directly as a texture (since this seems to work with the editor).</p>
  <pre><code>renderer.material.mainTexture = Resources.LoadAssetAtPath&quot;Assets/Images/test.png&quot;,Texture);
  </code></pre>
  <p>No luck with either. Any suggestions on how to accomplish this?</p>
  <p><strong>EDITED</strong></p>
  <p>I was able to get this working thanks to the link provided by Xerosigma. I had an issue with paths (and I suspect with the Texture requirement on load).</p>
  <p>Outside of loading the PNG textures, I also discovered that Unity does not do memory management on textures. To recover memory, textures need to be manually unloaded after use.</p>
  <p>Textures are all placed in a &quot;Resources&quot; folder. The resources folder can be placed anywhere in the Assets hierarchy - I created several individual folders for each of my PNG sequences to organize them, then added a Resources folder within each to hold the actual PNGs.</p>
  <p>Textures are assigned through the editor by specifying a texture count (ie. 100), a base name (&quot;texture_&quot; for instance), and zero padding.</p>
  <h3>File PNGSequenceRunTime.js</h3>
  <pre><code>#pragma strict
  
  var imageCount : int = 0;
  var imageNameBase : String = &quot;&quot;;
  var imageNameZeroPadding : int = 0;
  
  var changeInterval : float = 0.06;
  var isRunning = false;
  var isLooping = false;
  
  private var texture : Texture = null;
  
  function PlayRocket () {
      isRunning = true;
  }
  
  function Start () {
      var isRunning = false;
      var fileName : String = imageNameBase + ZeroPad(0,imageNameZeroPadding);
      texture = Resources.Load(fileName);
  }
  
  function Update () {
      if( isRunning == true) {
          PNGAnimation ();
      }
  }
  
  function PNGAnimation () {
      var index : int = Time.time/changeInterval;
      index = index % imageCount;
      var fileName : String = imageNameBase + ZeroPad(index,imageNameZeroPadding);
      Resources.UnloadAsset(texture);
      texture = Resources.Load(fileName);
      renderer.material.mainTexture = texture;
  
  
      if ( index == imageCount-1){
          Debug.Log(&quot;End of Animation&quot;);
          Debug.LogWarning(&quot;End of Animation&quot;);
          if( isLooping == false){
              isRunning = false;
          }
          fileName = imageNameBase + ZeroPad(0,imageNameZeroPadding);
          Resources.UnloadAsset(texture);
          texture = Resources.Load(fileName);
          renderer.material.mainTexture = texture;
      }
  }
  
  function ZeroPad(number : int, size : int) {
      var numberString : String = number.ToString();
      while (numberString.Length &lt; size) numberString = &quot;0&quot; + numberString;
      return numberString;
  }
  </code></pre>