- Home /
What is the fastest way to activate or hide/show many gameobjects?
So I have a GameObject with a lot of children, and I want to hide/show it almost instantly, like a flicker effect. I've toggled the SetActive on the parent GO, and this works, but it's not very fast. There is a clear delay. Just wondering if there were a faster way.
is there a posibility you can mix the childrens into one big object? that will be faster, if not shaders would be the solution.
So there are something like 500 objects, all under a parent GO that I activate and deactivate. When there were 100 or so, it was instantaneous, but now that the number of children have done up there's a noticeable lag.
Not familiar with writing shaders, and not sure how to make one that would ignore a specific GO. Plus, I need the colliders disabled as well, so not sure if the shaders would work.
Try turning the renderer and collider off and on rather than setting the entire object on and off. This might put less hassle on the memory.
Other than that, you may try using different threads for certain group of objects. Prioritze them. Dont try to load them all at same second or same thread.
I think the problem with that is that it'd have to be in a loop of all the children, ins$$anonymous$$d of just the parent object, and I feel like that would take longer.
did you test unifying the gameobjects? you willl save all the iteration over the childs.
Answer by dan_wipf · Feb 25, 2019 at 05:38 PM
As from the comments above To Merge Objects into one you can use this Script (don't know the actual Performance)
Mergin the SubMeshes will increase Performance but will take away the ability to have multiple Materials on the Merged Object if Submerge is false, Materials will be taken over to the new Mesh.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CombineMesh : MonoBehaviour
{
public bool MergeSubMeshes = false;
GameObject[] objToCombine;
GameObject combinedObj;
void Start(){
combinedObj = this.gameObject;
objToCombine = GetComponentsInChildren<GameObject>(true);
CombineAll();
}
void CombineAll()
{
List<CombineInstance> comb = new List<CombineInstance>();
List<Material> mat = new List<Material>();
for (int i = 0; i < objToCombine.Length; i++)
{
GameObject currentObj = objToCombine[i];
//currentObj.SetActive(false);
MeshFilter[] meshFilters = currentObj.GetComponentsInChildren<MeshFilter>(true);
for (int j = 0; j < meshFilters.Length; j++)
{
MeshFilter meshFilter = meshFilters[j];
CombineInstance combine = new CombineInstance();
combine.mesh = meshFilter.sharedMesh;
combine.transform = meshFilter.transform.localToWorldMatrix;
if(!MergeSubMeshes){
mat.Add(meshFilter.GetComponent<Renderer>().sharedMaterial);
}
comb.Add(combine);
}
}
Mesh combinedMehs = new Mesh();
combinedMehs.CombineMeshes(comb.ToArray(), MergeSubMeshes);
combinedObj.GetComponent<MeshFilter>().sharedMesh = combinedMehs;
if(!MergeSubMeshes){
combinedObj.GetComponent<Renderer>().sharedMaterials = mat.ToArray();}
}
}
Yes. I see what you're trying to do here, but I can't lose materials. I appreciate the answer though. I think I can use the export to OBJ, but if not, I may be able to import them into something else and merge them.
wel you keep the materials with the code above, (if bool is false)but i don’t know how performative it is.
Your answer