Search Unity

How to modify the standard HelloARcontroller script to instantiate one time?

Discussion in 'ARCore' started by V-J, Apr 11, 2018.

  1. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    31
    Hello, i want to modify the standard c# code HelloARcontroller to instantiate one time. So by touching the screen it will come only 1 time. Can someone help me with this please?


    Code (CSharp):
    1. //-----------------------------------------------------------------------
    2. // <copyright file="HelloARController.cs" company="Google">
    3. //
    4. // Copyright 2017 Google Inc. All Rights Reserved.
    5. //
    6. // Licensed under the Apache License, Version 2.0 (the "License");
    7. // you may not use this file except in compliance with the License.
    8. // You may obtain a copy of the License at
    9. //
    10. // http://www.apache.org/licenses/LICENSE-2.0
    11. //
    12. // Unless required by applicable law or agreed to in writing, software
    13. // distributed under the License is distributed on an "AS IS" BASIS,
    14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15. // See the License for the specific language governing permissions and
    16. // limitations under the License.
    17. //
    18. // </copyright>
    19. //-----------------------------------------------------------------------
    20.  
    21. namespace GoogleARCore.HelloAR
    22. {
    23.     using System.Collections.Generic;
    24.     using GoogleARCore;
    25.     using UnityEngine;
    26.     using UnityEngine.Rendering;
    27.  
    28. #if UNITY_EDITOR
    29.     // Set up touch input propagation while using Instant Preview in the editor.
    30.     using Input = InstantPreviewInput;
    31. #endif
    32.  
    33.     /// <summary>
    34.     /// Controls the HelloAR example.
    35.     /// </summary>
    36.     public class HelloARController : MonoBehaviour
    37.     {
    38.         /// <summary>
    39.         /// The first-person camera being used to render the passthrough camera image (i.e. AR background).
    40.         /// </summary>
    41.         public Camera FirstPersonCamera;
    42.  
    43.         /// <summary>
    44.         /// A prefab for tracking and visualizing detected planes.
    45.         /// </summary>
    46.         public GameObject TrackedPlanePrefab;
    47.  
    48.         /// <summary>
    49.         /// A model to place when a raycast from a user touch hits a plane.
    50.         /// </summary>
    51.         public GameObject AndyAndroidPrefab;
    52.  
    53.         /// <summary>
    54.         /// A gameobject parenting UI for displaying the "searching for planes" snackbar.
    55.         /// </summary>
    56.         public GameObject SearchingForPlaneUI;
    57.  
    58.         /// <summary>
    59.         /// A list to hold new planes ARCore began tracking in the current frame. This object is used across
    60.         /// the application to avoid per-frame allocations.
    61.         /// </summary>
    62.         private List<TrackedPlane> m_NewPlanes = new List<TrackedPlane>();
    63.  
    64.         /// <summary>
    65.         /// A list to hold all planes ARCore is tracking in the current frame. This object is used across
    66.         /// the application to avoid per-frame allocations.
    67.         /// </summary>
    68.         private List<TrackedPlane> m_AllPlanes = new List<TrackedPlane>();
    69.  
    70.         /// <summary>
    71.         /// True if the app is in the process of quitting due to an ARCore connection error, otherwise false.
    72.         /// </summary>
    73.         private bool m_IsQuitting = false;
    74.  
    75.         /// <summary>
    76.         /// The Unity Update() method.
    77.         /// </summary>
    78.         public void Update()
    79.         {
    80.             // Exit the app when the 'back' button is pressed.
    81.             if (Input.GetKey(KeyCode.Escape))
    82.             {
    83.                 Application.Quit();
    84.             }
    85.  
    86.             _QuitOnConnectionErrors();
    87.  
    88.             // Check that motion tracking is tracking.
    89.             if (Session.Status != SessionStatus.Tracking)
    90.             {
    91.                 const int lostTrackingSleepTimeout = 15;
    92.                 Screen.sleepTimeout = lostTrackingSleepTimeout;
    93.                 if (!m_IsQuitting && Session.Status.IsValid())
    94.                 {
    95.                     SearchingForPlaneUI.SetActive(true);
    96.                 }
    97.  
    98.                 return;
    99.             }
    100.  
    101.             Screen.sleepTimeout = SleepTimeout.NeverSleep;
    102.  
    103.             // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
    104.             Session.GetTrackables<TrackedPlane>(m_NewPlanes, TrackableQueryFilter.New);
    105.             for (int i = 0; i < m_NewPlanes.Count; i++)
    106.             {
    107.                 // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
    108.                 // the origin with an identity rotation since the mesh for our prefab is updated in Unity World
    109.                 // coordinates.
    110.                 GameObject planeObject = Instantiate(TrackedPlanePrefab, Vector3.zero, Quaternion.identity,
    111.                     transform);
    112.                 planeObject.GetComponent<TrackedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
    113.             }
    114.  
    115.             // Hide snackbar when currently tracking at least one plane.
    116.             Session.GetTrackables<TrackedPlane>(m_AllPlanes);
    117.             bool showSearchingUI = true;
    118.             for (int i = 0; i < m_AllPlanes.Count; i++)
    119.             {
    120.                 if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
    121.                 {
    122.                     showSearchingUI = false;
    123.                     break;
    124.                 }
    125.             }
    126.  
    127.             SearchingForPlaneUI.SetActive(showSearchingUI);
    128.  
    129.             // If the player has not touched the screen, we are done with this update.
    130.             Touch touch;
    131.             if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
    132.             {
    133.                 return;
    134.             }
    135.  
    136.             // Raycast against the location the player touched to search for planes.
    137.             TrackableHit hit;
    138.             TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
    139.                 TrackableHitFlags.FeaturePointWithSurfaceNormal;
    140.  
    141.             if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
    142.             {
    143.                 var andyObject = Instantiate(AndyAndroidPrefab, hit.Pose.position, hit.Pose.rotation);
    144.  
    145.                 // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
    146.                 // world evolves.
    147.                 var anchor = hit.Trackable.CreateAnchor(hit.Pose);
    148.  
    149.                 // Andy should look at the camera but still be flush with the plane.
    150.                 if ((hit.Flags & TrackableHitFlags.PlaneWithinPolygon) != TrackableHitFlags.None)
    151.                 {
    152.                     // Get the camera position and match the y-component with the hit position.
    153.                     Vector3 cameraPositionSameY = FirstPersonCamera.transform.position;
    154.                     cameraPositionSameY.y = hit.Pose.position.y;
    155.  
    156.                     // Have Andy look toward the camera respecting his "up" perspective, which may be from ceiling.
    157.                     andyObject.transform.LookAt(cameraPositionSameY, andyObject.transform.up);
    158.                 }
    159.  
    160.                 // Make Andy model a child of the anchor.
    161.                 andyObject.transform.parent = anchor.transform;
    162.             }
    163.         }
    164.  
    165.         /// <summary>
    166.         /// Quit the application if there was a connection error for the ARCore session.
    167.         /// </summary>
    168.         private void _QuitOnConnectionErrors()
    169.         {
    170.             if (m_IsQuitting)
    171.             {
    172.                 return;
    173.             }
    174.  
    175.             // Quit if ARCore was unable to connect and give Unity some time for the toast to appear.
    176.             if (Session.Status == SessionStatus.ErrorPermissionNotGranted)
    177.             {
    178.                 _ShowAndroidToastMessage("Camera permission is needed to run this application.");
    179.                 m_IsQuitting = true;
    180.                 Invoke("_DoQuit", 0.5f);
    181.             }
    182.             else if (Session.Status.IsError())
    183.             {
    184.                 _ShowAndroidToastMessage("ARCore encountered a problem connecting.  Please start the app again.");
    185.                 m_IsQuitting = true;
    186.                 Invoke("_DoQuit", 0.5f);
    187.             }
    188.         }
    189.  
    190.         /// <summary>
    191.         /// Actually quit the application.
    192.         /// </summary>
    193.         private void _DoQuit()
    194.         {
    195.             Application.Quit();
    196.         }
    197.  
    198.         /// <summary>
    199.         /// Show an Android toast message.
    200.         /// </summary>
    201.         /// <param name="message">Message string to show in the toast.</param>
    202.         private void _ShowAndroidToastMessage(string message)
    203.         {
    204.             AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    205.             AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    206.  
    207.             if (unityActivity != null)
    208.             {
    209.                 AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
    210.                 unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    211.                 {
    212.                     AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity,
    213.                         message, 0);
    214.                     toastObject.Call("show");
    215.                 }));
    216.             }
    217.         }
    218.     }
    219. }
    220.  
    1 time. Can someone help me with this please?
     
  2. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    31
  3. maart

    maart

    Joined:
    Aug 3, 2010
    Posts:
    76
    i get the following error

    Assets\GoogleARCore\Examples\HelloAR\Scripts\HelloARController.cs(95,42): error CS0246: The type or namespace name 'TrackedPlaneVisualizer' could not be found (are you missing a using directive or an assembly reference?)
     
  4. meritt

    meritt

    Joined:
    Jul 10, 2017
    Posts:
    1
    //bool spawned = false;

    if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit) && spawned == false)
    {
    spawned = true;

    ...}