Browsing articles in "Blog"

How to create a Native Android Plugin for Unity

Mar 18, 2014   //   by Lorenzo Nuvoletta   //   Android, Blog, Mobile, Unity  //  No Comments

Building a Unity plugin for Android in Java is pretty straightforward, although if you have never done it, this is what you need to know.

Assuming that you have Unity installed on your machine and you have already started a project, download and install the Android SDK with the ADT Bundle, this also contains Eclipse which will be used to create the JAR file.

- Set up the project

Open up Ecplise and create a new Android Application Project, name it as you wish, make sure to use the same package name you used in Unity Player Settings.

* Just as in Unity:

And include the “classes.jar” that you can find in this folder:

“C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin\classes.jar”

To include a JAR right click on the project and click “Properties” then go to “Java Build Path” tab “Libraries” and click on “Add External JARs…

- Create the bridge

This is the bridge class, in this example there is a method with an int return and another method that shows how to handle parameters injection and callback to Unity.

To generate a JAR file out of this file, right click on it, select “Export” then export as “JAR file”.

- Unity side

In Unity create a folder in Assets named Plugins and a subfolder in Plugins named Android (Assets/Plugins/Android), in this last one put the JAR file you just created.

In the same folder create an AndroidManifest.xml as described below:

Also create a C# script and attach it to the Main Camera.

This script calls a method on click passing some parameters, this call back a method in Unity which then calls again a method in Java to return an integer.

You will not have to go this route every time, although I wanted to expose all the possibilities.

It should look like this:

In the above script there is a GUI Text that displays the message, simply create a Game Object Gui Text and drag it in the Script field using the Unity Inspector.

That’s all, if everything went fine you should be able to click Build and Run in Unity and see the app on your phone.

Create an Air ANE for Android to dispatch background tasks

Jul 15, 2013   //   by Lorenzo Nuvoletta   //   Air, Android, AS3, Blog, Mobile  //  5 Comments

If you are using DIRECT or GPU mode with Adobe Air you probably have already noticed that once the app goes in background it stops firing any kind of Event. This means that any task that dispatches Event will stop working.

Luckily it is still possible to dispatch Events from an ANE.

In this post I am going to create an ANE for Android that dispatches an Event every second which triggers a callback on the AS3 side.

Assuming that you have already downloaded the Android SDK, open Eclipse and create a new Android Application Project naming it BackgroundTasker, also if you want change the Package Name, I changeds mine to com.lorenzonuvoletta.backgroundtasker.

Click Next a bunch of times and you should have your project setup.

You can also go ahead and delete the file MainActivity.java in src folder, you don’t need that.

Also from your Air SDK folder copy the file “\lib\android\FlashRuntimeExtensions.jar” into your “BackgroundTasker\lib\” folder and add it to your project by, Richt Click on porject name -> Properties -> Java Build Path -> Add External JARs…

 

It’s time to create the first Class, right click on the package in “src” and select Class, name it OneSecondTask and use FREFunction as interface.

This class will simply dispatch an Event every second, here is the code for it

Create a new Class and name it BackgroundTaskerContext, and choose as Superclass FREContext, this class will map to the previously created function, this is the code for it:

And finally create the class that handles the context, make sure to use FREExtension as the interface, this is the code:

At this point you should not have any error, export the JAR file by clicking on File -> Export -> Java -> JAR file, and when it asks to select the resources, simply select the “src” folder.

Also create an XML file that looks like this:

Almost there, the last piece is the ActionScript class that will glue the Extension, in this class you start the tasker by passing a callback method which is going to be called every second by the ANE, this is the code for it:

Ok now it’s time to generate the ANE and SWC files.

Open terminal and write:

  •  set PATH_FLEXSDK=”C:\Program Files (x86)\FlashDevelop\Tools\flexsdk” (Use whatever path you have for the Flex SDK, make sure it also contains the Air SDK)
  • %PATH_FLEXSDK%\bin\compc -source-path as -include-classes com.lorenzonuvoletta.backgroundtasker.BackgroundTasker -swf-version 20 -external-library-path %PATH_FLEXSDK%\frameworks\libs\air\airglobal.swc -output com.lorenzonuvoletta.backgroundtasker.BackgroundTasker.swc
This will generate a SWC file, open the SWC like it was a ZIP file and extract the file “library.swf” and put that in the same location where you saved your JAR file.
In my case the folder is named “jar”.
  • %PATH_FLEXSDK%\bin\adt -package -target ane com.lorenzonuvoletta.backgroundtasker.BackgroundTasker.ane extension.xml -swc com.lorenzonuvoletta.backgroundtasker.BackgroundTasker.swc -platform Android-ARM -C jar .
This will create an ANE.
Now that you have the SWC and the ANE, just add them to the “lib” folder of your project.
Usage example:

When you compile make sure you add this to your application XML:

Also if you are using FlashDevelop make sure you add “-extdir lib” in the Package.bat where it calls adt.

If you are interested on how to create an iOS ANE Extension, look at this other post.

Create a cross platform game with Cocos2DX and C++

Feb 18, 2013   //   by Lorenzo Nuvoletta   //   Android, Blog, C++, Cocos2DX, iOS, Mobile, XCode  //  1 Comment

If you like to code in C++ and you are interested in building a cross platform game, there is a library out there you can use, it is called Cocos2DX.

The cool thing about it is that “theoretically” you write the code once and it should work on any platform… In practice it gets a little complicated, but with a bit of stubbornness anything is possible.

There is a big list of games already using it and I guess now one of the most famous is Hero Academy (which I am not sure why they have not released on Android), also Glu mobile seems to like using it.

You will not be able to find huge amounts of tutorials online, and the official website is confusing at best, although there is one that is pretty good, Paralaxer.

The tutorial is free, and I believe for a few dollars you can buy the entire source code of the game, worth checking out.

For now I will limit this guide to explaining how to create an executable for Windows, got that it shouldn’t be too “difficult” to also deploy on Mac, Linux, Android and iOS.

First things first

  1. Make sure to download the Cocos2dX library and uncompress it somewhere you will remember.
  2. Install Microsoft Visual C++ 2010 Express.

Setting the Folder

Create a folder to contain the project, in my case the folder name is TestCocos2Dx so that the class files and the resources can stay outside of your Windows project, in this folder also copy “cocos2dx” and “CocosDenshion” folder that you find in the Cocos2dx library you downloaded before.

Also Download this ZIP (Contains Classes and Resources that you can modify) and uncompress it in the same folder.

Setting the Project

Open Visual Studio C++ and create a new Project.

Add the existing projects of Cocos2DX and CocosDenshion to it by clikcing on File->Add->Existing Projects… and loading the vcxproj in the proj.win32 folder.

At this point you can delete everything from the MyGame_Windows project except for External Dependencies. Although add the classes contained in the “Classes” folder and add 2 new classes:

Main.cpp

Main.h

Solution Properties

Right click on the Solution->Properties and make sure that Project MyGame_Windows has all the dependencies checked out.

 Project Properties

In Project->Properties, under Common Properties -> Framework and References, add the references of libcocos2d and libCocosDenshion and set to both “Reference Assembly Output” to False.

Configuration Properties

General:

  • Output Directory = $(SolutionDir)$(Configuration).win32\
  • Intermediate Directory = $(Configuration).win32\

Debugging

  • Working Directory = $(ProjectDir)..\..\Resources

C/C++ General

  • Additional Include Directories
  • ..\..\cocos2dx\platform\third_party\win32\OGLES
  • ..\..\cocos2dx\platform\win32
  • ..\..\cocos2dx\kazmath\include
  • ..\..\cocos2dx\include
  • ..\..\cocos2dx
  • ..\..\Classes

C/C++ Precompiled Headers

  • Precompiled Header, make sure nothing is writtein inside.

Linker Input

  • Additinal Dependencies add opengl32.lib, glew32.lib and libcocos2d.lib.

 

At this point you should be able to run the test without a problem.

Next posts I will cover how to use the same code to compile to Android and iOS.

Pages:12345»
July 2014
M T W T F S S
« Mar    
 123456
78910111213
14151617181920
21222324252627
28293031