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
8
Question by jc_lvngstn · Mar 25, 2011 at 10:02 PM · networkrpcdatasend

Sending bulk data via rpc

I'd like to send large chunks of bytes via RPC to my clients. This could be as much as 256k at a time. It's not constant, just at certain times.

It doesn't look like RPC has the ability to just send a byte[] array of values, so I've looked at converting bytes into a string.

Is there a better way to send this information to the clients, from my server? A key qualifier of "better" to me is ease of use. The RPC feature in Unity looks very easy to use, so I don't mind the string->byte->string conversion if it means avoiding a much more complicated way of sending the data.

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 Bunny83 · Mar 26, 2011 at 12:29 AM 0
Share

I had that problem too. I've also used the serializing approach. If there's a better way that would be nice. In OnSerializeNetworkView you can send a custom mix of data but it would be nice to do that with RPCs. At least a byte array as parameter would be nice. I guess as string each letter is still serialized as a 16bit character, so that wouldn't be nice to reduce the packet size. For one-shot transfers i think serialize to string is the easiest way at the moment.

1 Reply

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by Julien-Lynge · Dec 09, 2011 at 09:56 PM

This is an old question, but it is absolutely possible to send byte arrays with RPC - it's just not a documented feature. I am currently using it to transmit images between networked Unity instances (after encoding to jpg or png), and it has worked fine for 10k up to 300k transfers, though note that RPC seems to buffer the calls, meaning that if you're sending data faster than the pipe can handle or the receiving function can process you'll get farther and farther behind as time goes on - you may want to include a timestamp header on your data transfers and throw away RPC calls when they get too stale.

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 DaveA · Dec 09, 2011 at 10:24 PM 0
Share

Undocumented - so, would you $$anonymous$$d documenting how you did it here?

avatar image jc_lvngstn · Dec 11, 2011 at 01:44 PM 0
Share

Yes, a small example would be very helpful :)

avatar image Julien-Lynge · Dec 12, 2011 at 06:23 PM 1
Share

I can certainly provide a quick example. On the sending end:

 byte[] bytesToSend = preTransferTexture.EncodeToPNG();
 if (bytesToSend.Length > 0)
     networkView.RPC("ReceiveWebcamPNG", RPC$$anonymous$$ode.Others, bytesToSend);
 else
     Debug.LogError("Bad length of bytes to send.");

and on the receiving end:

 [RPC]
 public void ReceiveWebcamPNG(byte[] bytes)
 {
     if (bytes.Length < 1)
     {
         Debug.LogError("Received bad byte count from network.");
         return;
     }
     
     ((Texture2D)renderer.shared$$anonymous$$aterial.mainTexture).LoadImage(bytes);
 }

Some notes: On the sending end, if you are assigning color values to the preTransferTexture (say, pulling from a webcam), you don't have to 'Apply()' before creating the PNG byte array. On the receiving end, I include a sanity check for byte length; you could go a step further and include a checksum or expected length as a header, though in my testing on a few different machines I've never seen a case where data sent this way was corrupted - Unity may wrap such checks directly into the RPC calls, I don't know.

Final note: we started out sending PNGs and soon moved to JPG compression for speed reasons. There are plenty of libraries out there for encoding.

avatar image jpthek9 · Jan 07, 2015 at 11:38 PM 0
Share

This answer is a life saver! When will Photon reference this or write their own?

avatar image Mexallon · Jan 07, 2015 at 11:47 PM 0
Share

I know this question is already answered and quite old but I just want to add that Unity "lately" added JPG encoding to Unity so there is no longer a need for an external library. cheers.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Really quick/easy question about RPCs 1 Answer

Send Texture Over Network 1 Answer

Network Question. Sending value from iPhone Client to Mac Server 1 Answer

Games with multiple maps 1 Answer

client to client ping - NAT punchthrough 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