An introduction to Unity3D for Android

We’re going to take a short break from the Seven Languages updates to talk about Unity 3D, as I’m working on a short demo project and would like to chronicle some of the early highlights as well as bumps in the road.

Unity 3D is an incredible tool geared solidly towards making 3D game content. For years Unity lived in obscurity, targeting its output strictly to the largely unheard of Unity Web player, a competitor to Flash that never really took off. Unity has always been interesting, but its reach was never truly compelling until recently. What’s changed? Unity now has the ability to compile projects to native applications that target iOS and Android in much the same way as Adobe’s CS5 can create native applications for iOS and Android that run swfs using the AIR packager.

Most of what I’ve done with Unity involves mashing together various different tutorials and making them behave in new and interesting ways. This is surprisingly easy to do. What’s slightly harder is making sure it all works when you’re ready to publish to an APK file. I’ll break this down a bit and highlight some of the things that can go wrong.

Certain standard library code won’t compile on Android. This is typically going to be code in JavaScript files, as JS allows and encourages dynamic object referencing while Android an iOS does not allow it in any form no matter the language you choose. For this reason alone I’d suggest to never use JS if developing for iOS or Android. Code that doesn’t generate any immediate errors as you’re writing it may be non-compilable. One such error was easy to find and fix:

Assets/Standard Assets/Scripts/DragRigidbody.js(31,22): BCE0019: ‘isKinematic’ is not a member of ‘UnityEngine.Component’. (Filename: Assets/Standard Assets/Scripts/DragRigidbody.js Line: 31)

I found the solution to the issue here

Other errors were more insidious. One in particular was evident in the Detonator demo buried in the example project on this page.

BCE0019: ‘size’ is not a member of ‘UnityEngine.Component’.
BCE0019: ‘detail’ is not a member of ‘UnityEngine.Component’.

If you look at the relevant code in light of the restriction against dynamic coding the problem is obvious.

var offsetSize = currentDetonator.GetComponent("Detonator").size / 3;
var hitPoint = hit.point + ((Vector3.Scale(hit.normal, Vector3(offsetSize,offsetSize,offsetSize))));
var exp : GameObject = Instantiate (currentDetonator, hitPoint, Quaternion.identity);
exp.GetComponent("Detonator").detail = detailLevel;
 
// currentDetonator.GetComponent returns GameObject.  
// GameObject does not have a .size or a .detailLevel.
// variable.

So you’d think this could be solved by just casting the return value of GetComponent into Detonator and we’ll be fine. Unfortunately there’s an entirely different problem when you do that…

BCE0018: The name “Detonator does not denote a valid type”

But why is this? Why would it suddenly be invalid on Android but not when compiling for the web player? That has to do with the order in which scripts are compiled. This link details some of the pitfalls that can be related to script compilation order. This particular issue was a result of the fact that the JS file was compiled *before* the Detonator and had no reference to the Detonator scripts. I could solve this by re-arranging the locations of the script files, or by re-writing the JS to be C#. I opted for the latter. This solved my problems.

Another issue I ran into, that was harder to diagnose, is that in one of my scenes I’d created a terrain, as I wanted to play with the terrain painting tools. Unfortunately, every time I tried to load the scene that had the terrain my apk would crash. This is because Terrains are unsupported on Android. This means that you won’t be using any of the pretty terrain painting tools if you’re targeting mobile.

Don’t let any of these things stop you from exploring Unity, however. These have been fairly minor setbacks, and the results of a successful compile are nothing short of incredible. Unity’s particular deal with the Devil is that it gains incredible ease of content creation for performance, but depending on how you look at it Unity might just have gotten one over on Lucifer. Performance may not be up to the level of C++ but it is definitely respectable, and the tools for creating and managing projects are truly amazing.

I’ll be sure to keep you posted as this progresses.

Tags: , , ,

  1. #1 written by waltergs August 4th, 2011 at 13:42

    Do you have the file DetonatorTest in C#?

  2. #2 written by The Horseman August 7th, 2011 at 10:22

    @ waltergs
    I did convert the detonator test to C# at one point, but the computer on which I did this is no more.

    I can tell you that the detonator test runs dog slow on my phone. It’s impressive that it works, but it’s not suitable for a mobile game in its current state. I haven’t dug into the workings of Detonator to see if there are some obvious efficiencies to be made, but it’s possible.

  3. #3 written by Nisar Ahmed May 31st, 2012 at 06:17

    Hey, i am developing my game based on the demo of 3rd person shooter and making it complete for android, but when i use to run it on android it gives the above errors ?? can you help me solving those errors ?? please

  4. #4 written by The Horseman May 31st, 2012 at 06:26

    Which errors? There are several and they are caused by different things.

  5. #5 written by Nisar Ahmed May 31st, 2012 at 06:33

    I need those c# codes, if you can arrange them from some where, that will be a greater help for me thanks 🙂

  6. #6 written by Nisar Ahmed May 31st, 2012 at 06:35

    i just wanted to convert the whole demo project for the android, and ran into the series of errors in scripts ?? don’t knw why the hell those errors came from ?? but when i read your blog, i got to know that some libraries dont work on android ?? so how do i make this project runnable ??

  7. #7 written by Nisar Ahmed May 31st, 2012 at 06:37

    Please do help me … I am in real big problem ??

  8. #8 written by Nisar Ahmed May 31st, 2012 at 06:39

    Your blog really helped me in understanding the problem, but for solving them i need your help ?? As you already had figured out these problems and pitfalls and also managed to remove them, i need you to help me ?? would you please ??

  9. #9 written by Nisar Ahmed May 31st, 2012 at 06:43

    Ok i am showing you the error list ?? would you like to help me then?

  10. #10 written by The Horseman May 31st, 2012 at 18:32

    Hi,

    The project that I converted back then was on an old computer that I no longer have access to, so I cannot send a C# version of the code. I no longer have it.

    What errors do you receive? If it is this :
    “BCE0018: The name “Detonator does not denote a valid type””

    And if you are mixing JS and C# then you must make sure you are using only C#. It will help solve the problem you face.

  11. #11 written by Nisar Ahmed May 31st, 2012 at 22:37

    Thanks for the reply

    yeah detonator and many more errors regarding to file missing and size variable is not in the file are there.

    Ok then how much time would it take to convert the whole JS files in C# as i am a newbie ??

  12. #12 written by Nisar Ahmed May 31st, 2012 at 22:42

    You told in the post that you have opted two ways, why don’t you guide me in any of the ways you opted, this will be the optimum help from you ?? i really need help from your side to complete my project before the timeline ends ?