Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RazRael · Aug 23, 2017 at 08:41 AM · networkingpause gameonapplicationpause

UNET: how to send network data from OnApplicationPause?

Hello everyone, I'm currently prototyping a mobile game with local multiplayer and I'm using UNET for networking: since the game does not to require Internet access, using UNET with one player acting as host seemed a natural fit.

Playing around with the prototype, one issue I want to address is that the whole networking communication pauses when the game is minimized on the host device (testing on Android); which is understandable, since the OS is pausing the whole Unity process. The simplest way to address this seems to use the OnApplicationPause method to call a "network pause!" ClientRpc on all clients when the host minimizes the game; however, this is not currently working in my build as the ClientRpc is invoked on clients only after the host resumes the game. If I understood the problem correctly, Unity prepares data for the ClientRpc call on the host but the process is paused before Unity can actually send the data to all clients. I also tried setting the NetworkManager.singleton.maxDelay field to 0, but it didn't help.

You can find below the code I added to a NetworkBehaviour:

         float t = 0;
         private void OnApplicationPause(bool pause)
         {
             if (pause && isServer)
             {
                 t = NetworkManager.singleton.maxDelay;
                 NetworkManager.singleton.maxDelay = 0; //this seems useless :-(
                 RpcPause();
             }
         }
 
         [ClientRpc]
         private void RpcPause()
         {
             Time.timeScale = 0;
             Debug.LogError("Game paused!");
         }
 
         private void OnApplicationFocus(bool focus)
         {
             if (focus && isServer)
             {
                 NetworkManager.singleton.maxDelay = t;
                 t = 0;
                 RpcResume();
             }
         }
 
         [ClientRpc]
         private void RpcResume()
         {
             Time.timeScale = 1;
             Debug.LogError("Game resumed!");
         }

I'm currently using HLAPI and a ClientRpc, but any other solution which result in a synchronous data send inside the OnApplicationPause method is fine (network message? call to Transport API?).

Thank you all for your time and help!

Roberto

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · May 26, 2018 at 07:53 PM

this.connectionToServer.FlushChannels();

Had a similar problem with sending message. Adding that line after NetworkConnection.Send() call solved this issue for me. I can't guarantee that this would work on RPC calls.

My first suspicion was that the whole process stops when the screen turns off. Turns out, it doesn't. The unity process on Android runs in the background like a service(still, it's not. It's just a regular old linux process) and exchanges messages while the app is in the background(home key is pressed or screen is off). I captured the packets to come to this conclusion. It seems that the unity process on Android tries to keep the connection, even when it's the app is in the background with no rendering or callbacks like Update() happening.

Not sure, but my best shot is: the main thread does stop, while the network thread doesn't. The sockets used for UNet is handled by a separate thread. I think the connection is "preserved" because the network thread responds to the server's ping messages. RPC calls or Send() calls are not sent because the messages are queued and don't get the chance to be processed by the main thread until the app is back on again.

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 $$anonymous$$ · May 26, 2018 at 08:27 PM 0
Share

Also, there's this log that Unity prints every time the app pauses: I/Unity: Network layer will still work after application suspends

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

94 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Send a Pause RPC on OnApplicationPause 1 Answer

Check if a player has Network View 0 Answers

Raycasts in Multiplayer Network 0 Answers

Who owns NetworkViews placed in the editor? 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