通过网络实例化GameObjects

时间:2018-05-18 01:15:34

标签: c# unity3d unity3d-unet unet

我有一段代码应该生成一个随机的tile map,它只为服务器做。当他们加入时,我无法弄清楚如何让它与玩家同步。我如何同步加入玩家的预制件?它们加载到一个玩家(服务器)上,但当另一个玩家加入时,他们就不会出现。

enter image description here

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);
         }
     }
}

经过一些小工作后,我得到了瓷砖,但它们并没有同步为每个客户端分别运行的字符。

enter image description here

1 个答案:

答案 0 :(得分:2)

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

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

enter image description here

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);    
}