- Home /
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?
Answer by _Petroz · Jan 06, 2011 at 11:48 AM
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.
Actually, new Vector3(0, 0, 0) becomes a stack variable since it's a value type. It's just the syntax that is different. As a rule of thumb: All structs = stack. All classes = heap. There are a few exceptions to this though.
Answer by buntu · Jan 07, 2011 at 07:50 AM
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
Answer by Statement · Mar 12, 2011 at 06:13 PM
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[i]; } }
Arrays are defined as such:
int[] integers; // null by default
Arrays are created as such:
integers = new int[size]; // all elements are default(int) which is 0.
Or put together if you want:
int[] integers = new int[42];
Arrays can also be inline populated:
int[] integers = { 4, 5, 6, 2, 4, 1 };
Member variables can be initialized in their definition:
class Foo
{
int Bar = 42;
float Bar2 = 42.0f;
}
Member variables can be set in an initialization list:
Foo foo = new Foo()
{
Bar = 4,
Bar2 = 2.0f
};
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:
void Foo<T>(T value) where T : Component
{
value.name = "Bar";
}
Also you can't derive from generic types. This won't work for example:
class CppExample<T> : T { }
However, this will, since you can't have a type parameters as base classes but you can have base classes that accept type parameters:
class CSharpExample<T> : SomeBaseClass<T> { }
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.
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.
And I guess you could go on with some differences and gotchas but I ran out of time :)
Your answer
Follow this Question
Related Questions
convert file to 2 Answers
Is it possible to test Touch event without IOS? 1 Answer
Converting polygon model to Unity Tree. Is it possible? 3 Answers
java to C# 2 Answers