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: