Search Unity

[XR Interaction Toolkit][AR] Limit placable objects to 1

Discussion in 'XR Interaction Toolkit and Input' started by d4n3x, Feb 13, 2020.

  1. d4n3x

    d4n3x

    Joined:
    Jan 23, 2019
    Posts:
    24
    Dear Community,

    does anybody know if there is any way to limit the AR Objects to 1 with the interaction toolkit? Would be very important to me to know that.

    Thanks in advance
    Danex
     
  2. d4n3x

    d4n3x

    Joined:
    Jan 23, 2019
    Posts:
    24
    I actually found a (very cruel) way to do it with the toolkit.
    Just add the following script to the PlacementInteractableObject and after placing the first time, you will not be able to place one again till u set the prefab new.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.Interaction.Toolkit.AR;
    5.  
    6. public class LimitPlaceableObjectsToOne : MonoBehaviour
    7. {
    8.     private ARPlacementInteractable placementInteractable;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         placementInteractable = this.GetComponent<ARPlacementInteractable>();
    13.         ARObjectPlacedEvent aRObjectPlacedEvent = placementInteractable.onObjectPlaced;
    14.         aRObjectPlacedEvent.AddListener(ObjectPlaced);
    15.     }
    16.  
    17.  
    18.     private void ObjectPlaced(ARPlacementInteractable p, GameObject q)
    19.     {
    20.         placementInteractable.placementPrefab = null;
    21.     }
    22. }
    23.  
    Greets Danex

    Update:
    To Fix the Bug below you ned to add the following line to the PlacementInteractable code of the toolkit simply after the raycast:

    Code (CSharp):
    1.  
    2. if(placementPrefab == null)
    3.                     return;
    4.  
     
    Last edited: Feb 13, 2020
    Dalton-Lima likes this.
  3. d4n3x

    d4n3x

    Joined:
    Jan 23, 2019
    Posts:
    24
    Actually if you want to place a prefab and the placement prefab is set to null - the toolkit breaks. I hope somebody has an solution for this....^^
     
  4. Matt_D_work

    Matt_D_work

    Unity Technologies

    Joined:
    Nov 30, 2016
    Posts:
    202
    we have a bug for this :) will be in the next version!
     
  5. goout88

    goout88

    Joined:
    Sep 18, 2017
    Posts:
    4
    Hi, any updates about this problem ?
     
  6. LeFx_Tom

    LeFx_Tom

    Joined:
    Jan 18, 2013
    Posts:
    88
    Unfortunately no. Until there will be a fix, I scripted another simple but drastic solution, without modifying any of the XR-Toolkit Scripts. I'm not proud of it, but it works for now ^^.

    If a prefab was spawned first time, everytime a new one is spawned, I simply destroy its parent, which is the trackable anchor ("PlacementAnchor"). I made this modification to d4n3x script

    Code (CSharp):
    1.     public class LimitPlaceableObjectsToOne : MonoBehaviour
    2.     {
    3.         protected ARPlacementInteractable _placementInteractable;
    4.         protected bool _oneSpawned = false;
    5.  
    6.         protected virtual void OnEnable()
    7.         {
    8.             if (!_placementInteractable)
    9.                 _placementInteractable = GetComponent<ARPlacementInteractable>();
    10.  
    11.             _placementInteractable.onObjectPlaced.AddListener(ObjectPlaced);
    12.         }
    13.  
    14.         protected virtual void OnDisable()
    15.         {
    16.             _placementInteractable.onObjectPlaced.RemoveAllListeners();
    17.         }
    18.  
    19.         protected virtual void ObjectPlaced(ARPlacementInteractable p, GameObject q)
    20.         {
    21.             if (_oneSpawned)
    22.             {
    23.                 Destroy(q.transform.parent.gameObject);
    24.             }
    25.  
    26.             _oneSpawned = true;
    27.         }
    28.     }
     
    Voronoi and goout88 like this.
  7. reddogg

    reddogg

    Joined:
    Jun 17, 2013
    Posts:
    11
    Hi, only a simple question, where I must put LimitPlaceableObjectsToOne script?

    tanks
     
  8. LeFx_Tom

    LeFx_Tom

    Joined:
    Jan 18, 2013
    Posts:
    88
    Because of the
    _placementInteractable = GetComponent<ARPlacementInteractable>();
    expression, the LimitPlaceableObjectsToOne component has to be located on the same GameObject as the ARPlacementInteractable component.

    You could also set the _placementInteractable public and simply drag and drop the ARPlacementInteractable component into that field, in the Unity Editor.
     
  9. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    589
    I love this! But I also hate this, it is soo ugly! I'm using AR Foundation 5.0 and I get a warning that 'onObjectPlaced' is deprecated and use' objectPlaced' instead. Here's an updated script to work with 5.0:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.Interaction.Toolkit.AR;
    3.  
    4.  
    5. public class LimitPlacedObjectsToOne : MonoBehaviour
    6. {
    7.  
    8.         protected ARPlacementInteractable _placementInteractable;
    9.         protected bool _oneSpawned = false;
    10.  
    11.         protected virtual void OnEnable()
    12.         {
    13.             if (!_placementInteractable)
    14.                 _placementInteractable = GetComponent<ARPlacementInteractable>();
    15.  
    16.             _placementInteractable.objectPlaced.AddListener(ObjectPlaced);
    17.         }
    18.  
    19.         protected virtual void OnDisable()
    20.         {
    21.             _placementInteractable.objectPlaced.RemoveAllListeners();
    22.         }
    23.  
    24.         protected virtual void ObjectPlaced(ARObjectPlacementEventArgs args)
    25.         {
    26.             if (_oneSpawned)
    27.             {
    28.                 Destroy(args.placementObject);
    29.             }
    30.             _oneSpawned = true;
    31.         }
    32.  
    33. }
    34.  
    @Matt_D_work or anyone at Unity, is there a more elegant fix for this? It seems an odd choice to assume folks want to place unlimited copies of an object in an AR scene. Maybe expose a maxObjects created int option?