WPF 에 Unity 를 내장 하고 양 방향 통신 을 실현 합 니 다.

인용:http://www.unitymanual.com/blog-11491-1628.html
블 로그 원문:
http://www.lolofinil.com/2014/10/10/wpf-unity-embed-pipes
설명 하 다.
필요 한 것 은 편집기 입 니 다.각종 정적 데이터 시트(Excel)를 편리 하 게 편집 할 수 있 을 뿐만 아니 라 표 에 지정 한 자원 을 미리 볼 수 있 습 니 다(Spine 골격 애니메이션).
문 제 는 표 편집기 에 적합 한 소프트웨어 프레임 워 크,예 를 들 어 WPF,Winform 등 이 해당 하 는 Spine 렌 더 링 라 이브 러 리 가 없다 는 것 이다.한편,Spine 렌 더 링 을 지원 하 는 프레임 워 크,예 를 들 어 유 니 티 3d,MonoGame,Cocos2D 등 은 엑셀 라 이브 러 리 가 사용 하기 어렵 거나 소프트웨어 방향 컨트롤 이 부족 한 문제 가 존재 합 니 다.
WPF 를 잘 하 는 소프트웨어 기능 으로 사용 하고 유 니 티 를'렌 더 링 컨트롤'로 사용 하 는 방안 입 니 다.
기술 노선
왜 WPF 를 유 니 티 웹 플레이어 에 끼 워 넣 지 않 습 니까?Unity 를 WPF(또는 Winform)에 끼 워 넣 는 것 은 ActiveX 를 통 해 Unity WebPlayer 를 진정한 컨트롤 로 포장 하 는 것 이 흔 하 다.그러나 유 니 티 웹 플레이어 가 안전 샌 드 박스 에서 실행 되 기 때문에 다음 과 같은 두 가지 문제 가 있 습 니 다.
System.IO 의 프로그램 집합 이 없 으 면 assetbundle 만 불 러 올 수 있 고 파이프라인 예비 처리 되 지 않 은 Spine을 불 러 올 수 없습니다.
Excel 파일 을 불 러 올 수 없습니다Unity Windows Standalone 을 WPF 에 끼 워 넣 는 방법 은 Standalone 을 하나의 컨트롤 로 WPF 부모 창 에 지정 합 니 다.윈도 API 로 구현 합 니 다.
Standalone 과 WPF 의 양 방향 통신 을 어떻게 실현 하 는 지 는 WPF 에서 Standalone 의 현재 Spine 을 설정 할 수 있 고 Standalone 에서 도 WPF 가 유지 하 는 Excel 을 조작 할 수 있어 야 합 니 다.실행 가능 한 방안 은 pipes 이다.이 토론 에 서 는 Unity 가 NamedPipeline Server 를 초기 화하 지 않 는 다 고 합 니 다.그럼 이 걸 Named Pipeline Client 로 하 겠 습 니 다.
Unity Windows Standalone 을 WPF 에 끼 워 넣 기
핵심 코드 는 다음 과 같 습 니 다.//WPF 사용자 컨트롤 을 정의 하여 Standalone 의 사 이 즈 를 정의 합 니 다.ProcessStartInfo info=new ProcessStartInfo("Unity Control.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     
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); 

Unity Windows Standalone 과 WPF 통신 실현
// 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(); 

효과.
간단 한 테스트 를 했 는데 효과 가 좋아 보 였 다.
Unity(Client)가 WPF(Server)창 에 삽입 되 었 습 니 다WPF 는 Unity 에"hello from server!"를 보 냅 니 다.Unity 컨트롤 의 왼쪽 상단 에 표시Unity 는 WPF 에"hello from client!"를 보 냅 니 다.WPF 라벨 에 표시설명 과 참고
MSDN 파이프 통신 데모 어 떻 습 니까?이름 파 이 프 를 사용 하여 네트워크 프로 세 스 간 통신 을 합 니 다.
Mono 는 파이프 보안 단 계 를 지원 하지 않 습 니 다.사용 하지 않 으 면 됩 니 다.NamedPipe ServerStream 구조 함수 에 TokenImpersonationLevelStackOverflow 를 쓰 지 마 십시오.
Unity 에서 NamedPipelineServerStackOverflow 를 시작 할 수 없습니다.
Unity 에서 System.IO.Pipes 네 임 스페이스 를 Player Settings 에서.net 버 전 을.net 2.0 으로 설정 합 니 다.기본 값 은.net 2.0 subset StackOverflow 입 니 다.
컨트롤 C\#사용자 정의 컨트롤 로 창 을 삽입 하 는 방법:WinForm 은 다른 프로그램 창 을 자신의 내부 에 삽입 합 니 다.
다음으로 전송:https://www.cnblogs.com/nearpengju123/p/4494573.html

좋은 웹페이지 즐겨찾기