Search Unity

  1. Read here to learn more about our latest preview of the XR Interaction Toolkit.
    Dismiss Notice
  2. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  3. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

[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:
    22
    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:
    22
    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:
    22
    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:
    151
    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:
    28
    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.     }
     
    goout88 likes 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:
    28
    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.
     
unityunity