经过N过努力,终于搞定了联网,服务器根据用户选择创建地图,然后地图会同步到客户端。
现在的问题是,在服务端创建地图中的物体,物体的transform关系是正确的,但是一旦到客户端,都去根了。
下图是服务器端的地图物体列表:
而下面这个是客户端的地图物体列表
所有的子物体,Box啊,Ground的啊,Wall啊,都应该属于GameMap的。服务器上很好,但是客户端就全部都在根下。
代码如下:
public class GameMap : NetworkBehaviour
{
protected int m_MapWidth = 16;
protected int m_mapHeight = 16;
[SerializeField] protected Transform m_GameMapRoot;
[SerializeField] protected GameObject[] m_GroundPrefabs;
[SerializeField] protected GameObject[] m_WallPrefabs;
[SerializeField] protected GameObject[] m_BlockPrefabs;
private void CreateWall( int x, int y)
{
GameObject wall = GameObject.Instantiate(m_WallPrefabs[0], new Vector3(x, 0.5f, y), Quaternion.identity, m_GameMapRoot );
NetworkServer.Spawn(wall);
}
private void CreateGround(int x, int y)
{
int nBlock = (x + y) % 2;
GameObject grd = GameObject.Instantiate( m_GroundPrefabs[ nBlock ], new Vector3(x, 0, y), Quaternion.identity, m_GameMapRoot );
NetworkServer.Spawn(grd);
// 在地图的奇数方格中随机产生各类方块。
// 45%概率放置可被破坏的东西,15%概率放置无法破坏的墙。40%概率留空。
if( nBlock == 1)
{
int b = -1;
int r = Random.Range(0, 100);
if (r < 15)
b = 0;
else if (r < 30)
b = 1;
else if (r < 45)
b = 2;
else if (r < 60)
b = 3;
if( b > - 1)
{
GameObject block;
if( b == 3 )
block = GameObject.Instantiate(m_WallPrefabs[1], new Vector3(x, 0.5f, y), Quaternion.identity, m_GameMapRoot);
else
block = GameObject.Instantiate(m_BlockPrefabs[b], new Vector3(x, 0.5f, y), Quaternion.identity, m_GameMapRoot);
NetworkServer.Spawn(block);
}
}
}
private void InitMap()
{
int nWidth = m_MapWidth + 1;
int nHeight = m_mapHeight + 1;
for (int i = nWidth; i >= 0; --i)
{
for (int j = nHeight; j >= 0; --j)
{
if (i == 0 || j == 0 || i == nWidth || j == nHeight)
CreateWall(i, j);
else
CreateGround(i, j);
}
}
}
public override void OnStartServer()
{
InitMap();
}
}
大神,怎么破??如何让物体孵化后保持原有的关系。