Loading a PNG image as a texture at runtime in Unity3d -


i'm using series of png images sprites create animations on plane augmented reality application in unity. png images loaded textures, , textures applied plane create animation sequence tracks augmented reality target.

here script attached plane , controls animation sequence.

file pngsequence.js

#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];     } } 

textures assigned drag-and-drop of png files in editor , works fine if limit png count. texture count increases, however, i'm getting out of memory errors on android.

i load png files textures @ run time limit number memory, i'm having trouble getting code working.

i've tried loadimage.

var imagetextasset : textasset = resources.load("test.png"); var tex = new texture2d (4, 4); tex.loadimage(imagetextasset.bytes); renderer.material.maintexture = tex; 

i've tried loading image directly texture (since seems work editor).

renderer.material.maintexture = resources.loadassetatpath"assets/images/test.png",texture); 

no luck either. suggestions on how accomplish this?

edited

i able working link provided xerosigma. had issue paths (and suspect texture requirement on load).

outside of loading png textures, discovered unity not memory management on textures. recover memory, textures need manually unloaded after use.

textures placed in "resources" folder. resources folder can placed anywhere in assets hierarchy - created several individual folders each of png sequences organize them, added resources folder within each hold actual pngs.

textures assigned through editor specifying texture count (ie. 100), base name ("texture_" instance), , 0 padding.

file pngsequenceruntime.js

#pragma strict  var imagecount : int = 0; var imagenamebase : string = ""; 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("end of animation");         debug.logwarning("end of animation");         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 < size) numberstring = "0" + numberstring;     return numberstring; } 

in c#:

// load textures array object[] textures = resources.loadall("textures", typeof(texture2d));  // load single texture texture2d texture = resources.load("texture") texture2d;  renderer.material.maintexture = texture; 

hope helps. should able convert js no problem. @ unity documentation.

http://docs.unity3d.com/documentation/scriptreference/resources.html


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -