Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature hide childrens when lost tracker #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public override void OnInspectorGUI()
{
GenerateLibFromSceneSetting();
}

if (GUILayout.Button("Create NY Image Tracker"))
{
GenerateNyImageTracker();
}
}

public void GenerateLibFromSceneSetting()
Expand Down Expand Up @@ -47,4 +52,25 @@ public void GenerateLibFromSceneSetting()

Debug.Log(debugText);
}

public void GenerateNyImageTracker()
{
GameObject nyImageTracker = new GameObject();
nyImageTracker.AddComponent<NYImageTracker>();
nyImageTracker.name = GetNameNyImageTrackerObj();
}

private string GetNameNyImageTrackerObj()
{
int nyImageTrackoutSceneCount = FindObjectsOfType<NYImageTracker>().Length;

if(nyImageTrackoutSceneCount <= 9)
{
return $"NY-ImageTracker-0{nyImageTrackoutSceneCount}";
}
else
{
return $"NY-ImageTracker-{nyImageTrackoutSceneCount}";
}
}
}
7 changes: 7 additions & 0 deletions Assets/ARFoundationHelper/Scripts/NYImageTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class NYImageTracker : MonoBehaviour
float _playScaler = 1.0f;

public bool hideTrackerWhenPlay = true;
public bool hideChildrensWhenLostTarget = false;

public Transform referenceGrouper;

Expand All @@ -35,6 +36,12 @@ public void Start()
gameObject.GetComponent<MeshRenderer>().enabled = false;
}

if (hideChildrensWhenLostTarget)
{
gameObject.AddComponent<NYImageTrackerEventHandler>();
gameObject.GetComponent<NYImageTrackerEventHandler>().HideChildrensWhenLostTarget(hideChildrensWhenLostTarget);
}

// grab reference data before scale
_originPos = transform.position;
_originRot = transform.rotation;
Expand Down
32 changes: 29 additions & 3 deletions Assets/ARFoundationHelper/Scripts/NYImageTrackerEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,39 @@
[RequireComponent(typeof(NYImageTracker))]
public class NYImageTrackerEventHandler : MonoBehaviour
{
public virtual void OnTrackingFound ()
private bool hideChildrensWhenLostTarget = false;

private void Start()
{
HideChildrensEffect(true);
}

public virtual void OnTrackingFound()
{
Debug.Log("tracker [" + gameObject.name + "] Found!");
Debug.Log("tracker [" + gameObject.name + "] Found!");
HideChildrensEffect(false);
}

public virtual void OnTrackingLost ()
{
Debug.Log("tracker [" + gameObject.name + "] Lost!");
Debug.Log("tracker [" + gameObject.name + "] Lost!");
HideChildrensEffect(true);
}

public void HideChildrensWhenLostTarget(bool hideChildrensWhenLostTarget)
{
this.hideChildrensWhenLostTarget = hideChildrensWhenLostTarget;
}

private void HideChildrensEffect(bool hide)
{
if (hideChildrensWhenLostTarget)
{
for (int index = 0; index < transform.childCount; index++)
{
transform.GetChild(index).gameObject.SetActive(!hide);

}
}
}
}
16 changes: 10 additions & 6 deletions Assets/ARFoundationHelper/Scripts/NYImageTrackerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using UnityEngine.XR.ARFoundation;

public class NYImageTrackerManager : MonoBehaviour
{
public XRReferenceImageLibrary targetLib;
{
[Tooltip("The library of images which will be detected and/or tracked in the physical environment.")]
public XRReferenceImageLibrary targetLib;
[Tooltip("Detect trackers on runtime")]
public Transform[] trackerObjs;
TrackingState[] trackerStates;

Expand All @@ -18,8 +20,9 @@ public class NYImageTrackerManager : MonoBehaviour
int _imgIndex = 0;

// setting tracked image limit
Dictionary<int, NYImageTracker> trackedImgs = new Dictionary<int, NYImageTracker>();
int maxTrackImgCount = 0;
Dictionary<int, NYImageTracker> trackedImgs = new Dictionary<int, NYImageTracker>();
[Tooltip("The maximum number of moving images to track in realtime. Not all implementations support this feature.")]
public int maxTrackImgCount = 0;

// Start is called before the first frame update
void Start()
Expand All @@ -36,10 +39,11 @@ void Start()

// init foundation ar image manager
_foundationTrackerManager = GameObject.FindObjectOfType<ARTrackedImageManager>();
maxTrackImgCount = _foundationTrackerManager.currentMaxNumberOfMovingImages;
//maxTrackImgCount = _foundationTrackerManager.currentMaxNumberOfMovingImages;

Debug.Log("Get Manager: " + _foundationTrackerManager.gameObject.name);
//_foundationTrackerManager = gameObject.AddComponent<ARTrackedImageManager>();

_foundationTrackerManager.requestedMaxNumberOfMovingImages = maxTrackImgCount;
_foundationTrackerManager.enabled = false;
_foundationTrackerManager.referenceLibrary = targetLib;
_foundationTrackerManager.enabled = true;
Expand Down