Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preload textures in unity3d

Tags:

unity3d

I am modifying a material, that is applied to an object, by changing its texture but the problem is that i get some lag while doing this.

How can i preload some textures in memory to avoid temporal lag in unity3d?

like image 955
Alex Avatar asked Jan 08 '13 13:01

Alex


People also ask

How do you add Textures in Unity 3D?

Just like all other assets, adding textures to a Unity project is easy. Start by creating a folder for your textures; a good name would be Textures. Then drag any textures you want in your project into the Textures folder you just created. That's it!

Where are Textures stored in Unity?

They really can be anywhere inside the assets folder. Unity can access them wherever they are as long they are inside that folder. I usually organize them in a "Textures" folder directly inside the project panel (you can make a folder in unity by going to the project panel and: right click -> Create -> Folder).


1 Answers

Are you loading the texture right before you add it? How is the new texture stored? Typically, pre-caching is done during the Awake() or Start() loops.

Renderer objectRenderer;
public Texture2D replacementTextureAsset;
Texture2D runtimeTextureReplacement;

void Awake()
{
  objectRenderer = (Renderer)GetComponent(typeof(Renderer));

  // Either have the texture assigned via the inspector (replacementTextureAsset above)
  //  or else get a reference to it at runtime. Either way, the texture will be loaded into memory by the end of this loop.
  runtimeTextureReplacement = Resources.Load("myReplacementAsset");
}

void TextureReplace()  // Called whenever
{
  objectRenderer.material.texture = replacementTextureAsset;

  OR

  objectRenderer.material.texture = runtimeTextureReplacement;
}

If this is what you're already doing and you still have "lag", then you're probably experience the inevitable consequence of sending the texture to the frame buffer on the graphics card. In that case, there's nothing you can do except use a smaller texture.

like image 191
cjcurrie Avatar answered Dec 19 '22 07:12

cjcurrie