保存区域描述暂停应用程序

时间:2017-06-19 18:20:04

标签: android unity3d google-project-tango

我正在使用Google Tango应用程序,我一直在尝试使用TangoApplication类保存区域描述。

我目前在OnApplicationPause()事件

上调用了以下函数
private void DoSaveCurrentAreaDescription(bool forceLearningMode)
{
    // Disable interaction before saving.
    m_initialized = false;
    if (m_tangoApplication.m_areaDescriptionLearningMode)
    {
        // The keyboard is not readable if you are not in the Unity main thread. Cache the value here.
        string name = "config";
        // Start saving process in another thread.

        m_saveThread = new Thread(delegate ()
        {
            // Start saving process in another thread.

            m_curAreaDescription = AreaDescription.SaveCurrent();
            AreaDescription.Metadata metadata = m_curAreaDescription.GetMetadata();
            metadata.m_name = name;
            m_curAreaDescription.SaveMetadata(metadata);
            m_TangoManager.m_lastKnownAreaDescription = m_curAreaDescription;
            m_TangoManager.SaveProductLocationsToDisk();

        });        

        m_saveThread.Start();

    }
    else
    {
        m_TangoManager.SaveProductLocationsToDisk();
    }
}

这在应用程序暂停功能期间被调用,但它不允许我保存ADF。如果我在应用程序仍在运行时调用此函数,它将被保存。

如果有人知道可能会发生什么(我假设是在回溯过程中遇到问题),我将永远为你负债。

2 个答案:

答案 0 :(得分:1)

实际上,由于Tango生命周期暂停和卸载所有Tango资源,然后我的暂停功能才有机会保存它。当前进入后台时,没有真正的方法来调用保存区域描述功能。我还联系了谷歌的工程师并收到了大量的“不要那样做”,因为目前它并没有像Tango那样支持。

在SDK上测试:Hopak

答案 1 :(得分:0)

有两个可能的原因导致它无法保存:

1 。Unity会抛出异常。

如果Tango AreaDescriptionSaveProductLocationsToDisk API使用任何Unity API,那么这就是问题,因为您can't在另一个线程中使用Unity API,如果您尝试这样做,则会抛出异常

您可以将保存代码放在try catch块中,然后在 Android Studio 中查看来自 Android Monitor 的结果,以检查是否存在此问题。

解决方法是删除Thread代码,并确保保存代码在主Thread

中运行
private void DoSaveCurrentAreaDescription(bool forceLearningMode)
{
    // Disable interaction before saving.
    m_initialized = false;
    if (m_tangoApplication.m_areaDescriptionLearningMode)
    {
        //The keyboard is not readable if you are not in the Unity main thread. Cache the value here.
        string name = "config";
        //Start saving process in another thread.

        m_curAreaDescription = AreaDescription.SaveCurrent();
        AreaDescription.Metadata metadata = m_curAreaDescription.GetMetadata();
        metadata.m_name = name;
        m_curAreaDescription.SaveMetadata(metadata);
        m_TangoManager.m_lastKnownAreaDescription = m_curAreaDescription;
        m_TangoManager.SaveProductLocationsToDisk();
    }
    else
    {
        m_TangoManager.SaveProductLocationsToDisk();
    }
}

2 。由于应用程序在保存之前已退出,因此无法保存。

解决方案是将m_saveThread.Join();添加到代码的末尾,以便Unity在现有代码之前等待代码执行。

private void DoSaveCurrentAreaDescription(bool forceLearningMode)
{
    // Disable interaction before saving.
    m_initialized = false;
    if (m_tangoApplication.m_areaDescriptionLearningMode)
    {
        // The keyboard is not readable if you are not in the Unity main thread. Cache the value here.
        string name = "config";
        // Start saving process in another thread.

        m_saveThread = new Thread(delegate ()
        {
            // Start saving process in another thread.

            m_curAreaDescription = AreaDescription.SaveCurrent();
            AreaDescription.Metadata metadata = m_curAreaDescription.GetMetadata();
            metadata.m_name = name;
            m_curAreaDescription.SaveMetadata(metadata);
            m_TangoManager.m_lastKnownAreaDescription = m_curAreaDescription;
            m_TangoManager.SaveProductLocationsToDisk();

        });        

        m_saveThread.Start();
        //Wait for Save to finish before existing from app
        m_saveThread.Join();
    }
    else
    {
        m_TangoManager.SaveProductLocationsToDisk();
    }
} 

或者再次删除Thread代码,并确保保存代码在主Thread中运行,就像我们在#1 中一样。