有没有办法从unity c#和python程序发送数据?
我有一个unity项目,我想将对象的位置和其他数据一起发送到一个python项目,该项目将有一个神经网络,然后将输出发送回unity有没有办法从unity c#和python程序发送数据?,c#,python,unity3d,C#,Python,Unity3d,我有一个unity项目,我想将对象的位置和其他数据一起发送到一个python项目,该项目将有一个神经网络,然后将输出发送回unity void Update() { System.IO.StreamWriter saveFile = new System.IO.StreamWriter("Reading/Positions/PlanePos.txt", false); saveFile.Write(this.transform.position);
我需要从unity发送到python的数据是对象的位置(x,y,z)和一些布尔值可能是一些字符串,神经网络的输出将是(w,a,s,d),我所做的是,我将数据写入一个文本文件,python从该文件中读取数据,但这种方式总是会产生错误,因为python需要在c#写入时读取文件,这将产生IO错误,另一种方式是使用剪贴板,但这对于小数据是可行的,但在我的情况下并不好 c#代码void Update() { System.IO.StreamWriter saveFile = new System.IO.StreamWriter("Reading/Positions/PlanePos.txt", false); saveFile.Write(this.transform.position); saveFile.Close(); System.IO.StreamWriter saveFile2 = new System.IO.StreamWriter("Reading/Positions/done.txt", false); if (this.transform.position.y> 2) { leftground = true; } if (leftground) { if (this.transform.position.y < 2) { saveFile2.Write("done"); saveFile2.Flush(); dones.text = "done"; } else { saveFile2.Write("air"); saveFile2.Flush(); dones.text = "air"; } } else { saveFile2.Write("ground"); saveFile2.Flush(); dones.text = "ground"; } saveFile2.Close(); }
我想用一种方法将数据从unity发送到python,反之亦然,就像服务器或任何可以这样做的东西一样。为什么您不想使用服务器?您可以用python编写API服务器或套接字服务器,然后用c从unity连接到该服务器并传输数据。@Raguel我不想使用服务器,因为我不知道我到底能做什么,但如果你告诉我我会怎么做。@yousiffayed网上有很多教程,请检查这个,用python创建简单的Rest API:,然后你可以使用Unity web request从服务器发布或获取数据,这并不难创建,所以试试吧。您可以使用python的套接字创建它,但是@AminSojoudi发送的内容是创建类似内容的最佳方式,此外,您还将学到许多新东西。如果您对使用套接字进行创建感兴趣,然后可能会建议您在YouTube上发布关于套接字的sentdex。为什么不想使用服务器?您可以用Python编写一个API服务器或套接字服务器,然后使用c#从Unity连接到该服务器并传输数据。@Raguel我不想使用服务器,因为我不知道如何确切地做到这一点,但如果您告诉我如何做到这一点它。@yousiffayed网上有很多教程,请检查这个,用python创建简单的Rest API:,然后您可以使用Unity web request从服务器发布或获取数据,创建起来并不困难,所以请尝试一下。您可以使用python的套接字创建它,但是@AminSojoudi发送的内容是创建类似内容的最佳方式,此外,您还将学到许多新东西。如果您对使用套接字创建感兴趣,那么可能会建议您在YouTube上发布关于套接字的sentdex。def read_data(): # reading from file file = open("D:/Cs/Grad/Tests/airplane test/Reading/Positions/PlanePos.txt", "r") planepos = file.readline() file.close() file = open("D:/Cs/Grad/Tests/airplane test/Reading/Positions/AirportPosition.txt", "r") airportpos = file.readline() file.close() # ================================================================== # spliting and getting numbers #plane_X, plane_Y, plane_Z = map(float, planepos.strip('() \n').split(',')) #airport_X, airport_Y, airport_Z = map(float, airportpos.strip('() \n').split(',')) planepos=planepos.strip('() \n').split(',') airportpos=airportpos.strip('() \n').split(',') return planepos[0], planepos[1], planepos[2], airportpos[0], airportpos[1], airportpos[2]