WPF Unity two-way communication

Source: Internet
Author: User
Tags flush
Description

The requirement is this-an editor. It is convenient to edit various static data tables (Excel) and to preview the resources specified in the table (spine skeletal animation).

The problem is that the software framework for the table editor, such as WPF, WinForm, and so on, does not have a corresponding spine render library, and frameworks that support spine rendering, such as, MonoGame, cocos2d, etc., or have problems with Excel libraries that are bad or lack software to control.

Our approach is to use WPF as a software feature that it excels at, while using unity as a "rendering control." Technical Route

Why not use WPF embedding?
embedding unity in WPF (or WinForm) is a common practice of wrapping unitywebplayer into a real control through ActiveX. But because Unitywebplayer is running in a security sandbox, it has at least the following two issues: An assembly that does not have a System.IO, can only load assetbundle, cannot load a spine without pipeline preprocessing, how does the Excel file add unity Windows Standalone embedded in WPF
Specifies the standalone as a control, under the WPF parent window. Use WINDOWSAPI to achieve.

How to implement two-way communication between standalone and WPF
You need to be able to set the current spine of standalone in WPF, and you can manipulate Excel maintained by WPF on standalone. A viable solution is pipes.
This discussion says unity does not initialize Namedpipelineserver. Then take it as a namedpipelineclient. Embed Unity Windows Standalone into WPF

The core code is as follows:
Define a WPF user control to define the size of the standalone
ProcessStartInfo info = new ProcessStartInfo ("UnityControl.exe");
Info. UseShellExecute = true;
Info. WindowStyle = processwindowstyle.minimized;
m_appprocess = System.Diagnostics.Process.Start (info);
M_appprocess.waitforinputidle ();
This. Dispatcher.begininvoke (
System.Windows.Threading.DispatcherPriority.ApplicationIdle,
Appidleevent, this, null);

Application Idle Event handling
windowinterophelper helper = new Windowinterophelper (Window.getwindow (this));
IntPtr ptr = helper. Handle;
SetParent (app. Mainwindowhandle, Helper. Handle);
SetWindowLong (New HandleRef (this, app. Mainwindowhandle), Gwl_style, ws_visible);
MoveWindow (app. Mainwindowhandle, (int) control. Margin.left, (int) control. Margin.top, (int) control. Width, (int) control. Height, True);
Implementing Unity Windows Standalone and WPF communication
WPF Namedpipeserverstream pipeserver = new Namedpipeserverstream ("Testpipe", pipedirection.inout, 1);

Pipeserver.waitforconnection ();
string str = "Hello from server!";
byte[] Outbuffer = Encoding.Unicode.GetBytes (str);
int len = outbuffer.length;
Pipeserver.writebyte ((Byte) (len/256));
Pipeserver.writebyte ((Byte) (len% 256));
Pipeserver.write (outbuffer, 0, Len);

Pipeserver.flush ();
Len = Pipeserver.readbyte () * 256;
Len + = Pipeserver.readbyte ();
byte[] Inbuffer = new Byte[len];
Pipeserver.read (inbuffer, 0, Len);

String remoteinfo = Encoding.Unicode.GetString (Inbuffer);

Lbremoteinfo.content = Remoteinfo;
Unity pipeclient = new Namedpipeclientstream (".", "Testpipe", pipedirection.inout);

Pipeclient.connect (10000);
int len = pipeclient.readbyte () * 256;
Len + = Pipeclient.readbyte ();
byte[] Inbuffer = new Byte[len];
Pipeclient.read (inbuffer, 0, Len);

Remoteinfo = Encoding.Unicode.GetString (Inbuffer);
Pipeclient.flush (); string str = "Hello from ClienT! ";
byte[] Outbuffer = Encoding.Unicode.GetBytes (str);
len = outbuffer.length;
Pipeclient.writebyte ((Byte) (len/256));
Pipeclient.writebyte ((Byte) (len% 256));
Pipeclient.write (outbuffer, 0, Len);
 Pipeclient.flush ();
effect

A simple test that looks good: unity (Client) is embedded in the WPF (Server) window WPF sends a "Hello from server!" to unity, showing unity in the upper-left corner of the unity control to send WPF a " Hello from client! ", displayed on a WPF label

Description and Reference

MSDN Pipeline Newsletter Demo
How to: Use Named pipes for network interprocess communication

Mono does not support the pipeline security level
No need to, Namedpipeserverstream constructor, do not fill tokenimpersonationlevel
StackOverflow

Unity cannot start Namedpipelineserver
StackOverflow

Enable the System.IO.Pipes namespace in Unity
In player settings, set the. NET version to. net2.0, which by default is. net2.0 subset
StackOverflow

How to embed a form as a control
C # Custom Controls: WinForm Other application forms into their own internal unity plug-in WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.