how I can make some code outside unity ?

hi,

I have question I hope find answer here.

Did i Can make some code outside unity (say DLL extension or C# file )?
i want to modify the external file’s without build EXE file.

You can load managed libraries dynamically. I suggest you define a shared interface class that Unity know about and that your library know about to make it easier to bind. I will give an example of creating and loading a naive math library.

First, we’ll make a new class library in Visual Studio. I call this IMathBox, and will contain my interface. The interface allow us to easily separate the actual implementation, while still maintaining some sort of type safety and you don’t have to write a lot of binding code.

namespace IMathBox
{
    public interface IMath
    {
        int Calc(int a, int b);
    }
}

Compile that into IMathBox.dll and place a copy of that into your unity assets folder so Unity will know about it.

Next, we’ll write two classes that implement IMath. These classes will not part of Unity and you may replace it anytime you want without rebuilding the executable. Make a new class library and call it MathBox. Obviously you’ll need to add IMathBox to your assembly references since we will be implementing IMath. I created two classes, one for multiplication and one for division. Compile this into MathBox.dll and place it in the root folder of your Unity project while developing (Into the folder that hold Assets folder - not inside Assets folder). Make sure to include it as a copy in the root folder when you build a release.

namespace MathBox
{
    public class MultiplyMath : IMathBox.IMath
    {
        public int Calc(int a, int b)
        {
            return a * b;
        }
    }

    public class DivisionMath : IMathBox.IMath
    {
        public int Calc(int a, int b)
        {
            return a / b;
        }
    }
}

Now we need to actually load the dll, and create objects of those classes.

using UnityEngine;
using System.Reflection;

public class AssemblyLoadDemo : MonoBehaviour
{
    string resultMessage;
    void Awake()
    {
        Assembly dll = Assembly.LoadFile("MathBox.dll");
        System.Type multiplyType = dll.GetType("MathBox.MultiplyMath");
        System.Type divisionType = dll.GetType("MathBox.DivisionMath");
        IMathBox.IMath multiply = (IMathBox.IMath)Activator.CreateInstance(multiplyType);
        IMathBox.IMath division = (IMathBox.IMath)Activator.CreateInstance(divisionType);
        resultMessage = string.Format("Multiply: {0}, Division: {1}", multiply.Calc(10, 5), division.Calc(10, 5));
    }

    void OnGUI()
    {
        GUILayout.Label(resultMessage);
    }
}

We start by loading MathBox.dll, then extracting the types “MathBox.MultiplyMath” and “MathBox.DivisionMath”. Then we use the Activator to instantiate objects of those types. Finally for this little demonstration, I call “Calc” on both these objects and make sure I get the expected results. In this case 10*5 should be 50 and 10/5 should be 2, and when I run this on my computer it behaves as expected.

You can also compile code on the fly, if that is more what you are looking for, since you mentioned C# files. I won’t get into that since this already became quite lengty, but you can start looking at the CSharpCodeProvider class.

So yeah, this became 2 dlls. One dll for the interface that is shared between Unity and MathBox, called IMathBox. Then of course there is MathBox that implements IMathBox and Unity uses IMathBox types compile time and MathBox types runtime.