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()
{
	GUILayout.BeginHorizontal();
	if(GUILayout.Button("set to 0.0"))
	{
		SetCamOffsetFactor("0");
	}
	if(GUILayout.Button("set to 0.5"))
	{
		SetCamOffsetFactor("0.5");
	}
	if(GUILayout.Button("set to 1.0"))
	{
		SetCamOffsetFactor("1");
	}
	GUILayout.EndHorizontal();
}

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

79 thoughts on “tutorial: passing screen offset values from live wallpaper to unity3d with unity2eclipse

  1. Hello,

    just bought unity2eclipse and I’m my LWP keeps crashing with the following message:

    call to OpenGL ES API with no current context (logged once per thread)

    This happens in two cenarios : Rotate screen and Power button.

    Is this a bug? Or is it something that I need to do?

    Ps: Sorry about the bad English.

    • hi nelson,
      as mentioned the LWP functionality is really experimental yet… we’ve noticed some random crashes when unlocking the device too.. what kind of device are you running the wallpaper on?
      tom

  2. Guys, great work! Is there any chance of updating this to work with Unity 3.5.6? Reason I ask is a lot of my Asset Store packages aren’t backward compatible with 3.4.2.

  3. Just purchased this today, how does this work with fragment classes?

    I am trying to use it with mapsforge which changes the main activity to a mapactivity, but it doesnt seem to allow unity to run in a mapactivity. So will it work in a fragment class??

    Thanks

  4. Pingback: demo LWP now online in google play store | appjigger studios

  5. HELLO, Bought. It’s great. (3.4 version)

    But, can’t use almost the whole assets.(unity 3.56 / working ver.)

    I’ll wait to upgrade……

    Thank you.

  6. Hi guys, I´m developing a 3d live wallpaper, well, I have my 3D project finished and works perfectly with my phone, now, I´m developing a interactive menu like the Halloween Monsters and I have the same error, when I try do a action the wallpaper starts to load again and doesn´t work ok.
    Some solution or some advice??
    Thanks

  7. hi there, nice work.
    Im getting some trouble when used on some tablets, or generally when a user turns his device between portrait and landscape and the device allowes that. ( turning the whole screen with the icons..)

    the unity screen get adjusted from landscape to portrait or back, and the View gets disturbed. (like cam is too zoomed)

    I would like to adjust the cam in unity (zoom a little out and in ) depending on the device orientation.. is there any way to handle that like the moving of the camera left and right ?. the orientation in unity (ScreenOrientation ) keeps shouing to portriat. and it would be more nicer to handle that with a handler rather asking all the time in the update function.

    greetings

    • i didn’t test it yet, but you could use the ‘onLWPDesiredSizeChange()’ method that will be called when the orientation changes to pass a value to your unity player..

  8. Great work! but i have one problem: in unity i’ve set the default orientation as auto rotation, but the live wallpaper remains always in portrait mode.. what can i do?

        • i am not sure what why it does not work for you.. our demo wallpaper worked out of the box in all orientations.. you could try to publish your project directly from unity as a “regular” app…

        • just checked our project: the default orientation is set to “portrait” only – the rotation of the player is handled by the android launcher..

          • i did a regular app version of the unity project and the orientation changed correctly.. now i’m doing a test choosing only portrait orientation in unity

          • No, selecting portrait mode in unity still gives me a wallpaper that always stays in portrait mode..

          • ok. i think, i see the problem now.
            live wallpapers work different to stand alone players – the orientation of the device is provided by the OS launcher to the wallpaper service – in our case the wallpaper service forwards this information to the unity player. if you rotate your device all other icons an widgets stay in portrait right? this is how it works with all wallpapers on android phones… if you use a different launcher that supports landscape & portrait it will work than. (i think all tablet launchers support that..)

          • yes, if i rotate my mobile all the icons remain in portrait mode.. so to obtain the landscape mode on a wallpaper i need a tablet or a different launcher?

        • Ok, thanks for your patience! i’ll find some different device to test my wallpaper. If you are interested, i’ll give you the link of the google play page when i’ll upload it (probably tomorrow..)

  9. Can you suggest me how to pass a rotation value (i want to rotate an object in the background when the user swipes trough home pages)?

    • exactly the same way as described in this tutorial but you use the variable to rotate around an axis instate of moving an object… just rebuild this tutorial and play around with the values in unity.. 😉

  10. Hi, I have 2 question, does this work with textured 3d objects and can you create a 3D effect from moving physically the tablet/phone like the gyrospace LW ?

  11. Pingback: tutorial: create a unity3d live wallpaper with native android preferences using unity2eclipse plugin | appjigger studios

  12. Dear Tom,

    Just learning to use this fantastic app. I am trying to create a lwp using Unity v3.4 but having some trouble applying this tutorial.  Basically in unity when I tick the Cam Controller checkbox to pass the screen offset value, the player window does not show whats on the scene window but I if uncheck the Cam Controller checkbox option it seems to display the wallpaper fine. I have throughly followed your instructions.

    Any advice on what I might be doing wrong would be greatly appreciated as this is holding me back big time.

     

    Kind regards

    • it may be some scaling issue and the camera gets out of the screen.. the values that are passed by the wallpaper are between 0 and 1.
      you can use this code snippet directly in unity to simulate the changes and debug your scripts:
      void OnGUI()
      {
      GUILayout.BeginHorizontal();
      if(GUILayout.Button(“set to 0.0”))
      {
      SetCamOffsetFactor(“0”);
      }
      if(GUILayout.Button(“set to 0.5”))
      {
      SetCamOffsetFactor(“0.5”);
      }
      if(GUILayout.Button(“set to 1.0”))
      {
      SetCamOffsetFactor(“1”);
      }
      GUILayout.EndHorizontal();
      }

  13. Hi AppJigger

    My name is Rishabh Agrawal,.I am trying to port unity3d project into Android Live Wallpaper.I am already purchaged pluging for it.When i run my project using eclipse its getting me some error .i am sent  you  source code.Please look in to my code & let me know where i am wrong.

    02-24 20:04:04.398: E/AndroidRuntime(5734): FATAL EXCEPTION: main
    02-24 20:04:04.398: E/AndroidRuntime(5734): java.lang.UnsatisfiedLinkError: Couldn’t load mono: findLibrary returned null
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.Runtime.loadLibrary(Runtime.java:429)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.System.loadLibrary(System.java:554)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.unity3d.player.UnityPlayer.<init>(Unknown Source)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.appjigger.eclipse.WallpaperService.onCreate(WallpaperService.java:18)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.os.Handler.dispatchMessage(Handler.java:99)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.os.Looper.loop(Looper.java:130)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread.main(ActivityThread.java:3687)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.reflect.Method.invokeNative(Native Method)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.reflect.Method.invoke(Method.java:507)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at dalvik.system.NativeStart.main(Native Method)

  14. Hey, great job! It works perfectly. I do not understand C #. Please could you give an example of code to rotate an object to turn the page. please

  15. Hi Tom, I just bought this nice plugin and it’s working fine so far… the only thing I need to be perfect is that the screen rotates instead of offset. I’ve tried to follow your instruction about use the variable to rotate around an axis but since I am not a developer, just a 3D artist, Unity returns error when I change position to rotate in the code:

    using UnityEngine;
    using System.Collections;
    public class CamController : MonoBehaviour {
    public float scaleFactor = 5f;
    private float camOffsetFactor = 0.5f;
    private Vector3 newRotation;
    void FixedUpdate()
    {
    newRotation = new Vector3((camOffsetFactor – 0.5F) * scaleFactor, transform.rotation.y, transform.rotation.z);
    transform.rotation = Vector3.Lerp(transform.rotation, newRotation, 0.02F);
    }
    public void SetCamOffsetFactor(string offset)
    {
    camOffsetFactor = float.Parse(offset);
    }
    }

    I am doing something wrong?

    Thank you… :)

  16. Hi Tom, I just bought this nice plugin and it’s working fine so far… the only thing I need to be perfect is that the screen rotates instead of offset. I’ve tried to follow your instruction about use the variable to rotate around an axis but since I am not a developer, just a 3D artist and a noob coder, Unity returns error when I change position to rotate in the code:

    using UnityEngine;
    using System.Collections;
    public class CamController : MonoBehaviour {
    public float scaleFactor = 5f;
    private float camOffsetFactor = 0.5f;
    private Vector3 newRotation;
    void FixedUpdate()
    {
    newRotation = new Vector3((camOffsetFactor – 0.5F) * scaleFactor, transform.rotation.y, transform.rotation.z);
    transform.rotation = Vector3.Lerp(transform.rotation, newRotation, 0.02F);
    }
    public void SetCamOffsetFactor(string offset)
    {
    camOffsetFactor = float.Parse(offset);
    }
    }

    What I am doing wrong?

    Thank you… :)

    PS: this is the effect I am looking for: 

    http://www.youtube.com/watch?v=njm57Kdv7zo&feature=youtu.be&t=7s

    • hi douglas,
      this works a bit different.. 😉
      1. you should parent your camera to a null object that is located in the “wished” rotation point of the camera. pls take a look at this screenshot: http://blog.appjigger.com/wp-content/uploads/2013/03/screenshot-2.pngmyCam is a child of GameObject which is located in the middle of the cube array (only the transform gizmo is visible here)…

      2. the rotation script is attached to the null object that controls the camera. in this case the “camOffsetFactor” rotates the object around the Z axis (euler.z). the OnGUI buttons simulate the offset of the wallpaper so you can test it directly in unity. you’ll have to change the int and float values maybe to match your objects scale…

      let me know if it works for you!
      tom

      using UnityEngine;
      using System.Collections;

      public class CamController1 : MonoBehaviour {

      private float camOffsetFactor = 0.5f;
      private Vector3 euler;

      void FixedUpdate()
      {
      euler.z = Mathf.Lerp(euler.z, (camOffsetFactor – 0.5F) * 250, 0.01f);
      transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(euler) , 10);
      }

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

      void OnGUI()
      {
      GUILayout.BeginHorizontal();
      if(GUILayout.Button(“set to 0.0”))
      {
      SetCamOffsetFactor(“0”);
      }
      if(GUILayout.Button(“set to 0.5”))
      {
      SetCamOffsetFactor(“0.5”);
      }
      if(GUILayout.Button(“set to 1.0”))
      {
      SetCamOffsetFactor(“1”);
      }
      GUILayout.EndHorizontal();
      }
      }

    • cool. the unexpected symbol in line 3 of your console looks like a character format error.. some kind of utf-8 maybe.. try to copy&paste to a different editor first and recopy than to your code editor… or just retype the code.. 😉

  17. Im do not know anything about Unity 3D. Can you make tutorial how to make animation like yours.

    It easy as this?

    1.get unity 3d

    2.make animation

    3.record it

    4.install to phone

    Please do full tutorial please from A 2 Z

  18. Good afternoon.
    1. I bought a plug-in and faced the same problem: after creating a wallpaper when scrolling the screen does not move the camera (all done as per instructions)
    What can be the problem??
    2. Tell me skinte or script to repeat the animation endlessly (I’m not programmer, please be prudent to-me)
    thanks in advance!

    • hi,
      it pretty sure that your launcher does not support scrolling – none of the market live wallpapers will work correctly than…

      you can put your animation into the update method so it will run forever than..

  19. Hello, I am creating a 3d live wallpaper and when i reach to the “run as android project” it gives me the following message on my phone:”Unfortunately asd has stopped working.” and i have no idea why it happens. I do everything as you have shown on the tutorial.

  20. In the LogCat it gives me this:

    My name is Rishabh Agrawal,.I am trying to port unity3d project into Android Live Wallpaper.I am already purchaged pluging for it.When i run my project using eclipse its getting me some error .i am sent  you  source code.Please look in to my code & let me know where i am wrong.

    02-24 20:04:04.398: E/AndroidRuntime(5734): FATAL EXCEPTION: main
    02-24 20:04:04.398: E/AndroidRuntime(5734): java.lang.UnsatisfiedLinkError: Couldn’t load mono: findLibrary returned null
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.Runtime.loadLibrary(Runtime.java:429)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.System.loadLibrary(System.java:554)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.unity3d.player.UnityPlayer.<init>(Unknown Source)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.appjigger.eclipse.WallpaperService.onCreate(WallpaperService.java:18)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.os.Handler.dispatchMessage(Handler.java:99)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.os.Looper.loop(Looper.java:130)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at android.app.ActivityThread.main(ActivityThread.java:3687)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.reflect.Method.invokeNative(Native Method)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at java.lang.reflect.Method.invoke(Method.java:507)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    02-24 20:04:04.398: E/AndroidRuntime(5734):  at dalvik.system.NativeStart.main(Native Method)

Leave a Reply

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