我有一段代码应该生成一个随机瓦片地图,它只为服务器执行。我不知道如何让它在玩家加入时同步到玩家。我将如何同步加入玩家的预制件?它们加载到一个玩家(服务器)上,但是当另一个玩家加入时,它们不会出现。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GenerateLevel : NetworkBehaviour {
public int MapSize = 20;
public GameObject prefab;
public GameObject _LeftW;
public GameObject _RightW;
public GameObject _TopW;
public GameObject _Botw;
public GameObject SpawnLocRed;
public GameObject SpawnLocBlue;
public string map;
public override void OnStartServer()
{
//base.OnStartServer();
GetComponent<Renderer>().material.color = Color.blue;
}
void Start()
{
if (!isServer)
{
return;
}
for (int c = MapSize / 2; c > -MapSize / 2; c--)
{
for (int r = -MapSize / 2; r < MapSize / 2; r++)
{
var t = Instantiate(prefab, new Vector3(r, 0, c),
Quaternion.identity);
NetworkServer.Spawn(t);
Debug.Log("Col:" + c + " Row:" + r);
if (Random.Range(0, 3) == 2)
{
t.GetComponent<RAISE>().Run();
map += 1;
}
else
{
map += 0;
if (c < 0 - MapSize / 4 && r > 0 + MapSize / 4)
{
var red= Instantiate(SpawnLocRed, new Vector3(r, 2, c),
Quaternion.identity);
}
if (c > 0 + MapSize / 4 && r < 0 - MapSize / 4)
{
var blue= Instantiate(SpawnLocBlue, new Vector3(r, 2, c),
Quaternion.identity);
}
}
map += "r";
}
map += "c";
}
Debug.Log(map);
if (MapSize % 2 == 0)
{
GameObject wt = Instantiate(_TopW, new Vector3(-.5f, 0, -MapSize / 2), Quaternion.identity);
wt.transform.localScale = new Vector3(MapSize, 5, 1);
GameObject wb = Instantiate(_Botw, new Vector3(-.5f, 0, MapSize / 2+1), Quaternion.identity);
wb.transform.localScale = new Vector3(MapSize, 5, 1);
GameObject wl = Instantiate(_LeftW, new Vector3(-MapSize / 2-1, 0, .5f), Quaternion.identity);
wl.transform.localScale = new Vector3(1, 5, MapSize+2);
GameObject wr = Instantiate(_RightW, new Vector3(MapSize / 2, 0, .5f), Quaternion.identity);
wr.transform.localScale = new Vector3(1, 5, MapSize+2);
}
else
{
GameObject wt = Instantiate(_TopW, new Vector3(-.5f, 0, -MapSize / 2), Quaternion.identity);
wt.transform.localScale = new Vector3(MapSize-1, 5, 1);
GameObject wb = Instantiate(_Botw, new Vector3(-.5f, 0, MapSize / 2 + 1), Quaternion.identity);
wb.transform.localScale = new Vector3(MapSize-1, 5, 1);
GameObject wl = Instantiate(_LeftW, new Vector3(-MapSize / 2 - 1, 0, .5f), Quaternion.identity);
wl.transform.localScale = new Vector3(1, 5, MapSize + 1);
GameObject wr = Instantiate(_RightW, new Vector3(MapSize / 2, 0, .5f), Quaternion.identity);
wr.transform.localScale = new Vector3(1, 5, MapSize + 1);
}
Debug.Log(MapSize % 2);
}
这是更新的代码段
public GameObject[] tiles = new GameObject[1000];
public string map;
public override void OnStartServer()
{
//base.OnStartServer();
GetComponent<Renderer>().material.color = Color.blue;
}
public void OnPlayerConnected(NetworkPlayer player)
{
foreach(GameObject go in tiles)
{
NetworkServer.Spawn(go);
}
}
void Start()
{
if (!isServer)
{
return;
}
int temp = 0;
for (int c = MapSize / 2; c > -MapSize / 2; c--)
{
for (int r = -MapSize / 2; r < MapSize / 2; r++)
{
var t = Instantiate(prefab, new Vector3(r, 0, c), Quaternion.identity);
tiles[temp] = t;
NetworkServer.Spawn(t);
}
}
}
经过一番摆弄后,我得到了要生成的图块,但它们没有同步为每个客户端单独运行的 henerate 图块的字符。