Hi, I would like to make an program using Unity and WPF component.
Basically, The program shows 3d Object using Unity engine inside the frame
which has several components from WPF.
so the main frame is made by “WPF”, inside main frame(WPF), we would like to use
unity engine. I’ve watched some videos from Youtube,
they show me that we are able to use WPF components inside unity program.
it seems like using a special tool, but I have no idea about it
is that possible? if so, how would I like to do this? Thank you =)
(Sorry for my English, Eng is my 2nd language,
so some grammar mistakes might exist)
You can do this by getting Unity Handle and setting the Control’s handle to Unity’s handle with SetParent(). You will also need to call SetWindowLong() to hide the border of the Unity window and you might also need to position the Unity window at the top left of the control by calling MoveWindow().
If you don’t know what I mean here is the sample code below. This starts Unity waits for the process to load and parents it to the control UnityPanel which is a Panel. Then removes the boarder from the window and positions Unity at the top left corner of the control. You will want to make sure that you run your game in window mode.
Are you sure the “Unity” in the video is in fact Unity3D? Microsoft has a .NET product called Unity (contained in PRISM). It has nothing to do with games, but used for dependency injection.
But you have to change some code to make everythink work smoothely.
To position Unity right (if unity should not be in the upper left corner of youre window):
Point position = this.TranslatePoint(new Point(0f, 0f), Window.GetWindow(this));
MoveWindow(_appWin, (int)(position.X * scalingFactor), (int)(position.Y * scalingFactor), (int)(ActualWidth * scalingFactor), (int)(ActualHeight * scalingFactor), true);
´
And to aquiere the handle when unity takes some time to load:
//Wait to aquire handle.
_childp.WaitForInputIdle();
while (_childp.MainWindowHandle == IntPtr.Zero)
{
Thread.Sleep(10);
}
For the communication between Unity and WPF I used TcpListener
If done right this is fast enough to send data back and forth every frame.