[Utility] Unity calls external EXE or Shell commands, unityshell
All rights reserved. The source must be indicated for reprinting!
Those who like Firefox, Java, unity3D, and game development can join the Q group in muye village: 379076227
1. Open-door demands
Sometimes, we want to integrate some external commands into unity. For example, you want to update SVN by clicking a button in Unity (assuming the project is managed by SVN ).
Then, it involves a Unity call of external executable files, bat/shell and so on.
This requirement is quite common and is not difficult to implement.
2. simple and clear implementation
First, we encapsulate a function called by a command:
[C #] how to view the copy code in plain text?
0102030405060708091011121314151617181920212223242526272829 |
private static void processCommand( string command, string argument){ ProcessStartInfo start = new ProcessStartInfo(command); start.Arguments = argument; start.CreateNoWindow = false ; start.ErrorDialog = true ; start.UseShellExecute = true ; if (start.UseShellExecute){ start.RedirectStandardOutput = false ; start.RedirectStandardError = false ; start.RedirectStandardInput = false ; } else { start.RedirectStandardOutput = true ; start.RedirectStandardError = true ; start.RedirectStandardInput = true ; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } Process p = Process.Start(start); if (!start.UseShellExecute){ printOutPut(p.StandardOutput); printOutPut(p.StandardError); } p.WaitForExit(); p.Close(); } |
Well, if you are interested in the above code, you can go to msdn to check the API.
Do not care, continue to see how to use.
3. Meticulous and patient command line Teaching
For the function encapsulated above, the first parameter is the command name, and the second parameter is the parameter accepted by this command.
For those who do not have access to the command line. Now, let's talk to me about the command line. If you are familiar with the command line, you can directly jump to step 1.
The following operations are performed in WIN7.
A: Press win + R, enter "cmd" in the displayed running window, and press Enter.
B: A command line window is displayed.
C: Enter the command notepad and press Enter.
D: The notepad is opened.
E: assume that your dashboard has a file 1.txt.
F: Then you can enter notepad D: \ 1.txt in the command line to directly use the notebook to open the 1.txt of the d Drive.
In short, the above notepad is the first parameter of the encapsulated function to be passed in. And D: \ 1.txt is our second parameter.
What are the executable commands?
The exe and bat/bash (linux) files in all paths in the system variable PATH can be executed.
You can enter echo % PATH % in the command line to obtain the PATH information.
You can also choose "desktop"> "computer"> "right-click"> "properties"> "Advanced System settings" to view the PATH in "environment variables ".
Double-click this path to view and edit it.
If there is no path in the path. You can only use the full path to call the exe in the path. For example, you want to call the exe D: \ AA \ BB \ CC \ d.exe. If PATH does not contain D: \ AA \ BB \ CC. Then, you need to input D: \ AA \ BB \ CC \ d.exe in the command line to call him. If PATH contains D: \ AA \ BB \ CC. You can directly input d.exe in the command line to call it.
Okay. So much about the command line ~~ Go to step 2.
4. Call the function defined in step 2 cruelly
For example, if you want to update SVN, you only need to find a place in Unity (for example, you can write it) and execute the following code:
[C #] how to view the copy code in plain text?
010203040506070809101112131415161718192021222324252627282930313233343536373839404142 |
public class SvnForUnity{ static string SVN_BASE = "F:\\Project\\Game" ; [MenuItem( "SVN/Update" , false , 1)] public static void SvnUpdate(){ processCommand( "svn" , "update \"" +SVN_BASE+ "\"" ); } private static void processCommand( string command, string argument){ ProcessStartInfo start = new ProcessStartInfo(command); start.Arguments = argument; start.CreateNoWindow = false ; start.ErrorDialog = true ; start.UseShellExecute = true ; if (start.UseShellExecute){ start.RedirectStandardOutput = false ; start.RedirectStandardError = false ; start.RedirectStandardInput = false ; } else { start.RedirectStandardOutput = true ; start.RedirectStandardError = true ; start.RedirectStandardInput = true ; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } Process p = Process.Start(start); if (!start.UseShellExecute){ printOutPut(p.StandardOutput); printOutPut(p.StandardError); } p.WaitForExit(); p.Close(); } } |
The SVN/Update button is added to Unity. When you click him, He updates SVN.