Forum rules - please read before posting.

automatically turn document page

Hi to all.
AC is friendly, clear and powerfull solution. Thanks, Chris, for your work.
On DocumentUI GO I have the following script to voice over document page:

using UnityEngine;
using AC;

public class DocumentsController : MonoBehaviour
{
    private AudioSource _audio;

    void OnEnable()
    {
        if (KickStarter.player != null)
        {
        _audio = KickStarter.player.speechAudioSource;
            Play(KickStarter.runtimeDocuments.ActiveDocument);
        };
        EventManager.OnMenuElementShift += OnMenuElementShift;
        EventManager.OnMenuTurnOff += OnMenuTurnOff;
    }

    private void OnMenuTurnOff(Menu _menu, bool isInstant)
    {
        _audio.Stop();
    }

    private void OnMenuElementShift(MenuElement _element, AC_ShiftInventory shiftType)
    {
        if (_element is MenuJournal)
            Play(KickStarter.runtimeDocuments.ActiveDocument);
    }

        void Play(Document doc)
        {
            _audio.Stop();
                            int last_page = KickStarter.runtimeDocuments.GetLastOpenPage(doc)-1;
        int line_id = doc.pages[last_page].lineID;
                                    AudioClip clip = KickStarter.runtimeLanguages.GetSpeechAudioClip(line_id, KickStarter.player);
            if (clip != null)
                _audio.PlayOneShot(clip);
                                            }

            void OnDisable()
    {
        EventManager.OnMenuElementShift -= OnMenuElementShift;
        EventManager.OnMenuTurnOff -= OnMenuTurnOff;
    }
        }

All works great, but i need some more functionality. When AudioClip for page is finished playing, I need to automatically turn to next page. And if it is last page, automatically close DocumentUI.
I think, that in Play() method I need to add something like:
StartCoroutine(TurnPage(clip.length));

and:
IEnumerator TurnPage(float time)
{
yield return new WaitForSeconds(time);
//Turn page or close document//
}

But I dont know, how to call method, which handles the buttons (NextPage and CloseDocument) click. Or may be there is more simple way to do this?

Comments

  • Welcome to the community, @BlindRainGames.

    Yes - so long as the game isn't paused while the Document is open, then a coroutine should be able to do the job.

    You don't need to access the NextPage or CloseDocument buttons - you can modify the Menu and Journal directly.

    Try this:

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class DocumentsController : MonoBehaviour
    {
    
        private AudioSource _audio;
    
    
        private void OnEnable ()
        {
            if (KickStarter.player)
            {
                _audio = KickStarter.player.speechAudioSource;
                Play (KickStarter.runtimeDocuments.ActiveDocument);
            }
    
            EventManager.OnMenuElementShift += OnMenuElementShift;
            EventManager.OnMenuTurnOff += OnMenuTurnOff;
        }
    
    
        private void OnDisable ()
        {
            EventManager.OnMenuElementShift -= OnMenuElementShift;
            EventManager.OnMenuTurnOff -= OnMenuTurnOff;
        }
    
    
        private void OnMenuTurnOff (Menu _menu, bool isInstant)
        {
            _audio.Stop ();
        }
    
    
        private void OnMenuElementShift(MenuElement _element, AC_ShiftInventory shiftType)
        {
            if (_element is MenuJournal)
            {
                Play (KickStarter.runtimeDocuments.ActiveDocument);
            }
        }
    
    
        private void Play (Document doc)
        {
            if (doc == null) return;
    
            _audio.Stop ();
            int last_page = KickStarter.runtimeDocuments.GetLastOpenPage (doc) - 1;
            int line_id = doc.pages[last_page].lineID;
            AudioClip clip = KickStarter.runtimeLanguages.GetSpeechAudioClip (line_id, KickStarter.player);
            if (clip)
            {
                _audio.PlayOneShot (clip);
            }
    
            StopAllCoroutines ();
            StartCoroutine (TurnPage (clip.length));
        }
    
    
        private IEnumerator TurnPage (float time)
        {
            yield return new WaitForSeconds (time);
    
            Menu myMenu = PlayerMenus.GetMenuWithName ("MyMenu");
            MenuJournal journalToShift = (MenuJournal) myMenu.GetElementWithName ("MyJournal");
            int oldPageIndex = journalToShift.showPage;
    
            journalToShift.Shift (AC_ShiftInventory.ShiftNext, false, 1);
            journalToShift.RecalculateSize (myMenu.menuSource);
            myMenu.Recalculate ();
    
            int newPageIndex = journalToShift.showPage;
    
            if (newPageIndex != oldPageIndex)
            {
                myMenu.TurnOff ();
            }
        }
    
    }
    
  • Thanks, @ChrisIceBox. I found the following solution. May be, I don't understand something, but it works.

    IEnumerator TurnPage(float time)
    {
    yield return new WaitForSeconds(time);
    Menu menu = KickStarter.playerMenus.GetMenuWithCanvas(GetComponent());
    MenuElement btn = null;
    //_journal is MenuJournal var.
    if (_journal.GetCurrentPageNumber() == KickStarter.runtimeDocuments.ActiveDocument.pages.Count)
    btn = menu.GetElementWithName("CloseButton");
    else
    btn = menu.GetElementWithName("ShiftRight");

            btn.ProcessClick(menu, 0, MouseState.SingleClick);
        }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.