Search code examples
c#.netactivexunity-game-engineaxhost

ActiveX initialization: AxHost.State object


I'm trying to embed a Unity3D-ActiveX control into a WPF-Form by using a WinFormsHost-Control.

Actually it works well when setting the path in the property window of VS, but when setting it in my code file it does not load anything. This is a known issue of the control but i thought i can simply copy the creation code of the forms-designer and initialize it manually.

When looking at the code of the initialization i noticed that there is no src property in the code, but the property is used in the property window. Setting the property manually does not work ( throws a error ).

After some test's i decided to check the hole assembly for the src property, but the src property is never set and i can't even find the string of the path.

Final thoughts

I noticed that there can be only one place where the src-path is located: The resource generated by the window forms designer, which is a object of the AxHost.State-type.

Question

How do i create a valid AxHost.State object to initialize the Unity3D-ActiveX control which should load a Unity3D-file specified by me?


Solution

  • This is the solution which works but is a little bit slow( Note: You need to initialize the Control once in the Forms Designer and copy the OcxState object into the assembly resources ):

    // Create a ocx state object with the correct path
    _Unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
    ((System.ComponentModel.ISupportInitialize)(_Unity)).BeginInit();
    _Unity.OcxState = (AxHost.State)(Resources.Unity3DOcx);
    _Unity.TabIndex = 0;
    Controls.Add(_Unity);
    ((System.ComponentModel.ISupportInitialize)(_Unity)).EndInit();
    _Unity.src = _File;
    AxHost.State state = _Unity.OcxState;
    _Unity.Dispose();
    
    // Create the unity web player object
    _Unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
    ((System.ComponentModel.ISupportInitialize)(_Unity)).BeginInit();
    this.SuspendLayout();
    _Unity.Dock = DockStyle.Fill;
    _Unity.Name = "Unity";
    _Unity.OcxState = state;
    _Unity.TabIndex = 0;
    Controls.Add(_Unity);
    ((System.ComponentModel.ISupportInitialize)(_Unity)).EndInit();
    this.ResumeLayout(false);