Category Archives: Unity 3D

Using a selfmade C# class in a Unity 3D script

This example was part of a project where I wanted to move an object in Unity 3D triggered by events from a C# class in a DLL created with Visual Studio Express 2010. I am using C# edited with MonoDevelop for Unity 3.5.

There were my steps:

  • Create a DLL with Visual Studio
  • Implement a simple C# class without any events
  • Import the DLL in Unity 3D
  • Use the simple C# class from a Unity script
  • test the C# with the debugger in Unity

Step 1: Create a DLL with Visual Studio

  • Create a new project
  • Choose a C# project: class library
  • Name the new project: MyDllForUnity
  • Rename your class to MyClassForUnity (you can also rename the .cs file)
  • I’ve read that Unity 3D doesn’t support namespaces in user code yet. So I removed the namespace that Visual Studio automatically has added. Remove the namespace line and the opening and closing bracket from the C# file.
  • To allow Unity 3D to ‘talk’ to our class we have to make it of a base type that Unity can understand. This base class is called MonoBehaviour.
  • This base class is stored in a DLL from Unity that is called UnityEngine.dll. First we have to add a reference to this dll in our Visual Studio project.
  • Choose add reference and browse to your unity program files. Select the UnityEngine.dll from the folder

    \Program Files\Unity\Editor\Data\Managed\

  • The base class MonoBehaviour from this dll is defined in a namespace class UnityEngine. Make this namespace known to our code by adding a using statement.

    using UnityEngine;

  • Now we can derive our class from the base class MonoBehaviour.

    public class MyClassForUnity : MonoBehaviour
    {
    }

  • Important: Unity 3.5 doesn’t seem to support DLL made for the .NET framework 4. By default visual studio created the project for .Net framework 4. We have to change our project to be made for a lower version framework. I’ve used 3.5 in this example. If you forget this, you will get the following error message while running your script in Unity:

    Internal compiler error. See the console log for more information. output was:
    Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'MyDllForUnity'.

  • In visual studio. Select the properties of your project. In the Application tab, change Target Framework to: .Net Framework 3.5.
  • Save you project to any location. In the next step we’re going to going our DLL to a unity project. I’ve used D:\Documents\Visual Studio 2010\Projects\MyDllForUnity.

Step 2: Implement a simple C# class without any events

  • First we add a simple implementation to this class. A function to retrieve a string with version information. Our C# class should now look something like this:

    using System;
    using UnityEngine;

    public class MyClassForUnity : MonoBehaviour
    {
    public string GetVersion()
    {
    return "MyClassForUnity version 0.1";
    }
    }

  • Build the DLL (in release).

Step 3: Import the DLL in Unity 3D

  • I assume you already have a Unity 3D.
  • Unity expect external dll’s to be placed in a folder plugins inside the assets folder. It is doesn’t exist yet, create in. It will be something like this now:

    \Documents\New Unity Project\Assets\Plugins

  • Import our DLL as an asset from the Unity menu Import new Asset…. Select the dll from it’s visual studio location. That could be called something like this:

    \Documents\Visual Studio 2010\Projects\MyDllForUnity\MyDllForUnity\bin\Release\

  • Verify that the import was succesfull. In the Projects tab of Unity. Expande ‘Plugins’. You should see our dll MyDllForUnity. When you expand the dll, you should see our class MyClassForUnity.

Step 4: Test the simple C# class from a Unity script

  • Open (Edit) a script where we want to use our dll.
  • Unity has already added our dll to it’s references. You can examine this by looking at the references of the Assembly-C Sharp of the solution of your project (in MonoDevelop)
  • There is no need to add a usingstatement here, because our class is not in a namespace.
  • Add a member variable myClassObject for our class in the class of the scripts. The Unity script is called MyUnityScript in this example.

    public class MyUnityScript : MonoBehaviour
    {
    private MyClassForUnity myClassObject;
    }

  • In the Start function of the unity scripts, create an instance of our class. And call the simple fuction.

  • void Start()
    {
    // create an instance of our class
    myClassObject = new MyClassForUnity();

    // retrieve the version string from our object
    string txt = myClassObject.GetVersion();
    }

  • Build the script. We should not get any error message.

Step 5: Test the C# with the debugger in Unity

  • Put a break point (F9) on the line where we assign our string txt to the string we reveived from the GetVersion function.
  • Attach the debugger to the Unity editor from the MonoDevelop menu: Run -> Attach to process. Then select the Unity Editor.
  • Start the game that included the script by pressing Play in Unity 3D.
  • Switch back to the MonoDevelop editor and notice the yellow arrow on the line where you set the breakpoint. This means that the execution of the script by Unity 3D has arrived on the line with our breakpoint. Perform a single step (F10) to execute the function call to our object.
  • Examine the member variable txt. The contents should be our version string “MyClassForUnity version 0.1”.
  • We have succesfully called a function on a selfmade C# class, and retrieved information from it in our Unity 3D script!

Notes:
When copying a new version of your dll from visual studio folder to unity. Close the MonoDevelop editor.