I want to use for processing classes code written in JAVA.
also, I want to use these codes for “Unity for Android” and “Unity for iOS”.
Are there any good way?
I want to use for processing classes code written in JAVA.
also, I want to use these codes for “Unity for Android” and “Unity for iOS”.
Are there any good way?
So if you have code written in java, and you want to have it work in Unity, my recommendation is to retype it in C#. C# is very similar to normal java, so very few changes will need to be made.
example:
This method creates a 2-d array of integers in java, then returns their sum
public int testMethod()
{
int[][] numbers = new int[4][2];
for( int X = 0; X < numbers.length; X++)
for(int Y = 0; Y < numbers[X].length; Y++)
numbers[X][Y] = X + Y;
int output = 0;
for ( int c : numbers)
{ output += c; }
return output;
}
And this code does the same thing in Unity’s C#
public int testMethod()
{
int[,] numbers = new int[4, 2];
for( int X = 0; X < numbers.GetLength(0); X++)
for(int Y = 0; Y < numbers.GetLength(1); Y++)
numbers[X, Y] = X + Y;
int output = 0;
foreach ( int c in numbers)
{ output += c; }
return output;
}
As you can see, they are barely different. You might even be able to just copy the file, change the extension to C#, and then only fixing a small handful compile errors.
If you cross-type into C#, compiling it for iOS and Android is as simple as changing a few checks on the build settings.
Unity can't do Java. Unity scripting is based on Mono (open source version of .NET Framework), hence only understands CLI Code. You can only do scripts in UnityScript (A language similar to JavaScript NOT Java), C# and Boo. No other languages are supported. But there is also no easy conversion of Java <-> C#. While simple constructs work, the more complex ones won't, because C# of course don't use Java Framework classes but it's own Mono/.NET Classes. Java doesn't know of delegates and events (listeners aren't the same ;)), while C# does.
– Tseng@Tseng: Actually UnityScript is more similar to Java than it is to Javascript.
– Eric5h5Not really. The syntax is completely different, by default it's not strong-typed, etc. There are more similarities between C# and Java than Java to UnityScript
– Tseng@Tseng: Yes really. By default UnityScript is very much strongly-typed, it uses classes the same way rather than the prototype stuff in Javascript, etc. Aside from some superficial syntax similarities, UnityScript has little in common with Javascript, and much more in common with C# and Java.
– Eric5h5