Hello,
I have an .exe
program made in Python with Mediapipe. I call this program from Unity so that it runs every time the game starts. Everything works fine from the Unity interface, but when I try to Build my program nothing happens, it doesn’t want to build, nor do I get an error. How can I solve it so that I can build my unity with my Python .exe
program?
I have my file where I run the Python .exe
in Unity (Assets/PythonApp/DSDSmain/DSDSmain.exe
).
This is the code that calls that .exe
file:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class PythonManager : MonoBehaviour
{
private const string PythonAppName = "/PythonApp/DSDSmain/DSDSmain.exe";
private static Process PythonProcess;
[RuntimeInitializeOnLoadMethod]
private static void RunOnStart()
{
UnityEngine.Debug.Log(Application.dataPath + PythonAppName);
ProcessStartInfo PythonInfo = new ProcessStartInfo();
PythonInfo.FileName = Application.dataPath + PythonAppName;
PythonInfo.WindowStyle = ProcessWindowStyle.Hidden;
PythonInfo.CreateNoWindow = true;
PythonProcess = Process.Start(PythonInfo);
Application.quitting += () =>
{ if (!PythonProcess.HasExited) PythonProcess.Kill(); };
}
}
Is the python .exe file present in the build? How did it get there?
Unity’s built in way to solve the issue is to move the .exe file to the StreamingAssets folder. Anything in this folder is not imported as unity asset, but is directly copied to the build. You can then use Application.streamingAssetsPath to get the address of this folder within the environment (be it editor or build).
So instead of Assets/PythonApp/DSDSmain/DSDSmain.exe
, move it to Assets/StreamingAssets/PythonApp/DSDSmain/DSDSmain.exe
, then use something like
System.IO.Path.Combine(Application.streamingAssetsPath, "PythonApp/DSDSmain/DSDSmain.exe")
to get the exe file path.
@Pangamini Preformatted text
I placed this line of code to know what appears in the console:
UnityEngine.Debug.Log(System.IO.Path.Combine(Application.streamingAssetsPath, "/PythonApp/main/main.exe"))
and locate the .exe in: Assets/StreamingAssets/PythonApp/DSDSmain/DSDSmain.exe
but I got this error in Unity:
PythonManager.cs(16,114): error CS1002: ; expected
How can it be solved?
the complete code is:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class PythonManager : MonoBehaviour
{
private const string PythonAppName = "/StreamingAssets/PythonApp/DSDSmain/DSDSmain.exe";
private static Process PythonProcess;
[RuntimeInitializeOnLoadMethod]
private static void RunOnStart()
{
UnityEngine.Debug.Log(Application.dataPath + PythonAppName);
UnityEngine.Debug.Log(System.IO.Path.Combine(Application.streamingAssetsPath, "/PythonApp/DSDSmain/DSDSmain.exe"))
ProcessStartInfo PythonInfo = new ProcessStartInfo();
PythonInfo.FileName = Application.dataPath + PythonAppName;
PythonInfo.WindowStyle = ProcessWindowStyle.Hidden;
PythonInfo.CreateNoWindow = true;
PythonProcess = Process.Start(PythonInfo);
Application.quitting += () =>
{ if (!PythonProcess.HasExited) PythonProcess.Kill(); };
}
}
already executed!!, this is the new complete code:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using System.IO;
public class PythonManager : MonoBehaviour
{
private const string PythonAppName = "PythonApp/DSDSmain/DSDSmain.exe";
private static Process PythonProcess;
[RuntimeInitializeOnLoadMethod]
private static void RunOnStart()
{
string pythonAppPath = System.IO.Path.Combine(Application.streamingAssetsPath, PythonAppName);
UnityEngine.Debug.Log(pythonAppPath);
ProcessStartInfo PythonInfo = new ProcessStartInfo();
PythonInfo.FileName = pythonAppPath;
PythonInfo.WindowStyle = ProcessWindowStyle.Hidden;
PythonInfo.CreateNoWindow = true;
PythonProcess = Process.Start(PythonInfo);
Application.quitting += () =>
{ if (!PythonProcess.HasExited) PythonProcess.Kill(); };
}
}
Thank you!