Visual Basic .net and Unity 3D
The purpose of this page is to explain how to use the Unity 3D game engine inside Visual Basic .Net.

This page has been designed for people who have a basic knowledge of Visual Basic .Net and wish to incorporate Unity 3D in Visual Basic .Net applications.

Silly Warning:
You may NOT use this code if you really think I have gotten through all the trouble of legally copywriting this work. The purpose of this page is to help the 3D game making community and I encourage you to help as well.

This tutorial uses the "Simple As It Gets Policy" (SAIG). See bottom for details.
What you need.
Unity 3D Pro. 3.4
Visual Studio .Net 2005 or above.
An Html Editor.
In Unity 3D.
Prerequisites:
Knowledge of how to create a Scene and a Project in Unity 3D.
Knowledge of how to import game objects and how to place a game object on the scene.
Knowledge of how to create a Java Script File.
Knowledge of how to assign a script to the main camera.

Instructions:

Name your Main Camera as: Camera1
Create a new Java Script file, paste the following code and assign the Script to the Main Camera.
 
public function SetSelf(theobject : String) {
    var X : float; var Y : float; var Z : float;
   
var words = theobject.Split("|"[0]);
   
float.TryParse(words[1],X);
   
float.TryParse(words[2],Y);
   
float.TryParse(words[3],Z);
   
toRotate = GameObject.Find(words[0]);
   
toRotate.transform.eulerAngles = Vector3(X, Y, Z);
}


public function SetWorld(theobject : String) {
   
var X : float; var Y : float; var Z : float;
   
var words = theobject.Split("|"[0]);
   
float.TryParse(words[1],X);
   
float.TryParse(words[2],Y);
   
float.TryParse(words[3],Z);
   
toRotate = GameObject.Find(words[0]);
   
toRotate.transform.position = Vector3(X, Y, Z);
}

public function GetSelf(theobject : String) {
   
var X : float; var Y : float; var Z : float;
   
toRotate = GameObject.Find(theobject);
   
X = toRotate.transform.eulerAngles.x;
   
Y = toRotate.transform.eulerAngles.y;
   
Z = toRotate.transform.eulerAngles.z;
   
Application.ExternalCall("ShowGetSelf", X + "|" + Y + "|" + Z);
}

public function GetWorld(theobject : String) {
   
var X : float; var Y : float; var Z : float;
   
toRotate = GameObject.Find(theobject);
   
X = toRotate.transform.position.x;
   
Y = toRotate.transform.position.y;
   
Z = toRotate.transform.position.z;
   
Application.ExternalCall("ShowGetWorld", X + "|" + Y + "|" + Z);
}

public function Rotate(theobject : String) {
   
var X : float; var Y : float; var Z : float;
   
var words = theobject.Split("|"[0]);
   
float.TryParse(words[1],X);
   
float.TryParse(words[2],Y);
   
float.TryParse(words[3],Z);
   
toRotate = GameObject.Find(words[0]);
   
toRotate.transform.Rotate(X * Time.deltaTime, Y * Time.deltaTime, Z * Time.deltaTime);
}

public function Move(theobject : String) {
   
var X : float; var Y : float; var Z : float;
   
var words = theobject.Split("|"[0]);
   
float.TryParse(words[1],X);
   
float.TryParse(words[2],Y);
   
float.TryParse(words[3],Z);
   
toRotate = GameObject.Find(words[0]);
   
if (X != 0) {
       
toRotate.transform.position.x = toRotate.transform.position.x + X;
    }
   
if (Y != 0) {
       
toRotate.transform.position.y = toRotate.transform.position.y + Y;
    }
   
if (Z != 0) {
       
toRotate.transform.position.z = toRotate.transform.position.z + Z
;
    }
}
In HTML.
Prerequisites:
Knowledge of how to compile a Unity 3d game file for Web Game distribution.

Instructions:

Create a new web page.
Place the following code in the same folder as the "*.unity3d" generated file.
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Unity Web Player | WebPlayer</title>
        <script type="text/javascript" src="UnityObject.js"></script>
        <script type="text/javascript">
        <!--
        function GetUnity() {
            if (typeof unityObject != "undefined") {
                return unityObject.getObjectById("unityPlayer");
            }
            return null;
        }

        function
SetSelf(TheObject) {
            GetUnity().SendMessage("Camera1", "SetSelf", TheObject);
        }

        function GetSelf(TheObject) {
            GetUnity().SendMessage("Camera1", "GetSelf", TheObject);
        }

        function GetWorld(TheObject) {
            GetUnity().SendMessage("Camera1", "GetWorld", TheObject);
        }

        function SetWorld(TheObject) {
            GetUnity().SendMessage("Camera1", "SetWorld", TheObject);
        }

        function Rotate(TheObject, X, Y, Z) {
            GetUnity().SendMessage("Camera1", "Rotate", TheObject);
        }

        function Move(TheObject, X, Y, Z) {
            GetUnity().SendMessage("Camera1", "Move", TheObject);
        }

        function ShowGetSelf(TheObject) {
            window.external.ShowGetSelf(TheObject);
        }

        function ShowGetWorld(TheObject) {
            window.external.ShowGetWorld(TheObject);
        }

        if (typeof unityObject != "undefined") {
            unityObject.embedUnity("unityPlayer", "WebPlayer.unity3d", 600, 450);
        }
        -->
        </script>
    </head>
    <body style="margin: 0">
        <div class="content">
            <
div id="unityPlayer">
                <
div class="missing">
                    <
a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install!">
                        <img alt="Unity Web Player. Install now!" src="http://webplayer.unity3d.com/installation/getunity.png" width="193" height="63" />
                    </a>
                </div>
            </div>
        </div>
    </body>
</
html
>
In Visual Basic.
Instructions:

Paste the Following code in a Visual Basic Form and place controls as you read them in the code.
 
Imports System.Security.Permissions
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1

Public Sub SetSelf(ByVal TheObject As String, ByVal X As Double, ByVal Y As Double, ByVal Z As Double)
    Dim ObjArr(0) As Object
    ObjArr(0) = CObj(New String(TheObject & "|" & Trim(Str(X)) & "|" & Trim(Str(Y)) & "|" & Trim(Str(Z))))
    WebBrowser1.Document.InvokeScript("SetSelf", ObjArr)
End Sub

Public Sub SetWorld(ByVal TheObject As String, ByVal X As Double, ByVal Y As Double, ByVal Z As Double)
    Dim ObjArr(0) As Object
    ObjArr(0) = CObj(New String(TheObject & "|" & Trim(Str(X)) & "|" & Trim(Str(Y)) & "|" & Trim(Str(Z))))
    WebBrowser1.Document.InvokeScript("SetWorld", ObjArr)
End Sub

Public Sub GetSelf(ByVal TheObject As String)
    Dim ObjArr(0) As Object
    ObjArr(0) = CObj(New String(TheObject))
    WebBrowser1.Document.InvokeScript("GetSelf", ObjArr)
End Sub

Public Sub GetWorld(ByVal TheObject As String)
    Dim ObjArr(0) As Object
    ObjArr(0) = CObj(New String(TheObject))
    WebBrowser1.Document.InvokeScript("GetWorld", ObjArr)
End Sub

Public Sub Rotate(ByVal TheObject As String, ByVal X As Double, ByVal Y As Double, ByVal Z As Double)
    Dim ObjArr(0) As Object
    ObjArr(0) = CObj(New String(TheObject & "|" & Trim(Str(X)) & "|" & Trim(Str(Y)) & "|" & Trim(Str(Z))))
    WebBrowser1.Document.InvokeScript("Rotate", ObjArr)
End Sub

Public Sub Moves(ByVal TheObject As String, ByVal X As Double, ByVal Y As Double, ByVal Z As Double)
    Dim ObjArr(0) As Object
    ObjArr(0) = CObj(New String(TheObject & "|" & Trim(Str(X)) & "|" & Trim(Str(Y)) & "|" & Trim(Str(Z))))
    WebBrowser1.Document.InvokeScript("Move", ObjArr)
End Sub

Public Sub ShowGetSelf(ByVal Positions As String)
    Dim Words() As String
    Words = Split(Positions, "|")
    TextBox1.Text = Words(0)
    TextBox2.Text = Words(1)
    TextBox3.Text = Words(2)
End Sub

Public Sub ShowGetWorld(ByVal Positions As String)
    Dim Words() As String
    Words = Split(Positions, "|")
    TextBox10.Text = Words(0)
    TextBox11.Text = Words(1)
    TextBox12.Text = Words(2)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.WebBrowser1.ObjectForScripting = Me
    WebBrowser1.Navigate("file://G:\Systems\Control3D\ControlWeb\Web\WebPlayer\ThePlayer.html")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Call GetSelf(TxtBoneName.Text)
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Call SetSelf(TxtBoneName.Text, Val(TextBox1.Text), Val(TextBox2.Text), Val(TextBox3.Text))
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Call Moves(TxtBoneName.Text, Val(TextBox4.Text), Val(TextBox5.Text), Val(TextBox6.Text))
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Call Rotate(TxtBoneName.Text, Val(TextBox7.Text), Val(TextBox8.Text), Val(TextBox9.Text))
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    Call GetWorld(TxtBoneName.Text)
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Call SetWorld(TxtBoneName.Text, Val(TextBox10.Text), Val(TextBox11.Text), Val(TextBox12.Text))
End Sub

End Class
Simple as it gets.
Simple As It Gets Policy (SAIG).

When programmers look for information they often stumble on code that has more than what they really need and they might even need only a fraction of the code.

The minimum code approach for tutorial reasons helps programmers as they get what they need faster.

The "Simple As It Gets" policy is much needed in code sharing and can be labeled "SAIG" upon sharing.

SAIG contains no opinions, the less comments possible and only the code strictly needed to teach a given function. To include opinions add another file, page or link to the SAIG Tutorial.

We encourage you to share code with the SAIG Policy and include this policy on your own tutorials codes.