- Home /
Destroying and recreating renderers/shaders at runtime
Hi, I am having trouble with changing Anti Aliasing at run time and I would like to find a work around, I believe it has something to do with not destroying and recreating renderers at run time. I have managed to destroy the renderers at run time but recreating them is proving difficult at the moment.
Has anyone had experience or has an idea how I would be able to do this?
renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
foreach (Renderer renderer in renderers)
{
if (renderer.sharedMaterial.mainTexture != null)
{
//Save Textures
//textureForMaterial[renderer.sharedMaterial] = renderer.sharedMaterial.mainTexture;
//Remove Textures
//renderer.sharedMaterial.mainTexture = null;
Renderer.Destroy(renderer);
//print(renderer.sharedMaterial.shader);
//Shader.DestroyImmediate(renderer.sharedMaterial.shader, true);
}
}
What does AA have to do with destroying and creating renderers at runtime???
Also you should avoid using Instantiate. It's probably the most expensive function there is, so if you don't want something to render, simply disable the renderer :)
What? Unless I'm misinformed when you change AA you destroy your glContext and create a new one with the parameters of AA. Renderers being used in that context are stored. When you create a new context you have to reapply the renderers to the objects. On a mobile device there isn't a lot of RA$$anonymous$$ so whereas on PC you can destroy the context and create a new one then reassign the renderers and textures, mobile devices will free the renderers from memory before creating the new context then struggle to reassign the renderers and therefore textures.
Hope that makes sense, I assume you're not familiar with mobile graphics hardware and stuff
You are correct, but I've never heard of anyone who destroyed and recreated all the mesh renderers. Are you sure Unity doesn't do that for you? It would honestly be quite sad if it didn't.
Something goes wrong when AA is changed so either it's unity (this will fix the problem) or it's the GPU (The company I work at can work on fixing it)
I switch AA on and off using QualitySettings at run time.
Answer by Skydive · Sep 15, 2013 at 03:03 AM
Assuming you want to enable/disable it. Open up the AntiAliasaing.cs or js file and look for OnRenderImage(...) From at the top of the method simply add.
if(PlayerPrefs.GetInt("AAEnabled") == 0)
{
Graphics.Blit (source, destination);
return;
}
I hope this information helps you. ;) Worth a shot I guess.
I'll have to look up what this all does but thanks for the suggestion I'll let you know how it goes :)
Your answer
Follow this Question
Related Questions
Building a Unity 5 Standard Material at runtime 3 Answers
Unity 5.4 - SetVectorArray - how to read values back from MaterialPropertyBlock? 0 Answers
How to tell if a texture was generated at runtime? 1 Answer
How to find and assign Default-Particle material to a renderer 2 Answers
Is it better to destroy and instantiate or turn off mesh renderer? 1 Answer