trackedObject.trackingState only the "tracking" return.

Hello. I am not very good at English.
I'm Japanese people.
Very troubled.

     private void Start()
    {
        count = 0;
        flg = false;
        arobj = TrackingState.Limited;
        logcount = 0;
        _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
        _markerNameAndCheckDictionary = new Dictionary<string, GameObject>();
    }
    private void Update()
    {
        if (_arPrefabs[2] != null && _objectManager.referenceLibrary.name == "assetMacker" && !flg)
        {
            flg = true;
            kousin();
        }
    }
    public void kousin()
    {
        _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
        referenceLibraryNameCheck = _objectManager.referenceLibrary;
        for (var i = 0; i < _arPrefabs.Length; i++)
        {
            var arPrefab = Instantiate(_arPrefabs[i]);
            _markerNameAndPrefabDictionary.Add(_objectManager.referenceLibrary[i].name, arPrefab);
            _markerNameAndCheckDictionary.Add(_objectManager.referenceLibrary[i].name, checkmark[i]);
            arPrefab.SetActive(false);
            mackerLabel[i].text = _objectManager.referenceLibrary[i].name;
       _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name]);
        }
        foreach (string key in _markerNameAndPrefabDictionary.Keys)
        {
            dictuionaryText += "key=" + key + "&val=" + _markerNameAndPrefabDictionary[key] + "/";
        }
    }
    public void objReset()
    {
        for (var i = 0; i < _arPrefabs.Length; i++)
        {
            _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
            _markerNameAndCheckDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
        }
        _markerNameAndPrefabDictionary.Clear();
        _markerNameAndCheckDictionary.Clear();
        _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
        _objectManager.referenceLibrary = referenceLibraryClone;
        kousin();
    }
    private void OnDisable()
    {
        _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    }

    private void ActivateARObject(ARTrackedObject trackedObject)
    {
        arobj = trackedObject.trackingState;
        mackerName = trackedObject.referenceObject.name;
        var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
        var objectMarkerTransform = trackedObject.transform;

        arobj = trackedObject.trackingState;

        var check = _markerNameAndCheckDictionary[trackedObject.referenceObject.name];
        var markerFrontRotation = objectMarkerTransform.rotation * Quaternion.Euler(0f, 0f, 0f);
        arObject.transform.SetPositionAndRotation(objectMarkerTransform.transform.position, markerFrontRotation);
        arObject.transform.SetParent(objectMarkerTransform);

        if (trackedObject.trackingState == TrackingState.Tracking && arObject.activeSelf == false)
        {
            check.SetActive(true);
            arObject.SetActive(true);
        }
        else if (trackedObject.trackingState == TrackingState.Limited)
        {
            rimitcount++;
            arObject.SetActive(false);
            check.SetActive(false);
        }
        else if (trackedObject.trackingState == TrackingState.None)
        {
            Nonecount++;
            arObject.SetActive(false);
            check.SetActive(false);
        }
    }

    private void OnTrackedObjectsChanged(ARTrackedObjectsChangedEventArgs eventArgs)
    {
        eeventArgs = eventArgs;
        foreach (var trackedObject in eeventArgs.added)
        {
            addCount++;
            ActivateARObject(trackedObject);
        }
        foreach (var trackedObject in eeventArgs.updated)
        {
            updateCount++;
            ActivateARObject(trackedObject);
        }
        foreach (var trackedObject in eeventArgs.removed)
        {
            removeCount++;
        }
    }
}

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.

This script will be a good reference: https://github.com/Unity-Technologies/arfoundation-samples/blob/main/Assets/Scenes/ImageTracking/BasicImageTracking/TrackedImageInfoManager.cs

Remove your Update lines, add the
_objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
in OnEnable() instead.

1 Like

thank you.I'll try it.


I tried but there was no change.

Can you share your current script after you change it? Also have you check this script https://github.com/Unity-Technologies/arfoundation-samples/blob/main/Assets/Scenes/ImageTracking/BasicImageTracking/TrackedImageInfoManager.cs

1 Like

From our docs:

[quote]
In cases where tracking quality changes, such as if a trackable manager is disabled or a tracked object moves out of frame of the AR device's camera, the relevant subsystem may not remove any trackables. Instead, it may change those trackables' trackingState value. Your application can respond to tracking state changes by reading the trackingState property of trackables as they are added or updated.
[/quote]

https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/manual/architecture/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

1 Like

[quote=“xiennastudio”, post:5, topic: 929259]
Can you share your current script after you change it? Also have you check this script https://github.com/Unity-Technologies/arfoundation-samples/blob/main/Assets/Scenes/ImageTracking/BasicImageTracking/TrackedImageInfoManager.cs
[/quote]

    private void Start()
    {
        Debug.Log("スタート");
        count = 0;
        flg = false;
        scanFlg = false;
        arobj = TrackingState.Limited;
        logcount = 0;
        _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
        _markerNameAndCheckDictionary = new Dictionary<string, GameObject>();
    }
    private void Update()
    {
        if (_arPrefabs[2] == null)
        {
            Debug.Log("オブジェクトNULL");
        }
        if (_arPrefabs[2] != null && _objectManager.referenceLibrary.name == "assetMacker" && !flg)
        {
            flg = true;
            kousin();
        }
        //再検知の待ち時間
        if (scanFlg)
        {
            scanCount++;
            if (scanCount > 70)
            {
                _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
                scanFlg = false;
                scanCount = 0;
                Debug.Log("待ち時間おわり");
            }
        }
    }
    public void kousin()
    {
        Debug.Log("更新したよ");
        referenceLibraryNameCheck = _objectManager.referenceLibrary;

        //辞書を作る 画像の名前とARオブジェクトのPrefabを紐づける
        for (var i = 0; i < _arPrefabs.Length; i++)
        {
            var arPrefab = Instantiate(_arPrefabs[i]);
            _markerNameAndPrefabDictionary.Add(_objectManager.referenceLibrary[i].name, arPrefab);
            _markerNameAndCheckDictionary.Add(_objectManager.referenceLibrary[i].name, checkmark[i]);
            arPrefab.SetActive(false);
            mackerLabel[i].text = _objectManager.referenceLibrary[i].name;
            Debug.Log("辞書オブジェクトチェック" + _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name]);
        }
        foreach (string key in _markerNameAndPrefabDictionary.Keys)
        {
            dictuionaryText += "key=" + key + "&val=" + _markerNameAndPrefabDictionary[key] + "/";
        }
        Debug.Log("辞書チェック" + dictuionaryText);
    }
    public void OnObjReset()
    {
        Debug.Log("リセット");
        for (var i = 0; i < _arPrefabs.Length; i++)
        {
            _markerNameAndPrefabDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
            _markerNameAndCheckDictionary[_objectManager.referenceLibrary[i].name].SetActive(false);
        }
    }
    private void OnEnable()
    {
        Debug.Log("アクティブ");
        _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
    }
    private void OnDisable()
    {
        Debug.Log("非アクティブ");
        _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    }

    /// <summary>
    /// 認識したオブジェクトマーカーに応じて紐づいたARオブジェクトを表示
    /// </summary>
    /// <param name="trackedObject">認識したオブジェクトマーカー</param>
    private void ActivateARObject(ARTrackedObject trackedObject)
    {
        Debug.Log("トラックしたオブジェクトの名前は" + trackedObject.referenceObject.name);
        arobj = trackedObject.trackingState;
        mackerName = trackedObject.referenceObject.name;
        //認識したオブジェクトマーカーの名前を使って辞書から任意のオブジェクトを引っ張り出す
        var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
        var objectMarkerTransform = trackedObject.transform;

        var check = _markerNameAndCheckDictionary[trackedObject.referenceObject.name];
        //位置合わせ
        var markerFrontRotation = objectMarkerTransform.rotation * Quaternion.Euler(0f, 0f, 0f);
        arObject.transform.SetPositionAndRotation(objectMarkerTransform.transform.position, markerFrontRotation);
        arObject.transform.SetParent(objectMarkerTransform);

        if (trackedObject.trackingState == TrackingState.Tracking && arObject.activeSelf == false && scanFlg == false)
        {
            check.SetActive(true);
            arObject.SetActive(true);
            scanFlg = true;
            _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
        }
        else if (trackedObject.trackingState == TrackingState.Limited)
        {
            Debug.Log("Limitedが検出");
            rimitcount++;

            arObject.SetActive(false);
            check.SetActive(false);
        }
        else if (trackedObject.trackingState == TrackingState.None)
        {
            Debug.Log("Noneが検出");
            Nonecount++;
            arObject.SetActive(false);
            check.SetActive(false);
        }
    }

    /// <summary>
    /// TrackedImagesChanged時の処理
    /// </summary>
    /// <param name = "eventArgs" > 検出イベントに関する引数 </ param >
    private void OnTrackedObjectsChanged(ARTrackedObjectsChangedEventArgs eventArgs)
    {
        foreach (var trackedObject in eventArgs.added)
        {
            addCount++;
            ActivateARObject(trackedObject);
            VibrationMng.ShortVibration();
        }
        foreach (var trackedObject in eventArgs.updated)
        {
            updateCount++;
            ActivateARObject(trackedObject);
        }
        foreach (var trackedObject in eventArgs.removed)
        {
            removeCount++;
            Debug.Log("remove回数は" + removeCount);
            var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
            arObject.SetActive(false);
        }
    }

Additional OnEnable()

I will check URLscript.


I'll give MonoBehaviour.OnBecameInvisible a try.
thank you.