Unity3d HLAPI,Unet,网络感知产生

时间:2016-11-03 19:03:20

标签: networking unity3d game-engine unity5 unity3d-unet

您好我正在尝试创建我的第一个团结游戏,我想要网络感知但我鼓励网络预制产生的问题。这是我的代码:

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

}

编译器说没有问题,但在统一中我遇到了这样的错误:&#34; UNetWeaver错误:脚本生成器使用[命令] CmdSpawnBuilding但不是NetworkBehaviour。 UnityEngine.Debug:LOGERROR(对象)&#34;我的代码运行良好,没有&#34; [命令]&#34;他们认为它没有网络意识。此外,我知道它有点混乱,但我试图找出它的错误,所以是的,有点乱码。

1 个答案:

答案 0 :(得分:2)

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

public class Builder : NetworkBehaviour