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
1
Question by iamoscarmike · Oct 07, 2011 at 02:40 PM · networkrpcparameteronserializenetworkview

Not able to pass an instance of a class over a network through OnSerializeNetworkView or an RPC

Is there a way to pass an instance of a class in the OnSerializeNetworkView function or a RPC. I need to pass a class over a network. But ONSerializeNetworkView and RPC's both only allow booleans, chars, shorts, ints, floats, quaternions, Vector3's, Networkplayers and NetworkID's as parameters.

If this is not possible in any way, does someone know an alternative?

Thanks in advance..

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

Answer by syclamoth · Oct 07, 2011 at 03:25 PM

The way to do this is using .net's binary serialization. It's not as straightforward as it sounds, and it took my several weeks to find this solution!

EDIT: I have put underscores _ in front of all the generics, as well as System.IO to stop UnityAnswers from interpreting them as html tags (grrrrrrrr)

using System; using "System.IO"; // Get rid of the quotes, they're only there to fix auto-linking using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Reflection;

using UnityEngine; using System.Collections;

// This class sends and recieves an object of type 'Info'. You can replace this // with whatever you need to.

public class InfoSender : MonoBehaviour {

 public void SendInfo(Info info)
 {
     byte[] message = SerializeObject<_Info>(info); // should be just Info, not _Info
     networkView.RPC("RecieveInfo", RPCMode.Others, message);
     //Debug.Log(message);
 }

 [RPC] void RecieveInfo(byte[] dataStream)
 {
     MemoryStream stream = new MemoryStream(dataStream);
     stream.Position = 0;
     BinaryFormatter bf = new BinaryFormatter();
     bf.Binder = new VersionFixer();


     Info newInfo = (Info)bf.Deserialize(stream);

     // now you can use the recieved data however you need to!
 }

 byte[] SerializeObject<_T>(_T objectToSerialize)
                 //same as above, but should technically work anyway
 {
     BinaryFormatter bf = new BinaryFormatter();
     MemoryStream memStr = new MemoryStream();

     bf.Serialize(memStr, objectToSerialize);
     memStr.Position = 0;

     //return "";
    return memStr.ToArray();
 }

}

// This class fixes up problems with assembly mismatch between different // kinds of build. For example, it allows you to use it between a standalone // and the editor.

sealed class VersionFixer : SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { Type typeToDeserialize = null;

     // For each assemblyName/typeName that you want to deserialize to
     // a different type, set typeToDeserialize to the desired type.
     String assemVer1 = Assembly.GetExecutingAssembly().FullName;

     if (assemblyName != assemVer1) 
     {
         // To use a type from a different assembly version, 
         // change the version number.
         // To do this, uncomment the following line of code.
          assemblyName = assemVer1;

         // To use a different type from the same assembly, 
         // change the type name.
     }

     // The following line of code returns the type.
     typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));

     return typeToDeserialize;
 }

}

I'm assuming you have enough knowledge of RPC calls to adapt this to your needs.

Comment
Add comment · Show 4 · 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 syclamoth · Oct 07, 2011 at 03:31 PM 0
Share

Does anyone know how to stop it from turning "System.IO" into http://System.IO? It's really annoying!

avatar image Bunny83 · Oct 08, 2011 at 03:31 AM 0
Share

If you mark it as code everything should be fine. To make UA recognise something as code you have to indent the text by 4 spaces or more and make sure you have an empty line before and after your code block. That's what actually happens when you select some text and press the "101 010"-button.

To mark only a small part as code just put it between two accent graves. That should enable you to write something like List<string> inside a sentence.

The block highlighting works also in the comments ;)

 using System.IO;
 using System.Collections.Generic;
 
 public List<string> test;
 public List<List<string>> test2;

A little hint, if you use a numbered or bulleted list make sure you have at least one normal text line between the list and a code block or it won't recognise the code.

avatar image syclamoth · Oct 08, 2011 at 03:42 AM 0
Share

Nope it's not working. It really doesn't like my generics!

avatar image Jojo Batista · Jul 24, 2014 at 11:49 AM 0
Share

THAN$$anonymous$$ 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

RPC , string parameter is sent, but length is 0 1 Answer

When to use OnSerializeNetworkView and RPC's 1 Answer

RPC a lot of lags (Photon) 0 Answers

Unity3d Network - Cant See Connected Players 3 Answers

Really quick/easy question about RPCs 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