Skip to content

Commit 5f18134

Browse files
authored
Merge pull request #88 from Unity-Technologies/UNI-23240-bring-Unity-to-front-Windows
UNI-23240 wrote c# script to bring Unity to front on Windows
2 parents 9680bc6 + a867da3 commit 5f18134

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

Assets/Integrations/Autodesk/maya2017/scripts/unityOneClick/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ def doIt(self, args):
151151
.format(unityAppPath, unityProjectPath, unityCommand)
152152

153153
elif maya.cmds.about(windows=True):
154-
melCommand = r'system("start \"{0}\" -projectPath {1} -executeMethod {2}");'\
155-
.format(unityAppPath, unityProjectPath, unityCommand)
154+
melCommand = r'system("start \"{0}\" \"{1}\" \"-projectPath {2} -executeMethod {3}\"");'\
155+
.format(unityProjectPath + "/Assets/Integrations/BringToFront.exe", unityAppPath, unityProjectPath, unityCommand)
156156

157157
else:
158158
raise NotImplementedError("missing platform implementation for {0}".format(maya.cmds.about(os=True)))

BringToFront.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Runtime.InteropServices;
6+
using System.Diagnostics;
7+
8+
namespace FbxExporters
9+
{
10+
class Program
11+
{
12+
13+
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
14+
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
15+
16+
[DllImport("USER32.DLL")]
17+
public static extern bool SetForegroundWindow(IntPtr hWnd);
18+
[DllImport("User32.dll")]
19+
private static extern bool IsIconic(IntPtr handle);
20+
[DllImport("User32.dll")]
21+
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
22+
const int SW_RESTORE = 9;
23+
public static void bringToFront(string title)
24+
{
25+
// Get a handle to the application.
26+
IntPtr handle = FindWindow(null, title);
27+
28+
// Verify that this is a running process.
29+
if (handle == IntPtr.Zero)
30+
{
31+
return;
32+
}
33+
if (IsIconic(handle))
34+
{
35+
ShowWindow(handle, SW_RESTORE);
36+
}
37+
38+
SetForegroundWindow(handle);
39+
}
40+
41+
static void Main(string[] args)
42+
{
43+
Process[] processlist = Process.GetProcessesByName("Unity");
44+
45+
bool found = false;
46+
foreach (Process process in processlist)
47+
{
48+
if (!String.IsNullOrEmpty(process.MainWindowTitle))
49+
{
50+
bringToFront(process.MainWindowTitle);
51+
found = true;
52+
}
53+
}
54+
55+
if(!found){
56+
Process myProcess = new Process();
57+
myProcess.StartInfo.FileName = args[0];
58+
if(args.Length > 1){
59+
myProcess.StartInfo.Arguments = args[1];
60+
}
61+
myProcess.Start();
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)