Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question trackedObject.trackingState only the "tracking" return.

Discussion in 'AR' started by Enta_Hokama, Sep 15, 2023.

  1. Enta_Hokama

    Enta_Hokama

    Joined:
    Sep 12, 2022
    Posts:
    6
    Hello. I am not very good at English.
    I'm Japanese people.
    Very troubled.
    Code (CSharp):
    1.  
    2.  
    3.      private void Start()
    4.     {
    5.         count = 0;
    6.         flg = false;
    7.         arobj = TrackingState.Limited;
    8.         logcount = 0;
    9.         _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
    10.         _markerNameAndCheckDictionary = new Dictionary<string, GameObject>();
    11.     }
    12.     private void Update()
    13.     {
    14.         if (_arPrefabs[2] != null && _objectManager.referenceLibrary.name == "assetMacker" && !flg)
    15.         {
    16.             flg = true;
    17.             kousin();
    18.         }
    19.     }
    20.     public void kousin()
    21.     {
    22.         _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
    23.         referenceLibraryNameCheck = _objectManager.referenceLibrary;
    24.         for (var i = 0; i < _arPrefabs.Length; i++)
    25.         {
    26.             var arPrefab = Instantiate(_arPrefabs[i]);
    27.             _markerNameAndPrefabDictionary.Add(_objectManager.referenceLibrary[i].name, arPrefab);
    28.             _markerNameAndCheckDictionary.Add(_objectManager.referenceLibrary[i].name, checkmark[i]);
    29.             arPrefab.SetActive(false);
    30.             mackerLabel[i].text = _objectManager.referenceLibrary[i].name;
    31.        _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name]);
    32.         }
    33.         foreach (string key in _markerNameAndPrefabDictionary.Keys)
    34.         {
    35.             dictuionaryText += "key=" + key + "&val=" + _markerNameAndPrefabDictionary[key] + "/";
    36.         }
    37.     }
    38.     public void objReset()
    39.     {
    40.         for (var i = 0; i < _arPrefabs.Length; i++)
    41.         {
    42.             _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
    43.             _markerNameAndCheckDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
    44.         }
    45.         _markerNameAndPrefabDictionary.Clear();
    46.         _markerNameAndCheckDictionary.Clear();
    47.         _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    48.         _objectManager.referenceLibrary = referenceLibraryClone;
    49.         kousin();
    50.     }
    51.     private void OnDisable()
    52.     {
    53.         _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    54.     }
    55.  
    56.     private void ActivateARObject(ARTrackedObject trackedObject)
    57.     {
    58.         arobj = trackedObject.trackingState;
    59.         mackerName = trackedObject.referenceObject.name;
    60.         var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
    61.         var objectMarkerTransform = trackedObject.transform;
    62.  
    63.         arobj = trackedObject.trackingState;
    64.  
    65.         var check = _markerNameAndCheckDictionary[trackedObject.referenceObject.name];
    66.         var markerFrontRotation = objectMarkerTransform.rotation * Quaternion.Euler(0f, 0f, 0f);
    67.         arObject.transform.SetPositionAndRotation(objectMarkerTransform.transform.position, markerFrontRotation);
    68.         arObject.transform.SetParent(objectMarkerTransform);
    69.  
    70.         if (trackedObject.trackingState == TrackingState.Tracking && arObject.activeSelf == false)
    71.         {
    72.             check.SetActive(true);
    73.             arObject.SetActive(true);
    74.         }
    75.         else if (trackedObject.trackingState == TrackingState.Limited)
    76.         {
    77.             rimitcount++;
    78.             arObject.SetActive(false);
    79.             check.SetActive(false);
    80.         }
    81.         else if (trackedObject.trackingState == TrackingState.None)
    82.         {
    83.             Nonecount++;
    84.             arObject.SetActive(false);
    85.             check.SetActive(false);
    86.         }
    87.     }
    88.  
    89.     private void OnTrackedObjectsChanged(ARTrackedObjectsChangedEventArgs eventArgs)
    90.     {
    91.         eeventArgs = eventArgs;
    92.         foreach (var trackedObject in eeventArgs.added)
    93.         {
    94.             addCount++;
    95.             ActivateARObject(trackedObject);
    96.         }
    97.         foreach (var trackedObject in eeventArgs.updated)
    98.         {
    99.             updateCount++;
    100.             ActivateARObject(trackedObject);
    101.         }
    102.         foreach (var trackedObject in eeventArgs.removed)
    103.         {
    104.             removeCount++;
    105.         }
    106.     }
    107. }
    108.  
    trackedObject.trackingState only the "tracking" return.
    trackedObject.trackingState want to SetActive(false) objects Limited or None.
    How can I get them?

    eeventArgs.removed
    is not called.
    I am also concerned about this phenomenon.

    Please help me.
     
  2. xiennastudio

    xiennastudio

    Joined:
    Aug 2, 2023
    Posts:
    19
    Enta_Hokama likes this.
  3. Enta_Hokama

    Enta_Hokama

    Joined:
    Sep 12, 2022
    Posts:
    6
  4. Enta_Hokama

    Enta_Hokama

    Joined:
    Sep 12, 2022
    Posts:
    6
    I tried but there was no change.
     
  5. xiennastudio

    xiennastudio

    Joined:
    Aug 2, 2023
    Posts:
    19
    Enta_Hokama likes this.
  6. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    956
    From our docs:

    https://docs.unity3d.com/Packages/c...chitecture/managers.html#trackable-life-cycle

    If the tracking state value is
    Tracking
    , then the AR platform is attempting to track the object, even if it is out of frame. If you are trying to detect when the object is no longer in frame, you should use some other mechanism to achieve this, such as MonoBehaviour.OnBecameInvisible: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameInvisible.html
     
    Enta_Hokama likes this.
  7. Enta_Hokama

    Enta_Hokama

    Joined:
    Sep 12, 2022
    Posts:
    6
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         Debug.Log("スタート");
    4.         count = 0;
    5.         flg = false;
    6.         scanFlg = false;
    7.         arobj = TrackingState.Limited;
    8.         logcount = 0;
    9.         _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
    10.         _markerNameAndCheckDictionary = new Dictionary<string, GameObject>();
    11.     }
    12.     private void Update()
    13.     {
    14.         if (_arPrefabs[2] == null)
    15.         {
    16.             Debug.Log("オブジェクトNULL");
    17.         }
    18.         if (_arPrefabs[2] != null && _objectManager.referenceLibrary.name == "assetMacker" && !flg)
    19.         {
    20.             flg = true;
    21.             kousin();
    22.         }
    23.         //再検知の待ち時間
    24.         if (scanFlg)
    25.         {
    26.             scanCount++;
    27.             if (scanCount > 70)
    28.             {
    29.                 _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
    30.                 scanFlg = false;
    31.                 scanCount = 0;
    32.                 Debug.Log("待ち時間おわり");
    33.             }
    34.         }
    35.     }
    36.     public void kousin()
    37.     {
    38.         Debug.Log("更新したよ");
    39.         referenceLibraryNameCheck = _objectManager.referenceLibrary;
    40.  
    41.         //辞書を作る 画像の名前とARオブジェクトのPrefabを紐づける
    42.         for (var i = 0; i < _arPrefabs.Length; i++)
    43.         {
    44.             var arPrefab = Instantiate(_arPrefabs[i]);
    45.             _markerNameAndPrefabDictionary.Add(_objectManager.referenceLibrary[i].name, arPrefab);
    46.             _markerNameAndCheckDictionary.Add(_objectManager.referenceLibrary[i].name, checkmark[i]);
    47.             arPrefab.SetActive(false);
    48.             mackerLabel[i].text = _objectManager.referenceLibrary[i].name;
    49.             Debug.Log("辞書オブジェクトチェック" + _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name]);
    50.         }
    51.         foreach (string key in _markerNameAndPrefabDictionary.Keys)
    52.         {
    53.             dictuionaryText += "key=" + key + "&val=" + _markerNameAndPrefabDictionary[key] + "/";
    54.         }
    55.         Debug.Log("辞書チェック" + dictuionaryText);
    56.     }
    57.     public void OnObjReset()
    58.     {
    59.         Debug.Log("リセット");
    60.         for (var i = 0; i < _arPrefabs.Length; i++)
    61.         {
    62.             _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
    63.             _markerNameAndCheckDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
    64.         }
    65.     }
    66.     private void OnEnable()
    67.     {
    68.         Debug.Log("アクティブ");
    69.         _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
    70.     }
    71.     private void OnDisable()
    72.     {
    73.         Debug.Log("非アクティブ");
    74.         _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    75.     }
    76.  
    77.     /// <summary>
    78.     /// 認識したオブジェクトマーカーに応じて紐づいたARオブジェクトを表示
    79.     /// </summary>
    80.     /// <param name="trackedObject">認識したオブジェクトマーカー</param>
    81.     private void ActivateARObject(ARTrackedObject trackedObject)
    82.     {
    83.         Debug.Log("トラックしたオブジェクトの名前は" + trackedObject.referenceObject.name);
    84.         arobj = trackedObject.trackingState;
    85.         mackerName = trackedObject.referenceObject.name;
    86.         //認識したオブジェクトマーカーの名前を使って辞書から任意のオブジェクトを引っ張り出す
    87.         var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
    88.         var objectMarkerTransform = trackedObject.transform;
    89.  
    90.         var check = _markerNameAndCheckDictionary[trackedObject.referenceObject.name];
    91.         //位置合わせ
    92.         var markerFrontRotation = objectMarkerTransform.rotation * Quaternion.Euler(0f, 0f, 0f);
    93.         arObject.transform.SetPositionAndRotation(objectMarkerTransform.transform.position, markerFrontRotation);
    94.         arObject.transform.SetParent(objectMarkerTransform);
    95.  
    96.         if (trackedObject.trackingState == TrackingState.Tracking && arObject.activeSelf == false && scanFlg == false)
    97.         {
    98.             check.SetActive(true);
    99.             arObject.SetActive(true);
    100.             scanFlg = true;
    101.             _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    102.         }
    103.         else if (trackedObject.trackingState == TrackingState.Limited)
    104.         {
    105.             Debug.Log("Limitedが検出");
    106.             rimitcount++;
    107.  
    108.             arObject.SetActive(false);
    109.             check.SetActive(false);
    110.         }
    111.         else if (trackedObject.trackingState == TrackingState.None)
    112.         {
    113.             Debug.Log("Noneが検出");
    114.             Nonecount++;
    115.             arObject.SetActive(false);
    116.             check.SetActive(false);
    117.         }
    118.     }
    119.  
    120.     /// <summary>
    121.     /// TrackedImagesChanged時の処理
    122.     /// </summary>
    123.     /// <param name = "eventArgs" > 検出イベントに関する引数 </ param >
    124.     private void OnTrackedObjectsChanged(ARTrackedObjectsChangedEventArgs eventArgs)
    125.     {
    126.         foreach (var trackedObject in eventArgs.added)
    127.         {
    128.             addCount++;
    129.             ActivateARObject(trackedObject);
    130.             VibrationMng.ShortVibration();
    131.         }
    132.         foreach (var trackedObject in eventArgs.updated)
    133.         {
    134.             updateCount++;
    135.             ActivateARObject(trackedObject);
    136.         }
    137.         foreach (var trackedObject in eventArgs.removed)
    138.         {
    139.             removeCount++;
    140.             Debug.Log("remove回数は" + removeCount);
    141.             var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
    142.             arObject.SetActive(false);
    143.         }
    144.     }
    Additional OnEnable()

    I will check URLscript.
     
    Last edited: Sep 20, 2023
  8. Enta_Hokama

    Enta_Hokama

    Joined:
    Sep 12, 2022
    Posts:
    6
    I'll give MonoBehaviour.OnBecameInvisible a try.
    thank you.