2017-06-19 67 views
0

我正在研究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下面的函數。如果我在應用程序仍在運行時調用此函數,它將被保存。

如果任何人有任何想法會發生什麼(我假設線程問題與背後的過程)我會永遠在你的債務。

回答

0

有兩個可能的原因是它不保存:

.Exception是由Unity拋出。

如果探戈AreaDescriptionSaveProductLocationsToDisk API使用任何統一的API,然後這就是問題所在,因爲你can't使用統一的API在另一個線程和異常會被拋出,如果你嘗試這樣做。

您可以檢查,如果這是通過將保存代碼裏面try catch塊的問題,然後從的Android監視器Android Studio中查看結果。

的解決方法是刪除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(); 
    } 
} 

。它不是節能,因爲它是做保存之前的應用程序正在退出。

解決方案是在代碼的末尾添加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一樣。

1

這實際上是由於探戈的生命週期暫停和卸載了所有探戈資源之後,我的暫停功能纔有機會保存它。當前進入後臺時,沒有真正的方法來調用保存區域描述功能。我還聯繫了谷歌的工程師,並接受了一個沉重的「不要這樣做」,因爲目前它沒有像Tango那樣工作。

在SDK上測試:Hopak