首页 > 解决方案 > 如何将 ZeroMQ 导入 Unity 引擎?

问题描述

我正在尝试为 Unity 中的项目导入 ZeroMQ 库。我正在使用C#Visual Studio 进行编辑。我使用 NuGet 将 ZeroMQ 导入 Visual Studio,但是当我尝试运行游戏时出现错误

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'ZeroMQ' could not be found (are you missing a using directive or an assembly reference?)    Assembly-CSharp C:\Users\<me>\OneDrive\Documents\UrBalls\Assets\Scripts\PlayerController.cs 4   Active

控制器文件来自教程:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZeroMQ;

public class PlayerController : MonoBehaviour
{
    public float speed;
    private Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();     
    }


    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        System.Console.WriteLine("Hey");
        rb.AddForce(movement * speed);
    }

    // Update is called once per frame
    void Update()
    {        
    }
}

如何让编译器看到包?赞赏!

标签: visual-studiounity3dzeromq

解决方案


我没有测试这是否真的有效,但我能够克服你在这里遇到的错误......

1) 如果您从 NuGet(metadings 包)安装了顶级结果,请将其卸载并尝试使用“clzmq”包(或 64 位版本)。

2) 将 DLL 从“C:\Users\.nuget\packages\clrzmq\2.2.5\lib”和“C:\Users\.nuget\packages\clrzmq\2.2.5\content”复制到“资产” ' Unity 项目的文件夹。

3) 在您的脚本中,切换“使用 ZeroMQ;” '使用 ZMQ;'

此时您应该可以运行了。

我没有尝试通过元数据从第一个“ZeroMQ”包中复制 DLL,因此如果您愿意,可以尝试。我会先尝试 clzmq,如果它不适合您的需求,那么再给 metadings 一个机会。您还必须为 Unity 配置 4.0 框架支持才能使用 metadings 包。

更新:我最终测试了这个。我必须使用 x64 版本,但它可以工作。这是完整的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using ZMQ;

public class mqtest : MonoBehaviour
{
    public float speed;
    private Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }


    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        System.Console.WriteLine("Hey");
        rb.AddForce(movement * speed);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            print("space key was pressed");
            string endpoint = "tcp://127.0.0.1:5555";

            // ZMQ Context and client socket
            using (ZMQ.Context context = new ZMQ.Context())
            using (ZMQ.Socket client = context.Socket(SocketType.REQ))
            {
                client.Connect(endpoint);
                string request = "Hello";
                client.Send(request, Encoding.UTF8);
            }
        }
    }
}

推荐阅读