Android - 在活动中嵌入 Unity3d 场景 - 需要取消注册接收器? [英] Android - embedding Unity3d scene in activity - need to unregister receiver?

查看:17
本文介绍了Android - 在活动中嵌入 Unity3d 场景 - 需要取消注册接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成为 SO 成员有一段时间了,但从未真正提出过问题,所以这里是..

I've been a SO member for a while but never actually asked a question, so here goes..

我正在尝试制作一个包含两个 Activity 的 Android 应用.第一个是菜单屏幕(使用标准的 Android UI 元素),带有一个用于打开游戏活动的按钮.游戏活动将具有一些标准的 Android UI 元素和一个包含 Unity 3D 场景的 FrameLayout.

I'm trying to make an Android app with two activities. The first is a menu screen (using standard Android UI elements) with a button that opens the gameplay activity. The gameplay activity will have some standard Android UI elements and a FrameLayout containing a Unity 3D scene.

我使用的是 Unity 5 和 Eclipse Luna.

I'm using Unity 5 and Eclipse Luna.

我用一个简单的按钮制作了菜单屏幕来启动第二个活动.我遵循了本教程并设法获得了我的 Unity场景嵌入我的第二个活动中.到目前为止一切都很好...

I've made the menu screen with a simple button to start the second activity. I've followed this tutorial and managed to get my Unity scene embedded in my second activity. All good so far...

我第一次启动游戏活动时它工作正常(我可以看到嵌入在我的 FrameLayout 中的 Unity 场景)但是如果我关闭活动(返回菜单活动)然后再次启动游戏活动,我会得到这个错误...

The first time I start the gameplay activity it works fine (I can see the Unity scene embedded in my FrameLayout) but if I close the activity (to return to the menu activity) and then start the gameplay activity again I get this error...

Activity has leaked IntentReceiver com.unity3d.player.UnityPlayer$17@41866de0 that was originally registered here.  Are you missing a call to unregisterReceiver()?

我的尝试

我在网上做了很多搜索,看起来我需要在 onPauseonDestroy 中调用这样的东西;

What I've tried

I've done a lot of searching online and it looks like I need to call something like this in onPause or onDestroy;

m_UnityPlayer.currentActivity.unregisterReceiver(receiver);

...但是我不知道接收器是什么,所以我无法在我的 unregisterReceiver 调用中引用它.我试过创建一个 BroadcastReceiver,注册它然后在 onPause 取消注册,但它没有区别.

...but I don't know what the receiver is so I can't reference it in my unregisterReceiver call. I've tried creating a BroadcastReceiver, registering it and then unregistering it onPause but it makes no difference.

我在统一论坛上找到了一篇帖子,该帖子询问完全相同的问题,但令人沮丧的是,它没有答案(Unity 论坛上的很多问题似乎都没有答案,这就是我带着这个问题来到这里的原因).

I found a post on the unity forum that asks the exact same question but, frustratingly, it is unanswered (a lot of questions on the Unity forum seem to be unanswered, that's why I came here with this question).

我也试过在 onDestroy 中调用这个方法,但是当我关闭游戏活动时它实际上退出了我的整个应用程序.

I've also tried calling this method in onDestroy but it actually quits my whole app when I close the gameplay activity.

m_UnityPlayer.quit();

代码示例

这是我的游戏活动代码(为了清晰起见,我删除了导入);

Code Sample

Here's the code for my gameplay activity (I've removed the imports for clarity);

public class MyEmbeddedUnityActivity extends Activity {
    private UnityPlayer m_UnityPlayer;
    IntentFilter filter = new IntentFilter("");
    private BroadcastReceiver bRec = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //String action = intent.getAction();
            Log.d("", "onReceive intent: " + intent);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_embedded_unity);

        if(m_UnityPlayer==null){
            m_UnityPlayer = new UnityPlayer(this);
            int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
            m_UnityPlayer.init(glesMode, false);

            m_UnityPlayer.currentActivity.registerReceiver(bRec, filter);

            FrameLayout layout = (FrameLayout) findViewById(R.id.my_frame);
            LayoutParams lp = new LayoutParams(200, 300);
            layout.addView(m_UnityPlayer, 0, lp);
        }

    }

    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        m_UnityPlayer.windowFocusChanged(hasFocus);
    }

    @Override
    public void onDestroy (){
        //m_UnityPlayer.quit();
        m_UnityPlayer.currentActivity.unregisterReceiver(bRec);
        super.onDestroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        m_UnityPlayer.configurationChanged(newConfig);
    }

    @Override
    protected void onResume() {
        super.onResume();
        m_UnityPlayer.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        m_UnityPlayer.pause();
    }

}

最后...

我在嵌入 Unity 场景时遵循的步骤是在 2011 年编写的.整个过程(从 Unity 项目的暂存区抓取文件并在周围复制文件等)似乎真的hacky 和未记录.

是否有更好的方法将 Unity 场景嵌入到 Unity 5 的 Android 活动中?将 Unity 场景添加到现有应用中肯定是一件相当常见的尝试吗?

Is there a better way of embedding Unity scenes in Android activities with Unity 5? Surely adding a Unity scene to an existing app is a fairly common thing to try to do?

谢谢,任何帮助将不胜感激!

推荐答案

所以,我进行了更多搜索并最终找到了答案,就在这里!

So, I did some more searching and eventually found an answer, right here on SO!

此答案中所述,将此行添加到清单中即可解决问题;

As discussed in this answer, adding this line to the manifest fixes the problem;

android:process=":UnityKillsMe"

因此清单的相关部分如下所示;

so the relevant part of the manifest looks like this;

...

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".Studio"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:process=":UnityKillsMe"
        android:label="@string/title_activity_home" > 
    </activity>

...

这会强制游戏活动在单独的进程中启动.这意味着您可以在 Activity 的 onDestroy 方法中调用 m_UnityPlayer.quit(),而无需关闭整个应用程序.

This forces the gameplay activity to launch in a separate process. This means you can call m_UnityPlayer.quit() in the activity's onDestroy method without the whole app closing.

简单的修复,但令人讨厌的是它似乎完全没有记录.

Simple fix, but annoying that it seems to be completely undocumented.

希望这对某人有所帮助!

这篇关于Android - 在活动中嵌入 Unity3d 场景 - 需要取消注册接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆