Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by phanteh · Mar 10, 2013 at 05:23 AM · performancesendmessage

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Mar 10, 2013 at 07:44 AM 0
Share

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 !

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 10, 2013 at 05:46 AM 0
Share

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
 }
avatar image phanteh · Mar 10, 2013 at 06:52 AM 0
Share

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

avatar image pazzesco · Mar 10, 2013 at 05:04 PM 0
Share

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.

avatar image phanteh · Mar 10, 2013 at 05:49 PM 0
Share

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.

avatar image phanteh · Mar 10, 2013 at 10:41 PM 0
Share

Thank you Julien - it was indeed the debug logging!

Submit that as the answer and I'll mark it up!

Show more comments
avatar image
0

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")

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phanteh · Mar 10, 2013 at 10:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges