This commit is contained in:
Pavel Dobryakov
2019-04-20 17:48:35 +03:00
parent a0eb1b8398
commit 7b68d1e8a7
7 changed files with 200 additions and 1 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

6
AndroidManifest.xml Normal file
View File

@@ -0,0 +1,6 @@
<service android:name=".WallpaperActivity" android:enabled="true" android:label="@string/app_name" android:permission="android.permission.BIND_WALLPAPER" android:process="${applicationId}.wallpaper">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" />
</service>

View File

@@ -1 +1,25 @@
# Unity-Android-Live-Wallpaper
# Unity Android Live Wallpaper
Run your Unity game as a live wallpaper on Android. Used in production by the app [Fluid Simulation](https://play.google.com/store/apps/details?id=games.paveldogreat.fluidsimfree).
The main reason I open source it is because this implementation has several major bugs that I can't fix and I need help with:
- When you launch lwp preview, press home button and then go back to preview it crashes.
- On many launchers that are not stock it takes small portion of the screen instead of fullscreen.
- Also not taking fullscreen when in landscape mode.
So my goal is to fix these problems and to make this library the best Unity's lwp Android implementation ever, that is also the simplest and free.
# Usage
Export your game as Android Studio project. Then you need to make next steps:
1) Copy content from [AndroidManifest.xml](AndroidManifest.xml) to the project's AndroidManifest.xml, after main activity tag. Should look like this:
<img src="screenshots/manifest_screen.png?raw=true" width="800">
2) Add [WallpaperActivity.java](WallpaperActivity.java) script into the project. You probably would need to change the package name at the top of the script.
3) Make `xml` folder in `res` and add [wallpaper.xml](wallpaper.xml) file there.
<img src="screenshots/xml.png?raw=true" width="800">
Now you can build and run. Go into wallpaper settings of your phone to see it. If you are using Samsung device then install Google Wallpaper app.

163
WallpaperActivity.java Normal file
View File

@@ -0,0 +1,163 @@
package change.packagename.here;
import android.annotation.TargetApi;
import android.app.WallpaperColors;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.WindowInsets;
import android.content.Context;
import com.unity3d.player.UnityPlayer;
class MyUnityPlayer extends UnityPlayer
{
public MyUnityPlayer (Context var)
{
super(var);
}
}
public class WallpaperActivity extends WallpaperService
{
MyUnityPlayer mUnityPlayer;
int mVisibleSurfaces = 0;
@Override public void onCreate ()
{
super.onCreate();
mUnityPlayer = new MyUnityPlayer(getApplicationContext());
}
@Override public void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
@Override public Engine onCreateEngine ()
{
return new MyEngine();
}
@Override public void onLowMemory ()
{
super.onLowMemory();
mUnityPlayer.lowMemory();
}
@Override public void onTrimMemory(int level)
{
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL)
mUnityPlayer.lowMemory();
}
@Override public void onConfigurationChanged (Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
void Log (String message)
{
Log.d("LiveWallpaper", message);
}
class MyEngine extends Engine
{
SurfaceHolder mHolder;
boolean isPreview = false;
@Override public void onCreate (SurfaceHolder holder)
{
Log("Create");
super.onCreate(holder);
isPreview = isPreview();
setTouchEventsEnabled(true);
setOffsetNotificationsEnabled(false);
mUnityPlayer.UnitySendMessage("AppController", "TriggerIsWallpaper", isPreview ? "true" : "false");
}
@Override public void onDestroy ()
{
Log("Destroy");
super.onDestroy();
}
@Override public void onApplyWindowInsets (WindowInsets insets)
{
super.onApplyWindowInsets(insets);
int insetTop = 40;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
insetTop = insets.getStableInsetTop();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
insetTop += 140;
mUnityPlayer.UnitySendMessage("AppController", "ReceiveWindowInset", Integer.toString(insetTop));
}
@TargetApi(Build.VERSION_CODES.O_MR1)
@Override public WallpaperColors onComputeColors ()
{
Color color = Color.valueOf(Color.BLACK);
return new WallpaperColors(color, color, color);
}
@Override public void onSurfaceCreated (SurfaceHolder holder)
{
Log("SurfaceCreated");
super.onSurfaceCreated(holder);
mHolder = holder;
}
@Override public void onSurfaceChanged (SurfaceHolder holder, int format, int width, int height)
{
Log("SurfaceChanged, width: " + width + ", height: " + height);
super.onSurfaceChanged(holder, format, width, height);
mHolder = holder;
mUnityPlayer.displayChanged(0, mHolder.getSurface());
}
@Override public void onVisibilityChanged (boolean visible)
{
Log("VisibilityChanged, isPreview: " + isPreview + ", visible: " + visible);
super.onVisibilityChanged(visible);
if (visible)
{
mVisibleSurfaces++;
if (mHolder != null)
mUnityPlayer.displayChanged(0, mHolder.getSurface());
mUnityPlayer.windowFocusChanged(true);
mUnityPlayer.resume();
mUnityPlayer.UnitySendMessage("AppController", "TriggerVisible", isPreview ? "true" : "false");
return;
}
mVisibleSurfaces--;
mVisibleSurfaces = Math.max(mVisibleSurfaces, 0);
if (mVisibleSurfaces == 0)
{
mUnityPlayer.displayChanged(0, null);
mUnityPlayer.windowFocusChanged(false);
mUnityPlayer.pause();
}
}
@Override public void onSurfaceDestroyed (SurfaceHolder holder)
{
Log("SurfaceDestroyed");
super.onSurfaceDestroyed(holder);
}
@Override public void onTouchEvent (MotionEvent event)
{
super.onTouchEvent(event);
mUnityPlayer.injectEvent(event);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 KiB

BIN
screenshots/xml.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

6
wallpaper.xml Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:thumbnail="@mipmap/ic_launcher_foreground">
</wallpaper>