Send a mesh via Network

I am trying to create a 3D Tron Game and i used the Trail Script from the unity examples and added a mesh collider to it, so I can collider with the Trail which works pretty good. Now I need to sync the mesh via network and I just find a solution to it. Since i cant send arrays via an RPC I would have to convert everything into a string, but then i get massive lags because of the huge amount of data… any ideas?

This would send a basic mesh through. You could expand if for materials etc. It’s going to be big though, just not as big as your string.

using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;

public class SendMeshBehaviour : MonoBehaviour {
	
	public static event Action<string, Mesh> GotNewMesh = delegate {};
	
	[RPC]
	void GetMesh(byte[] data)
	{
		var fmt = new BinaryFormatter();
		var mem = new MemoryStream(data);
		var md = fmt.Deserialize(mem) as MeshData;
		var mesh = new Mesh { 
			vertices = md.vertices,
			normals = md.normals,
			triangles = md.triangles,
			uv1 = md.uv1,
			uv2 = md.uv2
		
		};
		mesh.RecalculateBounds();
		GotNewMesh(md.name, mesh);
	}
	
	public class MeshData
	{
		public string name;
		public int[] triangles;
		public Vector3[] vertices;
		public Vector2[] uv1;
		public Vector2[] uv2;
		public Vector3[] normals;
	}
	
	public void SendMesh(string name, Mesh mesh)
	{
		var mem = new MemoryStream();
		var fmt = new BinaryFormatter();
		fmt.Serialize(mem, new MeshData { triangles = mesh.triangles, 
			normals = mesh.normals, 
			vertices = mesh.vertices,
			uv1 = mesh.uv1,
			uv2 = mesh.uv2,
			name = name
		});
		networkView.RPC("GetMesh", RPCMode.Others, mem.GetBuffer());
	}
	
}

Just FWIW,

on this actual question about the trail-tail…

In a recent project when I literally send a mesh between two devices, I don’t - instead, I send the thing that makes the mesh! (This concept would vary depending on what you’re doing.)

In your case:

the trail (and hence the mesh) must be made in some way by some code that takes some starting input - for example, a series of positions of the object? Or perhaps a “velocity and position” …?? Right?

In fact, just send that “series of positions” or “velocity and position” and run THE SAME CODE on the far side. Cross fingers, and it should produce the SAME RESULT.

The very best part about all this is not the huge efficiency, etc. The best part is that if it works you feel REALLY smug! :slight_smile: