Search Unity

Having trouble fading out an image and fading in another one using ITrackableEventHandler

Discussion in 'Image Effects' started by PatrickAttie, May 8, 2018.

  1. PatrickAttie

    PatrickAttie

    Joined:
    Nov 2, 2016
    Posts:
    1
    This is (below the image) the DefaultTrackableEventHandler.cs that I have modified (it is attached to my image target).
    As you can see, I have 2 elements (element 0 and element 1) that I am trying to fade out (element 1) and fade in (element 0).
    The fade in and out work well, but always happen twice before completing and I do not understand why. Everything happens in the code below.

    If you think of a more elegant method, please let me know as I have 23 elements in fact coming to that Materials array in the Mesh Renderer.

    A big big thank you for any hint!

    upload_2018-5-7_20-6-47.png



    /*==============================================================================
    Copyright (c) 2017 PTC Inc. All Rights Reserved.
    Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    All Rights Reserved.
    Confidential and Proprietary - Protected under copyright and other laws.
    ==============================================================================*/
    using UnityEngine;
    using UnityEngine.Video;
    using Vuforia;
    using System.Collections;

    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
    {
    #region PRIVATE_MEMBER_VARIABLES
    protected TrackableBehaviour mTrackableBehaviour;

    //My Variables
    public AudioSource asource;
    public AudioClip aClip;
    public VideoPlayer vPlayer;
    private Material[] mats;

    public static bool targetDetected = false;
    #endregion // PRIVATE_MEMBER_VARIABLES
    #region UNTIY_MONOBEHAVIOUR_METHODS

    protected virtual void Start()
    {
    mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    if (mTrackableBehaviour)
    mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }
    #endregion // UNTIY_MONOBEHAVIOUR_METHODS

    #region PUBLIC_METHODS
    //Make only the voix-off text NOT transparent initially. Remember
    // that the last element in materials array is displayed on the screen cube.
    void InitializeMaterials(Material[] mats)
    {
    for (int i=0; i < mats.Length; i++)
    {
    //Check if i->first image
    if (mats .name != "0-TexteMuseeORF")
    {
    Color tempCol = mats.color;
    tempCol.a = 1;
    mats .color = tempCol;
    }
    else
    {
    Color tempCol = mats.color;
    tempCol.a = 0;
    mats .color = tempCol;
    }
    }
    }

    IEnumerator FadeOutThenIn(Material mat1, Material mat2)
    {
    //Fade out
    for (float f = 1f; f >= 0f; f -= 0.005f)
    {
    Color c = mat1.color;
    c.a = f;
    mat1.color = c;
    yield return null;
    }
    //Fade in
    for (float f = 0f; f <= 1f; f += 0.005f)
    {
    Color c = mat2.color;
    c.a = f;
    mat2.color = c;
    yield return null;
    }
    }

    //***************We will fix this bad code later!!!***************
    // void FadeWhenSoundCompleted2_1()
    // {
    // {
    // StartCoroutine (FadeOutThenIn (mats[2], mats[1]));
    // }
    // }
    void FadeWhenSoundCompleted1_0()
    {
    {
    StartCoroutine (FadeOutThenIn (mats[1], mats[0]));
    }
    }

    void DisplayVideoWhenFadingCompleted()
    {
    this.vPlayer.gameObject.SetActive(true);
    vPlayer.Play ();
    }

    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
    TrackableBehaviour.Status previousStatus,
    TrackableBehaviour.Status newStatus)
    {
    if (newStatus == TrackableBehaviour.Status.DETECTED ||
    newStatus == TrackableBehaviour.Status.TRACKED ||
    newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    {
    ////Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    OnTrackingFound();
    if ((mTrackableBehaviour.TrackableName == "CortegeMacabre"))
    {
    //Get materials from the mesh renderer
    mats = GetComponentInChildren<MeshRenderer> ().materials;

    //Make only voix-off text NOT transparent initially.
    // Only element 1 here is visible as it is the last element entered (the text).
    InitializeMaterials (mats);

    //Cancel all calls to Invoke to avoid a mess when target is lost and found
    // again many times before the audio or the fadeInOut complete.
    CancelInvoke ("FadeWhenSoundCompleted1_0");
    //CancelInvoke ("DisplayVideoWhenFadingCompleted");

    //Disable the Game Object (screen) "VideoRoueAGodets", make it invisible at the beginning
    this.vPlayer.gameObject.SetActive(false);

    //Play the voix off
    asource.Play();

    //When the voix off has completed, do the fading
    Invoke ("FadeWhenSoundCompleted1_0", aClip.length + 2f);

    //When fading has completed, display the video
    //Invoke("DisplayVideoWhenFadingCompleted", aClip.length + 6f);
    }
    }
    else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
    newStatus == TrackableBehaviour.Status.NOT_FOUND)
    {
    Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    OnTrackingLost();
    if ((mTrackableBehaviour.TrackableName == "CortegeMacabre"))
    {
    asource.loop = false;
    asource.Stop ();
    vPlayer.Stop ();
    }
    }
    else
    {
    // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
    // Vuforia is starting, but tracking has not been lost or found yet
    // Call OnTrackingLost() to hide the augmentations
    OnTrackingLost();
    }
    }
    #endregion // PUBLIC_METHODS
    #region PRIVATE_METHODS
    //Called when any of the image target is tracked
    protected virtual void OnTrackingFound()
    {
    //Target detected
    targetDetected = true;
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    // Enable rendering:
    foreach (var component in rendererComponents)
    component.enabled = true;
    // Enable colliders:
    foreach (var component in colliderComponents)
    component.enabled = true;
    // Enable canvas':
    foreach (var component in canvasComponents)
    component.enabled = true;
    }
    protected virtual void OnTrackingLost()
    {
    //Target detected
    targetDetected = false;
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    // Disable rendering:
    foreach (var component in rendererComponents)
    component.enabled = false;
    // Disable colliders:
    foreach (var component in colliderComponents)
    component.enabled = false;
    // Disable canvas':
    foreach (var component in canvasComponents)
    component.enabled = false;
    }
    #endregion // PRIVATE_METHODS
    }
     

    Attached Files: