tutorial: create a unity3d live wallpaper with native android preferences using unity2eclipse plugin

a lot of users ask how to add a preference activity to their live wallpapers created with the unity2eclipse plugin. this short tutorial will demonstrate how to toggle between black and white camera background color in your 3d wallpaper. in order to understand the basics of our plugin please read our two basic tutorial first:
passing screen offset values from live wallpaper to unity3d with unity2eclipse
Android Live Wallpapers with Unity3d

1. create a new xml layout file that is going to contain the settings elements:



in our (demo) case it’s going to be just a simple CheckBoxPreference:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
        android:summary="turn off to use white color..."
        android:title="use black as background" />

2. create a new java class in the src folder:

create new class: MyPreferencesActivity

create new class: MyPreferencesActivity.java

this new class will extend the PreferenceActivity and load our previously created layout:

public class MyPreferencesActivity extends PreferenceActivity  {
    protected void onCreate(Bundle savedInstanceState) {

3. find the wallpaper.xml file in res/xml folder:

find the wallpaper.xml


and add the line:


so our wallpaper “knows” that it has an activity that handles its preferences. (replace¬†com.appjigger.advLWP.MyPreferencesActivity with your own class here). it should look like this than:

<?xml version="1.0" encoding="UTF-8"?>

4. modify the manifest.xml: add the new activity to the application node:

      android:name=".MyPreferencesActivity" android:exported="true" > 

5. prepare your unity project to receive vars from the native android wallpaper service. in our case it is going to be a simple C# public method that changes the camera background color of the player:

public void SetCheckBoxValue(string myCheckBoxVal)
    camera.backgroundColor = myCheckBoxVal == "true" ? Color.black : Color.white;    

6. update the wallpaper service class in order to push the saved settings to the unity player each time the visibility of the LWP changes. find the main class in src:

find your wallpaperService class

find your wallpaperService class

find the onLWPVisibilityChanged method and add this code:

public void onLWPVisibilityChanged(boolean visible)
	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
	boolean myCheckbox = prefs.getBoolean("myCheckBox", true); 
	UnityPlayer.UnitySendMessage("myCam", "SetCheckBoxValue", String.valueOf(myCheckbox));

following this tutorial will add a “Settings…” button to your LWP preview that opens a new settings activity:




this quick tutorial shows a very basic usage of preferences in android/unity. however you can crate a very complex settings activities even using your own widgets as we have done in our popular (none unity3d) LWP – WP clock:



add adMob banner to your unity3d game using unity2eclipse

a great benefit about using unity2eclipse is the possibility to extend your unity3d projects with native android elements like buttons or layouts. as a practical example we decided to show you the integration of an adMob banner in your game.

first of all you have to create an eclipse project – the easiest way to do that is to use our unity2eclipse plugin of course! ūüėČ click here to learn how to do that.

once created we add the adMob SDK to the project:
right click on the project name, find Properties in the menu (it is located at the very bottom). selecting the properties will open a settings window Рnavigate to Java Build Path -> Libraries -> Add External JARs… select the previously downloaded adMob .jar file and click OK.

Java Build Path -> Libraries -> Add External JARs…

the ugly thing about the latest android SDK update is the requirement to copy the adMob .jar  file manually into the libs folder of your project too. i am not sure if this is a bug or a feature, but adding it twice is the only solution that worked for me. just drag & drop it from your finder/windows explorer into eclipse.

additional location of the adMob .jar file

lets start with the more fun part of the tutorial and create a layout file that contains a placeholder for our unity player and the adMob banner – we’ll name it unitylayout.xml and store in res/layout folder of our project.

create a new layout xml file here

the contents of the XML layout:

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" >

        android:orientation="vertical" />

        ads:loadAdOnCreate="true" />


(replace the “useYourIdHere” with your adMob ID ūüėČ )
we’ll use a FrameLayout – it will allow us to position widgets on top of each other – in our case the banner view on top of the unity player (learn more about android layouts)

in the next step we’ll adjust the .java class file:
the established way to implement an unity player here is to extend the UnityPlayerActivity:

public class myUnityProject extends UnityPlayerActivity

its fine, but it wont allow you to add any native android widgets to your app. we’ll change it, extend an Activity and push the unity player into the LinearLayout placed in our layout xml file. you can replace the whole content of the class with this code:

public class myUnityProject extends Activity
	protected void onCreate(Bundle savedInstanceState) 
		LinearLayout playerHolder = (LinearLayout) findViewById(R.id.unity);
		UnityPlayer myPlayer = new UnityPlayer(this);
        myPlayer.init(1, false);

the last file that we need to edit is the Manifest.xml located in the root of our project. adMob SDK requires some permissions in order to be displayed correctly:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >

        android:targetSdkVersion="15" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <meta-data android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" android:value="true" />


i hope that this little tutorial will help you to understand how to extend the functionality of your unity android game and to benefit from adMob integration.

tutorial: passing screen offset values from live wallpaper to unity3d with unity2eclipse

remember! creating android live wallpapers with unity2eclipse is experimental and works with unity 3.4.x!

one great thing about android live wallpapers is the possibility to interact with touch events generated when the user swipes through the home screens – this little tutorial will show you how to move (smoothly) the camera on the X-axis:

rename your camera to “myCam” and add this C# script to it:

using UnityEngine;
using System.Collections;

public class CamController : MonoBehaviour {

	public float scaleFactor = 5f;
	private float camOffsetFactor = 0.5f;
	private Vector3 newPosition;

	void FixedUpdate()
        newPosition = new Vector3((camOffsetFactor - 0.5F) * scaleFactor, transform.position.y, transform.position.z);
        transform.position = Vector3.Lerp(transform.position, newPosition, 0.02F);

	public void SetCamOffsetFactor(string offset)
		camOffsetFactor = float.Parse(offset);


than find your java class in eclipse…

…and update the onLWPOffsetsChanged method with UnitySendMessage() where:

  • myCam is the name of your unity3d object (in this case our camera);
  • SetCamOffsetFactor is the name of the public method in your C# script;
  • String.valueOf(xOffset) is the current screen offset as string (UnitySendMessage) supports string values as parameter only;
public void onLWPOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset)
	UnityPlayer.UnitySendMessage("myCam", "SetCamOffsetFactor", String.valueOf(xOffset));

it is always nice to have some easy way to test your LWP in your unity3d editor ūüėČ in this case you can just add this lines into the C# script:

void OnGUI()
	if(GUILayout.Button("set to 0.0"))
	if(GUILayout.Button("set to 0.5"))
	if(GUILayout.Button("set to 1.0"))

click here to download the demo wallpaper from the google play store

Unity2Eclipse and Vuforia

With a  few steps you can use our plugin to swap a unity project + vuforia to Eclipse.

1.You need a working UnityProject + Vuforia.
2.Just follow our instruction to export it to Eclipse.
All you have to do now in Eclipse is:
3. Copy the QCAR folder from  Library
to your assets  folder from your custom Android project.

4. Add QCAR.jar and QCARUnityPlayer.jar to Java Build Path.
You find these at your Library Folder /plugins

5. Also copy both jar files into libs folder.

6. Now copy your Manifest from Library to custom Android Project.

7. At least clean and build your project “Project”–> “Clean” and “Project” –> “Build”.
In some cases you need to fix your properties. Just right click on both projects and select ‚ÄúAndroid Tools‚ÄĚ –> ‚ÄúFix Project Properties‚ÄĚ.
Now choose “Run” –>”Run As” –>”Android Application” from the Eclipse menubar.


At the moment you have to do step 3 + normal export each time when you update your Unity project .