The Unity move - converting application from C++ to C#

Hi all,

I'm about to migrate a 3D application from a proprietary solution to Unity. The codebase is mostly C++, and would need to be rewritten in C#. Using C++ dlls is not an option since one major reason behind the conversion move is the ability to use a webplayer.

I'm wondering whether anyone had to go through the same process and could offer related tips?

Like, I'm pondering how software such as TangibleSoftware's C++ to C# converter might save me days of tedious types/syntax conversion work (we're talking several 100's of files to convert here!), even though it's clear there will also be some manual rewriting (if not refactoring) work needed.

Any feedback on this issue anyone?

I can't speak to the quality of any specific converter but some of the differences I've noticed from my experience so far (please correct me if any of these are inaccurate):

array declirations:

float[] f;

instead of:

float f[];

No fixed size arrays:

float[] f = new float[4];

instead of:

float f[4];

no stack variables:

func( new Vector3(0, 0, 0) );

instead of:

func( Vector3(0, 0, 0) );

Also, strings are very different. I'm not sure if you're using std::string, char*, or a custom class regardless it will probably be different.

I have a similar problem. May i need to convert all my C++ source code on C# or is there a way to avoid that. I have checked the P/Invoke method,(To communicate with my C++ code) but it's not allowed on WebPlayer. Iam considering checking if with an ActivX plug-in it could work...

So any feedback would be great... Thanks

  • C# has support for pointers, but you aren't allowed to use them in safe code (pointers are regarded as unsafe).

  • All class type objects are reference objects. Reference objects are allocated on heap and copied by reference. There is also stackalloc keyword but I doubt you can use it in Unity3D.

  • All struct type objects are value objects. Value objects are allocated on the stack and of course are sent copy-by-value to methods.

  • String is immutable.

In case you want to rather pass the reference of a value to a method, use the ref keyword (& in C++)

void Bar()
{
    int i = 0;
    Bar(ref i);
}

void Foo(ref int value)
{
    value += 42;
}

You can also use the out keyword if you want to explicitly set a value through reference.

void Bar()
{
    int i = 0;
    Bar(out i);
}

void Foo(out int value)
{
    value = 42;
}

varargs are done with params keyword.

void Foobar()
{
    int sum = Sum(5, 3, 1, 4, 6, 2, 4);
}

int Sum(params int[] values)
{
    int result = 0;
    for(int i = 0; i < values.Length; ++i)
    {
        result += values*;*
 *}*
*}*
*```*
*<p>Arrays are defined as such:</p>*
*```*
*int[] integers; // null by default*
*```*
*<p>Arrays are created as such:</p>*
*```*
*integers = new int; // all elements are default(int) which is 0.*
*```*
*<p>Or put together if you want:</p>*
*```*
*int[] integers = new int[42];*
*```*
*<p>Arrays can also be inline populated:</p>*
*```*
*int[] integers = { 4, 5, 6, 2, 4, 1 };*
*```*
*<p>Member variables can be initialized in their definition:</p>*
*```*
*class Foo*
*{*
 *int Bar = 42;*
 *float Bar2 = 42.0f;*
*}*
*```*
*<p>Member variables can be set in an initialization list:</p>*
*```*
*Foo foo = new Foo()*
*{*
 *Bar = 4,*
 *Bar2 = 2.0f*
*};*
*```*
*<p>Generics in C# are not the same as templates in C++ and not near as flexible. Use of generics require you define the interface before hand. This also means you only can pass in types that are derived from Component:</p>*
*```*
*void Foo<T>(T value) where T : Component*
*{*
 *value.name = "Bar";*
*}*
*```*
*<p>Also you can't derive from generic types. This won't work for example:</p>*
*```*
*class CppExample<T> : T { }*
*```*
*<p>However, this will, since you can't have a type parameters as base classes but you can have base classes that accept type parameters:</p>*
*```*
*class CSharpExample<T> : SomeBaseClass<T> { }*
*```*
*<p>Oh I don't remember much about structs in C++ but I think you could derive from them in C++. In C# you can't derive from a struct.</p>*
*<p>Also all types access modifier is internal by default (meaning only the assembly (dll/exe) can access it). All member variables are private by default. There is also a fifth modifier called protected internal meaning the assembly or any derived object has access to the member.</p>*
*<p>And I guess you could go on with some differences and gotchas but I ran out of time :)</p>*