- Home /
How to tell if a texture was generated at runtime?
I have a scene where renderers have materials & textures.
Then, over multiple iterations, textures are generated at runtime and set as the material's texture.
It seems unity likes to hold on to these for an indeterminate amount of time, and I am trying to save memory.
The following code:
if(renderer.material.mainTexture != null)
UnityEngine.Object.Destroy(renderer.material.mainTexture);
renderer.material.mainTexture = newTexture;
Works well on destroying the textures that were generated at runtime. But when it tries to destroy the original texture, I get the error message "Destroying assets is not permitted to avoid data loss."
Is there any way to tell if a texture was generated at runtime, or if it is an asset that was built-in to the build?
How about a simple manager class that maintains a list of runtime-generated textures?
Answer by badadam · Jan 14, 2019 at 10:32 AM
Create empty gameobject and than create two different objects for same shape. Add different materials to each one. Add these objects which have different Material but same shape to empty game object as a child. Set only one of the active. And add the script below to parent emptygame object.
public GameObject sameShapewithMaterial1;
public GameObject sameShapewithMaterial2;
public void changeShape()
{
sameShapewithMaterial1.SetActive(false);
sameShapewithMaterial2.SetActive(true);
}
NOTE : ALL OBJECTS WHICH HAVE SAME SHAPE BUT DIFFERENT MATERIAL HAVE SAME POSITION AND ROTATION VALUES.