• 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 maximilianFromJpn · Oct 04, 2011 at 08:32 AM · androidiosjava

How to use processing code written in JAVA.

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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SilverTabby · Oct 04, 2011 at 08:58 AM

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.

Comment
Add comment · Show 6 · 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 maximilianFromJpn · Oct 04, 2011 at 10:21 AM 0
Share

Thanks for your reply!

These Java code look quite complex that I want to use for Unity. ( I know a JAVA little bit... )

So, somehow, I want make Unity can read JAVA code at simple.

I'm looking for how to do it.

avatar image Tseng · Oct 04, 2011 at 11:16 AM 0
Share

Unity can't do Java. Unity scripting is based on $$anonymous$$ono (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 $$anonymous$$ono/.NET Classes. Java doesn't know of delegates and events (listeners aren't the same ;)), while C# does.

avatar image Eric5h5 · Oct 04, 2011 at 11:40 AM 0
Share

@Tseng: Actually UnityScript is more similar to Java than it is to Javascript.

avatar image Tseng · Oct 04, 2011 at 02:19 PM 1
Share

Not 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

avatar image Eric5h5 · Oct 04, 2011 at 02:42 PM 0
Share

@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.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Facebook Platform Policies and Promotions Policies 0 Answers

Empty game objects - too much? 3 Answers

Making an Input Listener Via Touch 1 Answer

Movie texture on mobiles! 3 Answers

motionblur not work on my android phone(android/ios) 1 Answer


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