이벤트 처리를 위해 ITrackableEventHandler를 추가한다.
1
public class TargetTracker : MonoBehaviour, ITrackableEventHandler
cs


해당 타겟의 TrackableBehaviour 컴포넌트를 받아 해당 스크립트에서 이벤트를 구독할 수 있도록 등록한다.
1
2
3
4
5
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
            {
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
cs


ITrackableEventHandler의 OnTrackableStateChanged 메소드를 구현해야 한다.(이부분에서 마커 트렉킹에 대한 정보가 나온다)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
        {
            print("Test");
            if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED ||
                       newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                print("TargetTracker : onTrackingFound");
                OnTrackingFound();
            }
            else
            {
                print("TargetTracker : onTrackingLost");
                OnTrackingLost();
            }
        }
cs



전체소스

TargetTracker.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
using System;
using System.Collections;
namespace Vuforia
{
    public class TargetTracker : MonoBehaviour, ITrackableEventHandler
    {
        private TrackableBehaviour mTrackableBehaviour;
        public event Action OnTrackingFound = () => { };
        public event Action OnTrackingLost = () => { };
 
        void Start()
        {
            mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
            {
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
        }
 
        public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
        {
            print("Test");
            if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED ||
                       newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                print("TargetTracker : onTrackingFound");
                OnTrackingFound();
            }
            else
            {
                print("TargetTracker : onTrackingLost");
                OnTrackingLost();
            }
        }
        
 
    }
}
cs








마커가 인식되면 생성될 오브젝트안에 들어갈 스크립트

targetTrackListener.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
 
namespace Vuforia
{
 
    public class targetTrackListener : MonoBehaviour
    {
 
        public bool visiToggle;
 
        private TargetTracker targetObject = null;
 
        void Start()
        {
            targetObject = GameObject.Find("ImageTarget (1)").GetComponent<TargetTracker>();
            targetObject.OnTrackingFound += trackingFound;
            //targetObject.OnTrackingLost+= trackingLost;
        }
 
 
        void onDestroy()
        {
            targetObject.OnTrackingFound -= trackingFound;
            //targetObject.OnTrackingLost-= trackingLost;
        }
 
 
        public void trackingFound()
        {
            if (visiToggle)
            {
                gameObject.SetActive(false);
                visiToggle = false;
            }
            else
            {
                gameObject.SetActive(true);
                visiToggle = true;
            }
        }
 
 
    }
 
}
cs


+ Recent posts