subere23

Lesson 4 AppControllerUpdate

Apr 12th, 2018
392
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using GoogleARCore;
  3. using System.Collections.Generic;
  4.  
  5. //Lesson 4
  6. using GoogleARCore.HelloAR;
  7.  
  8. public class AppController : MonoBehaviour
  9. {
  10.     private List<TrackedPlane> m_NewPlanes = new List<TrackedPlane>();
  11.     private List<TrackedPlane> m_AllPlanes = new List<TrackedPlane>();
  12.     private bool m_IsQuitting = false;
  13.  
  14.     //Lesson 4
  15.     public GameObject TrackedPlanePrefab;
  16.    // public GameObject VisualTracked
  17.  
  18.  
  19.     // Use this for initialization
  20.     void Start()
  21.     {
  22.         QuitOnConnectionErrors();
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.  
  29.         if (Session.Status != SessionStatus.Tracking)
  30.         {
  31.             const int lostTrackingSleepTimeout = 15;
  32.             Screen.sleepTimeout = lostTrackingSleepTimeout;            
  33.  
  34.             return;
  35.         }
  36.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
  37.        
  38.        
  39.         //Lesson 4 -
  40.         Session.GetTrackables<TrackedPlane>(m_NewPlanes, TrackableQueryFilter.New);
  41.         for (int i = 0; i < m_NewPlanes.Count; i++)
  42.         {
  43.             // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
  44.             // the origin with an identity rotation since the mesh for our prefab is updated in Unity World
  45.             // coordinates.
  46.             GameObject planeObject = Instantiate(TrackedPlanePrefab, Vector3.zero, Quaternion.identity,
  47.                 transform);
  48.             planeObject.GetComponent<TrackedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
  49.         }
  50.  
  51.         Session.GetTrackables<TrackedPlane>(m_AllPlanes);  
  52.  
  53.  
  54.  
  55.     }
  56.  
  57.  
  58.     private void QuitOnConnectionErrors()
  59.     {
  60.         if (m_IsQuitting)
  61.         {
  62.             return;
  63.         }
  64.  
  65.         // Quit if ARCore was unable to connect and give Unity some time for the toast to appear.
  66.         if (Session.Status == SessionStatus.ErrorPermissionNotGranted)
  67.         {
  68.             ShowAndroidToastMessage("Camera permission is needed to run this application.");
  69.             m_IsQuitting = true;
  70.             Invoke("_DoQuit", 0.5f);
  71.         }
  72.         else if (Session.Status.IsError())
  73.         {
  74.             ShowAndroidToastMessage("ARCore encountered a problem connecting.  Please start the app again.");
  75.             m_IsQuitting = true;
  76.             Invoke("_DoQuit", 0.5f);
  77.         }
  78.  
  79.     }
  80.  
  81.     private static void ShowAndroidToastMessage(string message)
  82.     {
  83.         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  84.         AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  85.  
  86.         if (unityActivity != null)
  87.         {
  88.             AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
  89.             unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
  90.             {
  91.                 AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity,
  92.                     message, 0);
  93.                 toastObject.Call("show");
  94.             }));
  95.         }
  96.     }
  97.  
  98. }
RAW Paste Data