Unity套接字程序无法运行 [英] Unity socket program doesn't run

查看:134
本文介绍了Unity套接字程序无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我统一制作了一个简单的游戏.我希望该游戏从套接字作为服务器读取数据.名为Socket.cs的文件,将创建一个服务器并侦听端口8000,如果我在Visual Studio中运行此文件,它将起作用.我要在团结游戏开始时运行此文件.问题在这里,游戏开始了,但是尚未启动socket.cs文件. Socket.cs放在统一游戏中的资产目录中.

I made a simple game in unity. I want this game to read data from socket as server. File named Socket.cs, creates a server and listens port 8000, if i run this file in visual studio, it works. I want to run this file when the unity game has started. The problem is here, game starts but socket.cs file hasn't been started. Socket.cs is placed on assets directory in unity game.

我在C#中用于套接字服务器的代码: http://paste.ubuntu.com/16466625/

The code i used for socket server in c#: http://paste.ubuntu.com/16466625/

游戏开始后,您能帮我启动socket.cs文件吗?

Could you help me please to start socket.cs file when the game has been started?

谢谢

推荐答案

首先,Unity没有像普通C#程序那样的Main函数.因此,将代码放入public static void Main()不会执行任何操作. Unity使用Start()和其他类似功能,因为它不是Main()的起点.

First of all, Unity does not have a Main function like a normal C# program. So putting a code in public static void Main() will not do anything. Unity uses Start() and other simliar functions as it's starting point not Main().

Socket.cs被放置在统一游戏的资产目录中

Socket.cs is placed on assets directory in unity game

仅将脚本放置在资产文件夹中并不意味着它将起作用.您必须从另一个Unity脚本实例化该脚本或将该脚本附加到GameObject.

Simply placing a script in the Asset Folder does not mean it will work. You have to instantiate the script from another Unity script or attach the Script to a GameObject.

即使代码在Unity中工作,Unity也会冻结,因为套接字代码是同步的而不是异步的.在阅读或等待客户端连接时它将阻塞.

Also even if the code worked in Unity, Unity would freeze because the socket code is synchronous instead of asynchronous. It will block while reading or waiting for client to connect.

您应该使用Thread或使用asynchronous插槽.

You should either make it with a Thread or use asynchronous socket.

此处是可以在Unity中使用的完整的Unity TCP服务器.它使用Thread.

Here is complete Unity TCP server that works in Unity. It uses Thread.

我建议您学习Unity的工作方式.学习基础知识.

I suggest you learn how Unity works. Learn the basic.

这篇关于Unity套接字程序无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆