I programmed a small game for Android devices where many cubes are created by script. It runs great on the PC, but since mobile devices don’t have that much power, it’s very laggy on my phone.
I already tried meshCombining, but unfortunately the performance is still very bad. (Well it doesen’t even changed much…) This is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class CombineMeshes : MonoBehaviour
{
public void CombineMesh()
{
StartCoroutine("Combine");
}
IEnumerator Combine()
{
yield return new WaitForSeconds(1);
Matrix4x4 myTransform = transform.worldToLocalMatrix;
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = myTransform * meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.SetActive(false);
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.SetActive(true);
}
}
I’ve read, that if a material is transparent, the performance will drop with so many cubes. But just the clouds are having a transparent material.
This is my profiler window:
How can i fix this performance problem?
Thank you in advance!
Transparency problems manifest themselves in a way that is different from what you’re experiencing, don’t worry about it for now.
There is a disconnect between what you posted about trying to combine meshes and the stats in the profiler. 4k+ batches is what you’ll see when you have no mesh combining.
I see you’ve repurposed code from this page: Unity - Scripting API: Mesh.CombineMeshes. Are you sure there aren’t any silly mistakes that cause this script to do nothing? The script relies on objects being children, is your hierarchy the same?
Hey,
I’m sorry I’m answering so late. I had a lot to do during the week. As @BakeMyCake said, I had a very stupid error and did not call the function. So @Peter77 my SetPass Calls went down to 450, but the Verts & Tris are doubled now.
I would profile on the target device to figure out what’s slow there, as device and editor have significantly different performance characteristics. I find the “Hierarchy View” easier to understand, so I would switch the profiler popup from “Timeline” to “Hierarchy”, find the most expensive entries in that list and expand those entries to find what’s causing the cost.
I would check if it’s CPU or GPU bound on the device.
I would use Unity’s Frame Debugger to understand why there are so many SetPass calls, Shadow Caster and Batches.
I would write me a debug menu where I can switch player/quality/rendering/shadow/etc settings at runtime, to see how it affects performance. Then have the profiler running, while changing those settings on the device, to see what makes it run faster or slower.
Investigate why you have 500+ shadow casters, because it’s not a good sign. Looks to me like there is still something incorrect about the way you’ve setup your mesh combining.
Create a new empty scene, remove all light sources, remove all post-processing, disable global illumination and skybox and place your level geometry that is going to be combined. Test the combining script in this scene. Everything will be black because there are no lights, but the stats should have 1 batch and 1 shadow caster. If the stats are different it means something is done wrong. After you’ve achieved a 1 batch and shadowcaster scene, start adding lights and all your other stuff and after each step recheck stats to see how each thing affects rendering.