Search Unity

Place content once

Discussion in 'Scripting' started by davejones1, Mar 20, 2018.

  1. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I am using the mid air Vuforia feature to place content in the real world. The script attached is used to place content into the world when the user touches the screen. So far the script is used to place content once the user taps on the screen and the content moves when the user taps in a new location.

    I am looking at a way to change it so that the user can only place content once. After they have placed the content when tapping on the screen they wont be able to tap on the screen again to move the content. Does anyone have a solution?









    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using Vuforia;
    4. public class DeployStageOnce : MonoBehaviour {
    5.    
    6.     public GameObject AnchorStage;
    7.     private PositionalDeviceTracker _deviceTracker;
    8.     private GameObject _previousAnchor;
    9.    
    10.     public void Start ()
    11.     {
    12.         if (AnchorStage == null)
    13.         {
    14.             Debug.Log("AnchorStage must be specified");
    15.             return;
    16.         }
    17.         AnchorStage.SetActive(false);
    18.     }
    19.     public void Awake()
    20.     {
    21.         VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
    22.     }
    23.     public void OnDestroy()
    24.     {
    25.         VuforiaARController.Instance.UnregisterVuforiaStartedCallback(OnVuforiaStarted);
    26.     }
    27.     private void OnVuforiaStarted()
    28.     {
    29.         _deviceTracker = TrackerManager.Instance.GetTracker<PositionalDeviceTracker>();
    30.     }
    31.     public void OnInteractiveHitTest(HitTestResult result)
    32.     {
    33.         if (result == null || AnchorStage == null)
    34.         {
    35.             Debug.LogWarning("Hit test is invalid or AnchorStage not set");
    36.             return;
    37.         }
    38.         var anchor = _deviceTracker.CreatePlaneAnchor(Guid.NewGuid().ToString(), result);
    39.         if (anchor != null)
    40.         {
    41.             AnchorStage.transform.parent = anchor.transform;
    42.             AnchorStage.transform.localPosition = Vector3.zero;
    43.             AnchorStage.transform.localRotation = Quaternion.identity;
    44.             AnchorStage.SetActive(true);
    45.         }
    46.         if (_previousAnchor != null)
    47.         {
    48.             Destroy(_previousAnchor);
    49.         }
    50.         _previousAnchor = anchor;
    51.     }
    52. }
     
  2. Deleted User

    Deleted User

    Guest

    You could disable the plane finder all together. On your planefinder you'll have to add a an entry to your OnInteractiveHitTest which then disables itself. That way you can only place content once (Even though it might not be the best solution)
     
    davejones1 likes this.
  3. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Thanks for the reply. Do you have example code of how this would be done?
     
  4. Deleted User

    Deleted User

    Guest

    Add it to your plane finder behavior in your editor:


     
  5. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Ok I will try this and get back to you.
     
  6. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Your commendation worked. Thank you very much.
     
  7. Deleted User

    Deleted User

    Guest

    No problem, I'm sure there are better ways in doing this. I just know this works
     
  8. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    In what ways could this be done better?
     
  9. Deleted User

    Deleted User

    Guest

    I haven't worked enough with Vuforia to answer that. I just know it can be done more elegant than just disabling the entire thing.
     
    davejones1 likes this.
  10. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I suppose a way could be to disable a function in the script or something like that.