3

我有一段代码应该生成一个随机瓦片地图,它只为服务器执行。我不知道如何让它在玩家加入时同步到玩家。我将如何同步加入玩家的预制件?它们加载到一个玩家(服务器)上,但是当另一个玩家加入时,它们不会出现。

在此处输入图像描述

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 图块的字符。

在此处输入图像描述

4

1 回答 1

3

您不能只使用该Instantiate功能使预制件在网络上显示。您还必须做其他事情:

1、必须通过NetworkManager组件注册prefab。如果有多个预制件,则必须为所有预制件执行此操作:

在此处输入图像描述

2 .将组件附加NetworkIdentity到预制件上。请注意,如果您还想同步对象变换,您也必须附加NetworkTransform 到预制件。

3 . 实例化预制件后,NetworkServer.Spawn也可以在网络上实例化它。

例如:

//Instantiate on this device
GameObject red = Instantiate(SpawnLocRed, new Vector3(r, 2, c), 
Quaternion.identity);
//Instantiate it on all clients
NetworkServer.Spawn(red);

最后,您可能希望将所有代码封装在内部isLocalPlayer,以确保仅从本地播放器实例化。

if (isLocalPlayer)
{
    //Instantiate on this device
    GameObject red = Instantiate(SpawnLocRed, new Vector3(r, 2, c), 
    Quaternion.identity);
    //Instantiate it on all clients
    NetworkServer.Spawn(red);    
}
于 2018-05-18T01:56:09.487 回答