Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vdelanno · Jan 06, 2011 at 10:22 AM · convert

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 12, 2011 at 06:14 PM 1
Share

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.

avatar image _Petroz · Mar 16, 2011 at 05:00 AM 0
Share

Thanks for the clarification.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

error BCE0022: Cannot convert error, please help 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges