The script is not executed when reading the pattern

0 votes
asked Jul 4, 2022 by yoelrodguez (140 points)

Hi, I am trying to catch the event to play sound when the pattern is read. At the moment of reading the pattern, the TargetLoad event does not do any action to me. Below I leave the code to see if you can help me.
Thank you

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using easyar;

public class Audiora : MonoBehaviour
{
    public AudioClip otherClip;
    public AudioSource audios;
    private AudioSource[] allAudioSources;

    private ImageTargetController controller;

    void Start()
    {
        audios = GetComponent<AudioSource>();
        AddTargetControllerEvents(controller);
    }

    private void AddTargetControllerEvents(ImageTargetController controller)
    {
        if (!controller)
        {
            return;
        }

        controller.TargetFound += () =>
        {
            Debug.LogFormat("Found target {{id = {0}, name = {1}}}", controller.Target.runtimeID(), controller.Target.name());
        };
        controller.TargetLost += () =>
        {
            Debug.LogFormat("Lost target {{id = {0}, name = {1}}}", controller.Target.runtimeID(), controller.Target.name());
        };
        controller.TargetLoad += (Target target, bool status) =>
        {
            allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
            foreach (AudioSource audioS in allAudioSources)
            {
                audioS.Stop();
            }

            if (!audios.isPlaying)
            {
                audios.Play();
            }
            Debug.LogFormat("Load target {{id = {0}, name = {1}, size = {2}}} into {3} => {4}", target.runtimeID(), target.name(), controller.Size, controller.Tracker.name, status);
        };
        controller.TargetUnload += (Target target, bool status) =>
        {
             Debug.LogFormat("Unload target {{id = {0}, name = {1}}} => {2}", target.runtimeID(), target.name(), status);
        };
    }
}

1 Answer

0 votes
answered Dec 26, 2022 by kenn (18,750 points)
Tried the sample?
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...