Syncing sprites over network using UNET on Unity 5.3
This is my current code for instantiating floor in game (2D top down game with dungeon like levels)
public void PlaceFloor(Vector2 pos)
{
var onFloor = Physics2D.Linecast(pos, pos, floorLayerMask);
if(!onFloor)
{
GameObject floor = GameObject.Instantiate (floorPrefab, pos, Quaternion.identity) as GameObject;
floor.layer = floorLayer;
floor.transform.position = pos;
floor.transform.parent = transform.FindChild("Floor");
floor.GetComponent<SpriteRenderer>().sprite = floorSprites[0];
FloorArray.Add(floor);
NetworkServer.Spawn(floor);
}
}
I run an autotile script right after all tiles are placed, but it seems that this doesn't really do anything on client end:
public void AutotileFloor()
{
foreach(GameObject floor in FloorArray)
{
Vector2 Up = new Vector2(floor.transform.position.x, floor.transform.position.y + 1);
Vector2 Right = new Vector2(floor.transform.position.x + 1, floor.transform.position.y);
Vector2 Down = new Vector2(floor.transform.position.x, floor.transform.position.y - 1);
Vector2 Left = new Vector2(floor.transform.position.x - 1, floor.transform.position.y);
var U = Physics2D.Linecast(Up, Up, floorLayerMask);
var R = Physics2D.Linecast(Right, Right, floorLayerMask);
var D = Physics2D.Linecast(Down, Down, floorLayerMask);
var L = Physics2D.Linecast(Left, Left, floorLayerMask);
var tileID = 0;
if(U)
{
tileID += 1;
}
if(R)
{
tileID += 2;
}
if(D)
{
tileID += 4;
}
if(L)
{
tileID += 8;
}
floor.GetComponent<SpriteRenderer>().sprite = floorSprites[tileID];
}
}
floorSprites are loaded from resources where I put all my tilesets... so since this won't work on it's own, are there any functions specifically for sprites or something like that? Currently if I connect with unity editor I can select all invisible floors placed and level looks exactly the same like on host, so it's working, but I don't know how to sync sprites properly, does anyone know if anyone had similar problem and it was already solved somewhere else (if it was then sorry for not googling long enough :( ) or could just help me with this? I am not asking for code, I am just lost on what I should do to sync all tiles (every type of wall/floor has 16 variations, every type is in individual image if that matters, all placed in resources folder then I load it onAwake with floorSprites = Resources.LoadAll(floorTileset);)
Basically my question is how, if possible and it doesn't use too much bandwitdh, I could sync spriterenderer or even the whole object to make it simpler? (not saying simpler is always better) If there are several ways, I'd like to know the best one, not the easiest one, thank you for your help in advance.
Your answer
Follow this Question
Related Questions
How to Update Objects Sprite to the Network 1 Answer
How do I sync Network-Server objects to clients? / Is my solution okay? 0 Answers
How do i get the two client's units to match up rather than be incorrect units? 1 Answer
[uNET] Instantiated gameobjects not syncing for new clients 1 Answer
Setting function to run on all clients but only work on the player that called it 0 Answers