简体   繁体   中英

How to make App Widgets with Unity

We want to create a Unity application include App Widgets. But we can not find that sample.

App Widgets https://developer.android.com/guide/topics/appwidgets/index.html

We know how to make App Widgets in native apps. But We do not know how to include App Widgets in Unity application.

We have two questions.

First, I want to know how to retrieve the contents saved in Unity's PlayerPrefs from the Android side.

Second, how to include AppWidgets in the Unity application.

We tried the sample to show Toast. But I did not know how to include AppWidgets.

First, I want to know how to retrieve the contents saved in Unity's PlayerPrefs from the Android side.

"On Android data is stored (persisted) on the device. The data is saved in SharedPreferences. C#/JavaScript, Android Java and Native code can all access the PlayerPrefs data. The PlayerPrefs data is physically stored in /data/data/pkg-name/shared_prefs/pkg-name.xml." So guess you fetch it from there?

Source: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Second, how to include AppWidgets in the Unity application.

I believe the solution in the link below might help you do that. Not entirely sure but I'd assess it will.

https://forum.unity.com/threads/using-unity-android-in-a-sub-view.98315/

※ Unity Version : 2018.2.17f1

Pre-Task

  • export Android build, with Export Project & Development Build checked
  • then you will get a folder contains .gradle .idea build gradle...
    ( I gotta call this folder as Project Folder )
  • open Android Studio and select import project (Gradle, Eclipse ADT, etc.)
  • in file explorer, you should pick up the Project Folder

1. How to retrieve Unity PlayerPrefs from Android side

you can find Project Folder/java/UnityPlayerActivity.java in project explorer on Android Studio. this class performs Unity application on Android side, thus you should write code for retrieving PlayerPrefs in here. example code is below:

public class UnityPlayerActivity extends Activity 
{
    protected SharedPreferences mPlayerPrefs = null;

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        // generated code

        this.mPlayerPrefs = this.getSharedPreferences(this.getPackageName() + ".v2.playerprefs", Context.MODE_PRIVATE);
    }

    // generated code
}

rest of job is all about using SharedPreferences, not the PlayerPrefs. Yeap.
for example...

int a = this.mPlayerPrefs.getInt("key", -1);
if(a == -1)
{
    Log.d("MyLog", "getInt(key) failed !");
}
else
{
    // some code
}

2. How to include AppWidget in Unity Application

it is very simple. just in the state with your Project Folder open on Android Studio, select File -> New -> Widget -> App Widget and you will see Widget wizard. after the wizard, your Unity Application can be located in home screen as widget. rest of job is all about widget programming... ;D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM