You can embed unity controls in a C # program (WINFORM,WPF) to implement a three-dimensional scene. Setting the scene by setting the Src property of the Unity control, however, SRC must be an absolute path, and can only be set in the designer and not dynamically modified in the code, which is tragic when the scene is dynamically switched when it is required to run. There are, of course, a lot of ways that unity can dynamically change the SRC method, which is correct, and the biggest problem is not stating where the code should be placed, anxious.
The following assumes the unity control's file Unity3dControl.cs, then open Unity3dControl.designer.cs, find the function private void InitializeComponent (), in
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager ( typeof (Unity3dcontrol)); This.u3dplayer = new U3dplayer (); ((System.ComponentModel.ISupportInitialize) (This.u3dplayer)). BeginInit (); This. SuspendLayout ();
After that, add the following code:
<span style= "White-space:pre" ></span> this.u3dPlayer.Dock = System.Windows.Forms.DockStyle.Fill; This.u3dPlayer.Enabled = true; This.u3dPlayer.Location = new System.Drawing.Point (0, 0); This.u3dPlayer.Name = "U3dplayer"; This.u3dPlayer.OcxState = ((System.Windows.Forms.AxHost.State) (resources. GetObject ("U3dplayer.ocxstate")); This.u3dPlayer.Size = new System.Drawing.Size (270, 285); This.u3dPlayer.TabIndex = 0; This. Controls.Add (This.u3dplayer); ((System.ComponentModel.ISupportInitialize) (This.u3dplayer)). EndInit (); U3DPLAYER.SRC = Modelfilepath; _state = u3dplayer.ocxstate; U3dplayer.dispose (); U3dplayer = new U3dplayer (); ((System.ComponentModel.ISupportInitialize) (U3dplayer)). BeginInit (); This. SuspendLayout (); U3dplayer.dock = DockStyle.Fill; U3dplayer.name = "Unity3d"; U3dplayer.ocXState = _state; U3dplayer.tabindex = 0; Controls.Add (U3dplayer); ((System.ComponentModel.ISupportInitialize) (U3dplayer)). EndInit (); This. ResumeLayout (FALSE);
In this, the most critical code is u3dplayer.src =modelfilepath, which passes the source file path from the outside to the unity control through the static property Modelfilepath , or it can be used in other ways.
According to the information on the Internet, in the editor, when the SRC attribute is set for unity, the string resource is automatically generated and assigned to the attribute ocxstate. Since there is no way to manually generate this string, you need to pass the code, that is, the first assignment to OcxState, and then the way to get the required string resources. Then assign the value to the re-created control.
Unity Dynamic Change SRC Solution