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?
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!
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With