简体   繁体   English

如何使用 Unity 制作应用小部件

[英]How to make App Widgets with Unity

We want to create a Unity application include App Widgets.我们要创建一个包含 App Widgets 的 Unity 应用程序。 But we can not find that sample.但是我们找不到那个样本。

App Widgets https://developer.android.com/guide/topics/appwidgets/index.html应用小工具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.但是我们不知道如何在 Unity 应用程序中包含 App Widgets。

We have two questions.我们有两个问题。

First, I want to know how to retrieve the contents saved in Unity's PlayerPrefs from the Android side.首先,我想知道如何从Android端检索保存在Unity的PlayerPrefs中的内容。

Second, how to include AppWidgets in the Unity application.二、如何在Unity应用中包含AppWidgets。

We tried the sample to show Toast.我们尝试使用示例来展示 Toast。 But I did not know how to include AppWidgets.但我不知道如何包含 AppWidgets。

First, I want to know how to retrieve the contents saved in Unity's PlayerPrefs from the Android side.首先,我想知道如何从Android端检索保存在Unity的PlayerPrefs中的内容。

"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." “在Android上,数据存储(持久化)在设备上。数据保存在SharedPreferences中。C#/JavaScript、Android Java和Native代码都可以访问PlayerPrefs数据。PlayerPrefs数据物理存储在/data/data/pkg-名称/shared_prefs/pkg-name.xml。” So guess you fetch it from there?那你猜你是从那里拿的?

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

Second, how to include AppWidgets in the Unity application.二、如何在Unity应用中包含AppWidgets。

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/ https://forum.unity.com/threads/using-unity-android-in-a-sub-view.98315/

※ Unity Version : 2018.2.17f1 ※ 统一版本: 2018.2.17f1

Pre-Task任务前

  • export Android build, with Export Project & Development Build checked导出 Android 构建,选中Export ProjectDevelopment Build
  • then you will get a folder contains .gradle .idea build gradle...然后你会得到一个包含.gradle .idea build gradle...的文件夹.gradle .idea build gradle...
    ( I gotta call this folder as Project Folder ) (我得称这个文件夹为项目文件夹)
  • open Android Studio and select import project (Gradle, Eclipse ADT, etc.)打开 Android Studio 并选择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 1.如何从Android端检索Unity PlayerPrefs

you can find Project Folder/java/UnityPlayerActivity.java in project explorer on Android Studio.您可以在 Android Studio 的项目资源管理器中找到Project Folder/java/UnityPlayerActivity.java this class performs Unity application on Android side, thus you should write code for retrieving PlayerPrefs in here.此类在 Android 端执行 Unity 应用程序,因此您应该在此处编写用于检索 PlayerPrefs 的代码。 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.剩下的工作就是使用 SharedPreferences,而不是 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 2.如何在Unity应用程序中包含AppWidget

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.只需在 Android Studio 上打开项目文件夹的状态下,选择File -> New -> Widget -> App Widget ,您将看到 Widget 向导。 after the wizard, your Unity Application can be located in home screen as widget.在向导之后,您的 Unity 应用程序可以作为小部件位于主屏幕中。 rest of job is all about widget programming... ;D其余的工作都是关于小部件编程......;D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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