Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by raja-bala · Jul 20, 2015 at 11:36 PM · networkingserverclientsynchronizationunity multiplayer

Sending texture updates from client to server

I'm working on a multiplayer sample wherein player's can see each other while playing. Currently, I just want to send the updated webcam video as png images over the network (will worry about compression/encoding later). I'm unsure how to accomplish this.

Initially, I thought I'd have a component attached to my player prefab, say PlayerVideo that overrides the OnSerialize and OnDeserialize methods -- however, these functions seem to be for the server to send specific updates to the clients (i.e., your own SyncVar functionality)

It seemed like Commands and ClientRPC calls are what I needed. The flow being:

PlayerVideo.Update() --> GetWebCamFeed() --> SendToServer --> CmdVideoTextureUpdate --> RpcUpdateVideoTexture

How do I send these texture data to the server and from the server to the clients though? I don't understand how exactly to use NetworkWriter and NetworkReader to accomplish this.

Here's some code:

 void Update()
     {
         // Send server the updated video texture data as a NetworkWriter object
         if (isLocalPlayer)
         {
             byte[] textureData = m_playerVideoTexture.EncodeToPNG();
             NetworkWriter writer = new NetworkWriter();
             writer.WriteBytesFull(textureData);
 
             CmdVideoTextureUpdate(writer);
         }
     }
 
     [Command]
     void CmdVideoTextureUpdate(NetworkWriter writer)
     {
         // argument should be NetworkReader, instead it's writer.. how to fix?
         RpcUpdateVideoTexture(textureData); 
     }
 
     [ClientRpc]
     void RpcUpdateVideoTexture(NetworkReader reader)
     {
         if (isLocalPlayer)
             return;
 
         byte[] textureData = reader.ReadBytesAndSize();
         m_playerVideoTexture.LoadImage(textureData);
         m_playerVideoTexture.Apply();
     }


How do I convert the NetworkWriter object to a NetworkReader object for the clientrpc to use? Or is this just the wrong way to do this in the first place?

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
1

Answer by seanr · Jul 22, 2015 at 05:20 PM

you could use NetworkWriter.ToArray() and send the array of bytes, then construct a NetworkReader from the array of bytes.

Comment
Add comment · Show 7 · 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 raja-bala · Jul 23, 2015 at 07:58 AM 0
Share

Thanks for helping out Sean! I didn't quite get what you meant. Can a command/clientrpc have a byte[] argument (and Unity takes care of serializing it and sending it down as a message)? i.e.

 [Command]
 void CmdVideoTextureUpdate(byte[] data) {
      RpcUpdateVideoTexture(data); 
 }
 
 [ClientRpc]
 void RpcUpdateVideoTexture(byte[] data)
 {
     if (isLocalPlayer)
        return;
     m_playerVideoTexture.LoadImage(data);
     m_playerVideoTexture.Apply();
 }

If I send a network message with the data, I'd have to route the data to the right network object. Is that correct?

avatar image seanr · Jul 23, 2015 at 12:45 PM 0
Share

yes a command can take an array of bytes

avatar image meat5000 ♦ · Jul 23, 2015 at 01:11 PM 0
Share

But it doesn't like transforms. Just worth noting.

avatar image raja-bala · Jul 23, 2015 at 04:33 PM 0
Share

Thanks @seanr, so I tried it and it seems like the PNG encoded texture byte data (haven't added video encoding yet) is too large.

 UnityException: NetworkWriter used buffer is too big!
 UnityEngine.Networking.NetworkServer.SendWriterToReady (UnityEngine.GameObject contextObj, UnityEngine.Networking.NetworkWriter writer, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:333)

Is there something I can do to work around it? Or would message passing be the alternative (with messages, it seems like i'd have to have a global client message handler and have to do a per-player-texture-data-sync manually)

avatar image seanr · Jul 23, 2015 at 04:36 PM 2
Share

there is a limit of (about) 1400 bytes on regular messages. If you use a fragmented channel, the limit is 32$$anonymous$$. You would add a fragmented channel to the QoS channels in the Network$$anonymous$$anager settings, then use [Command(channel=3)] where 3 is the new channel index.

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

24 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

Related Questions

Networking physics with HLAPI 1 Answer

Unity networking tutorial? 6 Answers

Need help getting "Server Reconciliation" to work. 0 Answers

How to watch in client applications what it is happening in the server one? 0 Answers

Creating a multiplayer game. 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