Hi,
i’m trying to marry the android.service.wallpaper.WallpaperService with the com.unity3d.player.UnityPlayer class. I’ve read that once upon a time there was an UnityWallpaperService included, but it seems to be dropped (Any official news on that?). There are some good tutorials for Live Wallpapers around, so i thought it could be worth to give it a try.
Did somebody figure out how it could be possible to wrap around a UnityPlayer instance - detached from the Activity? I’ve tried various things but since i’m new to Eclipse/Java its kind of hard to figure out whats the issue here. UnityPlayer.draw() seems to be unimplemented, other wild guesses lead to crashes (i suppose).
Any information on LiveWallpaper’s and Unity3D 4? Is it maybe something stupid to try at all since its not possible without the Activity?
public class WallpaperTestService extends WallpaperService {
[..]
private UnityPlayer mUnityPlayer;
@Override
public Engine onCreateEngine() {
mUnityPlayer = new UnityPlayer(this);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
// do i need to call setContentView()?
return new WallpaperTestEngine();
}
private class WallpaperTestEngine extends Engine {
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
public void run() {
draw();
}
};
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
// works!
canvas.drawARGB(255, 255, 255, 255);
// draw() not implemented?
// mUnityPlayer.draw(canvas);
// kind of crashes
// canvas.drawBitmap(mUnityPlayer.getDrawingCache(), canvas.getMatrix(), paint);
}
[..]
}
[..]
Here is the autogenerated code of the Java Unity Activity:
import com.unity3d.player.*;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class WallpaperTestActivity extends Activity
{
private UnityPlayer mUnityPlayer;
// UnityPlayer.init() should be called before attaching the view to a layout.
// UnityPlayer.quit() should be the last thing called; it will terminate the process and not return.
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
View playerView = mUnityPlayer.getView();
setContentView(playerView);
playerView.requestFocus();
}
protected void onDestroy ()
{
super.onDestroy();
mUnityPlayer.quit();
}
// onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
if (isFinishing())
mUnityPlayer.quit();
}
protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// Pass any keys not handled by (unfocused) views straight to UnityPlayer
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return mUnityPlayer.onKeyDown(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
return mUnityPlayer.onKeyUp(keyCode, event);
}
}