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 CommitteeWind · Apr 07, 2012 at 01:09 AM · multiplayernetworkserializeonserializenetworkview

Data Synchronization using OnSerializeNetworkView

Unity suggests:

 int health = 0;
 if (stream.isWriting)
 {
     health = Health;
     stream.Serialize(ref health);
 }
 else
 {
     stream.Serialize(ref health);
     Health = health;

 }

Works the same way (so far):

 stream.Serialize(ref Health);

So which is better? Does it matter if I am writing or reading?

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
Best Answer Wiki

Answer by Graphicated · Apr 07, 2012 at 07:02 AM

sure it matters, and it depend on what you are willing to do...

when you go deeply in Networking, and face different situations, you will be thanksfull to have such methods,, for example, what if only the owner is allowed to execute on server ? .. is anyone allowed to move things in the game as they like ?

what kind of functions should reach all players ? and which should be executed only on a specific players machine?

so in this case, when you ask if(stream.isWriting) .. you are asking unity to check if the newtworkview owner is writing now so it can send data ,otherwise the player will only be able to recieve data.. now here, each player is owner of his charachter lets say and has his own networkview.

well this will not help you for a long time, because if each user has his own Networkview this might gets you in trouble later..

and this way you can limit the movement or execution of movment to the server owner only ! here is an example about movement :

 function Update(){
     
     if(Network.isServer){
         //Only the server can move the cube!            
         var moveDirection : Vector3 = new Vector3(-1*Input.GetAxis("Vertical"), 0,Input.GetAxis("Horizontal"));
         var speed : float = 5;
         transform.Translate(speed * moveDirection * Time.deltaTime);
     }
     
 }
 
 
 function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
 {
     if (stream.isWriting){
         //Executed on the owner of the networkview; in this case the Server
         //The server sends it's position over the network
         
         var pos : Vector3 = transform.position;        
         stream.Serialize(pos);//"Encode" it, and send it
         
         
         
     }else{
         //Executed on the others; in this case the Clients
         //The clients receive a position and set the object to it
         
         var posReceive : Vector3 = Vector3.zero;
         stream.Serialize(posReceive); //"Decode" it and receive it
         transform.position = posReceive;
         
        }







you better concider the Authorative way ,, where players sends data to server, so server then decides what to do and sends data depending on that. and not to send direct data from player to player.. take a look at "RPC calls" in unity also.

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 CommitteeWind · Apr 07, 2012 at 03:50 PM 0
Share

This makes a lot more sense now. Thank you.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

texture in networked game 2 Answers

OnSerializeNetworkView problems with clients 1 Answer

How to send a gameobject in the OnSerializeNetworkView() 0 Answers

Can you tell OnSerializeNetworkView who the owner is? 3 Answers

Serialize is working in debug, but it has no effect 0 Answers


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