Performance: Materials and textures
This affects how fast the game loads and renders objects with visual detail.
Jump into concepts and practice - no test required
Material mat = new Material(Shader.Find("Standard")); Texture2D tex = Resources.Load<Texture2D>("OptimizedTexture"); tex.filterMode = FilterMode.Bilinear; mat.mainTexture = tex; renderer.material = mat;
Material mat = new Material(Shader.Find("Standard")); mat.mainTexture = Resources.Load<Texture2D>("HighResTexture"); renderer.material = mat;
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| High-res uncompressed textures | N/A | N/A | High GPU load and slow texture upload | [X] Bad |
| Compressed textures with mipmaps | N/A | N/A | Lower GPU load and faster rendering | [OK] Good |
Material in Unity primarily control?GetComponent<Renderer>().material property of the Renderer component.Renderer rend = gameObject.GetComponent<Renderer>(); Texture2D tex = new Texture2D(128, 128); rend.material.mainTexture = tex; Debug.Log(rend.material.mainTexture.width);
mainTexture.width returns 128.Renderer rend = GetComponent<Renderer>(); Texture2D tex; rend.material.mainTexture = tex;
tex is declared but never assigned a texture object, so it is null.mainTexture will remove the texture, likely not intended and may cause issues.