- Home /
Sendmessage performance
Hi Guys,
I'm relatively new to Unity development. I've got a bit of experience with c# (mostly visual c# / windows programs) and have been trying to write my own RTS game, mostly as a learning experience.
I'm currently just trying to build standard RTS controls. One of which is selecting all units of a certain type on screen when you hold the ctrl button. So, this is from my Update section:
if(Input.GetKey ("left ctrl"))
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes (camera);
foreach (GameObject g in _allunits)
{
if(GeometryUtility.TestPlanesAABB(planes, g.collider.bounds))
{
g.GetComponent ("unit").SendMessage ("UnitSelected",true);
_selectedunits.Add (g);
}
}
}
Now, I only have 133 units in _allunits (all the same type, so I'm not checking that in the code yet) and while the above works, when I zoom out a bit to see all the units and try it, there's almost a second delay before anything happens. During this time, any animation / camera movement freezes.
I initially thought that looping through _allunits was causing the problem, but it appears to be the send message / getcomponent.
Can anyone suggest something that may help me out here? I've tried:
(unit)g.getcomponent("unit").UnitSelected(true);
and a couple of variations, but they all seem to cause the same amount of delay.
Cheers Ross
Note my long discussion with Whydoidoit and his research on the matter here:
http://answers.unity3d.com/questions/324893/c-invoke-vs-unity-sendmessage.html
Be sure to vote-up Whydoidoit's answer !
Answer by Julien-Lynge · Mar 10, 2013 at 06:57 PM
Are you absolutely sure the lag isn't from doing hundreds of debug messages in a single frame? Debug is very often the cause of significant lag like you describe. Doing the I/O to write to the editor log file does take a relatively significant amount of time.
Answer by pazzesco · Mar 10, 2013 at 05:37 AM
I don't know if this will boost performance, but you could try:
(unit)g.GetComponent<unit>().UnitSelected(true);
If that doesn't work, I'd look through any performance snags in the UnitSelected() function itself. This may require digging.
You could just make UnitSelected do nothing. If performance goes up by a lot, then it's the code in UnitSelected that is slow.
void UnitSelected(bool value)
{
return;
// Rest of your old code
}
That doesn't work unfortunately (doesn't fix the lag). For some reason, I didn't think to look inside UnitSelected:
public void UnitSelected(bool _value)
{
_selected = _value;
_selectedindicator.renderer.enabled = _value;
Debug.Log (this.name + " Changed Selected State " + _value.ToString ());
}
I tried omitting.renderer.enabled, but this did not improve performance at all. Would handling this via events smooth out my selection?
Cheers
Ok, so we've got the UnitSelected() function itself ruled out. A shot in the dark, but maybe the GeometryUtility.TestPlanesAABB(planes, g.collider.bounds) is bogging down? If that isn't the case, then the problem could lie within Unity zoo$$anonymous$$g out and changing perspective to fit more on the screen.
It seems to be changing the _selected bool, from my testing.
I'm going to try and put some kind of unit manager in place to act as a proxy for messages, see if some events make it run a bit smoother.
Thank you Julien - it was indeed the debug logging!
Submit that as the answer and I'll mark it up!
Answer by Tarlius · Mar 10, 2013 at 06:46 PM
Why are you storing an array of GameObject if you need to GetComponent every frame? You'll probably get a pretty massive speed boost if you maintain an array of the component rather than the GameObjects.
Also, SendMessage is pretty slow, and by doing the GetComponent you've already thrown away the "usefulness" of SendMessage anyway. Just call the function directly. I've also heard the Generic version is faster (no string overheads).
UnitType unitType = unitGameObject.GetComponent<UnitType>();
Also, did you consider adding some code to make it only run once? (I assume its doing it every frame you have ctrl down, since there seems to be no code to implement "fire-once")
I'm using the array of GameObject as I was originally just using a collection of the component 'unit', but I need the collider information for GeometryUtility.TestPlanesAABB.
This is just me learning - I imagine I'll go back to the unit scripts to keep track of things, and find the parent in the event that I need to do location testing and so on.
Also, the code above is within a if(input.mousebuttondown(0)) block
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
'sendMessage' is not a member of 'unityengine.transform' 1 Answer
pickup weapon with GetComponent 0 Answers
Tetris style math game problem... 2 Answers
Multiple Enemies Help 1 Answer