1

您好,我正在尝试统一创建我的第一个游戏,我想了解网络,但我遇到了网络预制产生的问题。这是我的代码:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Builder : NetworkBehaviour {


    public GameObject preview;
    public Transform currentPreview;
    bool isPreviewing = false;
    GameObject buildingPreview;
    private NetworkIdentity networkId;

    // Use this for initialization
    void Start ()
    {
        networkId = GetComponent<NetworkIdentity>();
    }

    // Update is called once per frame

    void ViewPreview()
    {
        buildingPreview = Instantiate(preview, transform.position, transform.rotation) as GameObject;
        currentPreview = buildingPreview.transform;
        isPreviewing = true;
    }
    void Update ()
    {
        CmdBuild(); 
    }

    void CmdBuild()
    {
        if (networkId.isLocalPlayer)
        {

        }
        else
        { return; }
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (!isPreviewing)
                ViewPreview();
            else
            {
                Destroy(buildingPreview);
                isPreviewing = false;
            }
        }
        if (isPreviewing)
        {
            Preview();
        }
    }

    [Command]
    void CmdSpawnBuilding()
    {
        GameObject buildingPlaced = Instantiate(preview, currentPreview.position, currentPreview.rotation) as GameObject;
        NetworkServer.Spawn(buildingPlaced);
    }

    void Preview()
    {
        currentPreview.position = transform.position + transform.forward * 3f;
        currentPreview.rotation = transform.rotation;
        if (Input.GetButtonDown("Fire1"))
        {
            CmdSpawnBuilding();
            isPreviewing = false;
        }
    }

}

编译器说没有问题,但统一起来我有这样的错误:“UNetWeaver 错误:脚本生成器使用 [Command] CmdSpawnBuilding 但不是 NetworkBehaviour。UnityEngine.Debug:LogError(Object)”我的代码在没有“[Command ]" 行,尽管它不是网络感知的。另外,我知道这有点混乱,但我试图找出问题所在,所以是的,代码有点混乱。

4

1 回答 1

2

您的类需要从 NetworkBehaviour 继承,如下所示:

public class Builder : NetworkBehaviour 
于 2016-11-03T19:23:20.920 回答