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
and
Android Live Wallpapers with Unity3d

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

my_prefs_layout.xml

my_prefs_layout.xml

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" >
    <CheckBoxPreference
        android:key="myCheckBox"
        android:defaultValue="true"
        android:summary="turn off to use white color..."
        android:title="use black as background" />
</PreferenceScreen>

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  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.my_prefs_layout);
    }
}

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

find the wallpaper.xml

wallpaper.xml

and add the line:

android:settingsActivity="com.appjigger.advLWP.MyPreferencesActivity"

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"?>
<wallpaper
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:thumbnail="@drawable/icon"
    android:settingsActivity="com.appjigger.advLWP.MyPreferencesActivity"
    android:description="@string/app_name">
</wallpaper>

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

<activity
      android:name=".MyPreferencesActivity" android:exported="true" > 
</activity>

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:

wp1

wp2

wp1a

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:

 

 

10 thoughts on “tutorial: create a unity3d live wallpaper with native android preferences using unity2eclipse plugin

  1. i have a few questions: can i send a message to a unity javascript script or it has to be a c# script? do i have to “convert” every value to a String value in order to communicate with unity player? can i send a message to modify static variables in unity?

    • 1. i am not really familiar with javascript in unity, but it may work..
      2. yep – you can pass only string values to unity;
      3. no. you have to use a setter method to modify variables;

      • I need to make 6 different checkboxes in my settings activity but i need that when one is selected the other ones are automatically deselected. I tried to write something like
        if (myCheckbox1 == true) {
        myCheckbox2 = false;
        myCheckbox3 = false;

        }

        but it didn’t work. Do i have to set something on “my_prefs_layout.xml”? Thanks in advance, Tom!

        • java is not that simple as javascript… 😉 you’ll have the implement a listener in order to get status of the check box as it changes – something like this:

          public class MyPreferencesActivity extends PreferenceActivity {
          CheckBoxPreference chkbx1 , chkbx2;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          addPreferencesFromResource(R.xml.my_prefs_layout);

          chkbx1 = (CheckBoxPreference)findPreference("myCheckBox");
          chkbx2 = (CheckBoxPreference)findPreference("myCheckBox2");

          chkbx1.setOnPreferenceClickListener(new OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
          if(chkbx1.isChecked()){
          chkbx2.setChecked(false);
          } else {
          chkbx2.setChecked(true);
          }
          return false;
          }
          });
          }
          }

  2. Does unity2eclipse work with Unity version 4.0 . I want to buy the plugin for live wallpaper creation would like to know if it supports Unity3d Version 4.0 before buying .

     

    Waiting for your reply,

    Thanks in Advance,

    Tuhin Bhatt

Leave a Reply

Your email address will not be published. Required fields are marked *