I want to send a large amount of data to only one player, as it would be wasteful to do this for every player when only one player needs the data. After a bit of research, it appears that TargetRPC is the thing that I want to use. Unfortunately, TargetRPC was only introduced semi-recently in Unity 5.4, and I have not been able to find a guide on how to use it properly.
Here is a barebones version of my attempt so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
public class TargetRpcTest : NetworkBehaviour {
private List finishedConnections = new List();
void Start () {
// We don't want to send data to ourselves.
finishedConnections.AddRange(NetworkServer.connections);
}
void Update () {
// If we're not the server, we don't have any business in sending data to players.
if (!isServer) {
return;
}
// Do the code contained below for everyone we haven't done it for yet.
foreach (NetworkConnection connection in NetworkServer.connections) {
if (!finishedConnections.Contains (connection)) {
TargetSendStuff (connection);
finishedConnections.Add (connection);
}
}
}
[TargetRpc]
public void TargetSendStuff (NetworkConnection connection) {
print ("I've just received a target RPC call!");
}
}
This code is all I could come up with, and it does compile properly. No errors are thrown on the server side, either. However, when a client connects to the server, Unity throws this error:
Could not find target object with netId:5 for RPC call ClientRpc:InvokeRpcTargetSendStuff
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
The 5
in netId:5
varies depending on which object I apply this script to. All objects I attach this script to are required to have NetworkIdentity component because of NetworkBehavior. I have tried applying this script to static scene objects as well as the players themselves, but for some reason UNet still can't seem to find the target objects.
Also, I don't really need the TargetRPC call to apply to any particular object. The code within will do only operations to other objects, and even create new ones based on the sent data. All I need is some way that I can send data to specific clients.
No comments:
Post a Comment