- Home /
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..
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.
Does anyone know how to stop it from turning "System.IO" into http://System.IO? It's really annoying!
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.
Nope it's not working. It really doesn't like my generics!
Your answer
![](https://koobas.hobune.stream/wayback/20220613000941im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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