有没有从unity c和python程序发送数据的方法?

2024-09-26 04:59:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个unity项目,我想把对象的位置和其他数据一起发送到一个python项目,这个项目将有一个神经网络,然后将输出发送回unity。你知道吗

我需要从unity发送到python的数据是对象的位置(x,y,z)和一些bool,也许是一些字符串,神经网络的输出将是(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();
    }

Python代码:



    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]

i除了一种从unity向python发送数据的方法,反之亦然,就像服务器或任何可以这样做的东西一样。


Tags: 数据iotxtunitysystemfilewritesplit