当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » C#  » 编程基础
_guojj_

在WPF中使用unity3D控件

_guojj_ 发布于 2016年06月28日 13时, 0评/1677阅
分享到: 
收藏 +0
0
原理通在wpf调用winform控件实现
标签: <无>

代码片段(2) [全屏查看所有代码]

1. [文件] MainWindow.xaml.cs ~ 3KB     下载(6)     跳至 [1] [2] [全屏预览]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AxUnityWebPlayerAXLib;

namespace WPFPlusUnityTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private AxUnityWebPlayer axUnityWebPlayer1;
        public MainWindow()
        {


            InitializeComponent();
            InitUnity();
            //InitUnity3D();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            new Form1().Show();
        }


        void InitUnity3D()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainWindow));

            this.axUnityWebPlayer1 = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
            ((System.ComponentModel.ISupportInitialize)(this.axUnityWebPlayer1)).BeginInit();
            
            this.axUnityWebPlayer1.Enabled = true;
            this.axUnityWebPlayer1.Location = new System.Drawing.Point(50, 36);
            this.axUnityWebPlayer1.Name = "axUnityWebPlayer1";
            this.axUnityWebPlayer1.OcxState = axUnityWebPlayer1.OcxState;
            this.axUnityWebPlayer1.Dock = DockStyle.Fill;
            this.axUnityWebPlayer1.TabIndex = 0;
            this.axUnityWebPlayer1.src = @"file:///E:/Unity/WPFPlusUnityTest/WPFPlusUnityTest/bin/Debug/WPFPlusUnityTest.unity3d";
            (this.host.FindName("panel") as System.Windows.Forms.Panel).Controls.Add(this.axUnityWebPlayer1);
            ((System.ComponentModel.ISupportInitialize)(this.axUnityWebPlayer1)).EndInit();
        }

        private void InitUnity()
        {
            var unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
            ((System.ComponentModel.ISupportInitialize)(unity)).BeginInit();
            (this.host.Child as System.Windows.Forms.Panel).Controls.Add(unity);
            ((System.ComponentModel.ISupportInitialize)(unity)).EndInit();
            unity.src = @"file:///E:/Unity/WPFPlusUnityTest/WPFPlusUnityTest/bin/Debug/WPFPlusUnityTest.unity3d";
            AxHost.State state = unity.OcxState;
            unity.Dispose();
            unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
            ((System.ComponentModel.ISupportInitialize)(unity)).BeginInit();
            (this.host.Child as System.Windows.Forms.Panel).SuspendLayout();
            unity.Dock = DockStyle.Fill;
            unity.Name = "Unity";
            unity.OcxState = state;
            unity.TabIndex = 0;
            (this.host.Child as System.Windows.Forms.Panel).Controls.Add(unity); //panel1是我用的一个容器,改成this.Controls也可以
            ((System.ComponentModel.ISupportInitialize)(unity)).EndInit();
            (this.host.Child as System.Windows.Forms.Panel).ResumeLayout(false);
        }
    }
}

2. [代码][C#]代码     跳至 [1] [2] [全屏预览]

<Window x:Class="WPFPlusUnityTest.MainWindow"
        xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFPlusUnityTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <wfi:WindowsFormsHost x:Name="host">
            <wf:Panel Name="panel"></wf:Panel>
        </wfi:WindowsFormsHost>
    </Grid>
</Window>


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

开源从代码分享开始 分享代码
_guojj_的其它代码 全部(3)...