Hi,
I just tested something and it seems that RPCs in Unity don’t support inheritance. Is that correct?
class Unit : MonoBehaviour
{
[RPC]
public void Test () {}
}
class Melee : Unit
{
}
If I have a unit with Melee script on it, RPC won’t be received as RPC function “Test” cannot be found.
class Unit : MonoBehaviour
{
}
class Melee : Unit
{
[RPC]
public void Test () {}
}
On the contrary, this works and Test will be executed.
This introduces a set of new problems for us, can you please confirm that this is Unity’s standard behaviour? I hope we are doing something wrong…
We are using Photon but that should behave the same way as the integrated networking as far as I know.
EDIT:
Photon (PUN) class for the solution below:
1, in case the solution scripts are in the Plugin folder, move them out to your script folder
2, InheritableRPC.cs, replace the second class with this:
public static class InheritableRPCExtensions
{
public class StoredPlayer
{
public string ipAddress, guid;
public int port;
}
public static void RPCEx(this PhotonView view, string routineName, PhotonTargets targets, params object[] parameters)
{
using(var m = new MemoryStream())
{
var b = new BinaryFormatter();
b.Serialize(m, parameters);
m.Flush();
var s = Convert.ToBase64String(m.GetBuffer());
view.RPC("PerformRPCCall", targets, routineName, s);
}
}
public static void RPCEx(this PhotonView view, string routineName, PhotonPlayer player, params object[] parameters)
{
using(var m = new MemoryStream())
{
var b = new BinaryFormatter();
b.Serialize(m, parameters);
m.Flush();
var s = Convert.ToBase64String(m.GetBuffer());
view.RPC("PerformRPCCall", player, routineName, s);
}
}
}
3, example code. SampleCaller.cs class:
public class SimpleCaller : MonoBehaviour {
public PhotonView otherView;
void Start ()
{
Network.InitializeServer(200,8081,true);
}
void OnGUI()
{
if(GUILayout.Button("Call Print"))
{
otherView.RPCEx("PrintThis", PhotonTargets.All, "Hello World");
}
}
}