- Home /
Is it possible to use Unity Classes (Vector3) in an external c# console application?
Hey everybody,
I am trying to understand if it is possible to use the Unity Classes (such as Vector3, Quaternion, Transform) in a simple C# based console application external to Unity. My current setup is in visual studio I created a .NET Core Console App (Not sure if this is the issue due to .Net Core compatibility) then I added the UnityEngine.dll as a reference.
I wrote this simple Program.cs to test if I could access the variables and while I was writing it the autocomplete and function recognition all works correctly leading me to believe the reference was added correctly.
using System;
using UnityEngine;
namespace ConsoleUnityTest
{
class Program
{
static void Main(string[] args)
{
Vector3 test = new Vector3(0, 0, 0);
Console.WriteLine(test.x);
Console.ReadLine();
}
}
}
When trying to run this program it compiles correctly but gets this error in Visual Studio: System.IO.FileNotFoundException: 'Could not load file or assembly 'UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
Any help around how I could make this setup work or suggestion of an alternative other solution would be much appreciated.
Thanks, Callum
Answer by Bunny83 · Nov 28, 2018 at 05:40 AM
This can be simply answered with NO for several reasons: First and foremost you are not allowed to use any part of the Unity engine outside a Unity project. Second, even if some parts of types defined in the UnityEngine.dll would work, other parts are just managed wrappers which refer to native code. So even most of the Vector3 or the Quaternion struct are written in managed code, there are several methods which are declared external and are defined in the native C++ code.
Most actual classes and component like the Transform component are purely wrapper classes. The whole functionality is defined in the C++ core and pretty much every method and property is actually declared external.
Final note: Please make sure when viewing the repository i've linked above, that this has a special license applied. The repository is there only for reference. You are not allowed to copy or use any of the provided source code in your projects.
I believe Vector3 is being added in new versions of .NET so it will be possible to use it natively. There is actually quite a lot of things co$$anonymous$$g from Unity, but I couldn't find a link with all the changes
This is pretty irrelevant for the question. The Vector3 struct of the .NET framework, even it has a similar / the same purpose, is a completely seperate type and has nothing to do with Unity. There are countless of other implementations of vectors, matrices, quaternions and other types Unity may provide. There are other game engines which are based on C# / $$anonymous$$ono and ship their own types. For example i've written this "Vector3i" before Unity shipped its own Vector3Int struct. I've also created a $$anonymous$$atrix3x3 and a $$anonymous$$atrix5x5 struct because i needed them and Unity doesn't provide those.