- Home /
Fastest way to instantiate and move multiple game objects in the editor
Hello everybody :)
I have a script editor that instantiate a large number of small objects in my scene, around 1000 units.
This takes a lot of time to complete (around 15 sedonds) which is not really a problem for the creation.
However this editor script allows me also to move (translation + rotation) all these objects at once and this operation takes almost the same time. This is quite annoying because I have to move these objects quite often while editing my scene.
Is there some way to improve these instantiation and moving actions?
Here is a snippet of my code:
List<GameObject> m_CurveObjects = new List<GameObject>();
// Draw abject along a curve while (position < curve.Length) { curveGameObject = (GameObject) Instantiate(CurveObject); curveGameObject.isStatic = true; m_CurveObjects.Add(curveGameOject); }
curveGameObject.transform.localPosition = newposition;
trackGameObject.transform.rotation = new Quaternion();
trackGameObject.transform.Rotate(myangle1, myAngle2, myAngle3);
position += SpaceBetweenObjects;
}
position -= segment.Length;
}
When I only move the objects, I do the same things, but instead of instantiating I go through the list where I stored my objects.
Answer by Statement · Mar 29, 2011 at 03:30 PM
Are you sure you really need to instantiate 1000 GameObjects for that purpose?
It seems to me that you are making some sort of curve from several dummy objects (transforms) which would only slow your game down. It might be easier to just create a list of CurvePoints that isn't derived from MonoBehaviour and work on that set of objects instead. If you still want to be able to manipulate them through the editor, you could create an editor script that do this for you. Of course, I don't fully know what you intend to do with these GameObjects you're creating, but I thought it might still be worth mentioning. If you want to be able to store the list you need to add System.Serializable attribute to the class.
Thank you very much for your answer! Actually I tried to place many instances of objects along a road (e.g. signs, plants, lights, ...) I know the curve that follow this road so the code above simply walks along this road and places objects one by one by instantiating them. I must admit that I don't really understand the list of CurvePoints that you've mentioned above.
Well ins$$anonymous$$d of working with $$anonymous$$onoBehaviors you'd just work with naked classes. They are light weight and doesn't "exist in the scene", but they exist in your script. It would be too much of a lengthy discussion about this. An example class is found here: http://unity3d.com/support/documentation/ScriptReference/Serializable.html
Sorry, I still dont understand your comment and the relevance of the link. $$anonymous$$y curveObjects are GameObjects, not $$anonymous$$onoBehaviour. But maybe you're saying that GameObjects are too heavy and that I should work with lower level objects or maybe giving a list of location to a shader? (I also wanted to precise that I'm very used to C# and to the UNity's serialization->maybe this could help you to see what I don't understand)
Answer by by0log1c · Mar 29, 2011 at 03:23 PM
Try adding a:
yield;
anywhere in the while(){} block. It should allow for a frame to go between each manipulation, making the total process longer, but restoring control ( aka: not freezing).
I think it could help.
Thank you for your quick answer! Actually I'm not annoyed by the freeze of the editor but by the actual time of the operation.
Answer by loginpawan · Aug 16, 2012 at 02:11 PM
hello how can i control the animation applied on multiple gameobjects i used the script===== public var timer : float =0.0; var apple : GameObject; function Update(){ timer += Time.deltaTime; if(animation.IsPlaying("bounce")) { Debug.Log("Mellon is bouncing"+timer);
// apple.animation.Play("AppleBounce"); apple.animation.Stop("AppleBounce"); } // animation.Stop("AppleBounce"); if(timer>=3 && timer<6) { animation.Stop("bounce"); // Debug.Log("Mellon is bouncing"); apples(apple,"AppleBounce"); Debug.Log("function return in calling fn"+timer); // animation.Play("MelonBounce"); //timer=0.0;
} if(timer>=9 && timer<=12) { Debug.Log("comes in timer"+timer); apple.animation.Play("AppleBounce"); animation.Play("bounce"); Debug.Log("both are playing"+timer);
} }
function apples(apple:GameObject,animName:String) { apple.transform.animation.Play("AppleBounce"); Debug.Log("Apple is bouncing"+timer); if(timer>6 && timer< 9) { animation.Stop("AppleBounce"); //multiObject() } return; }
Your answer
Follow this Question
Related Questions
Dynamically update the editor? 3 Answers
Prefabs are instantiated in editor but not in executable 2 Answers
Ask for Script Compilation from EditorWindow Button 1 Answer
event sequencing 1 Answer