Forum rules - please read before posting.

Opening document in specific page

This is a custom script that opens a document in a certain page.

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{
    [System.Serializable]
    public class ActionDocumentOpenAtPage : Action
    {
        public int documentID;

        // 1-based page number, as shown in the Journal element
        public int pageNumber = 1;

        // The menu / Journal element that displays the document
        public string menuName = "Document";
        public string elementName = "PageText";

        // Seconds to wait after opening the document, before jumping to the page
        public float delay = 0.1f;


        public ActionDocumentOpenAtPage()
        {
            this.isDisplayed = true;
            category = ActionCategory.Document;
            title = "Open Document at page";
            description = "Opens a Document and jumps to a specific page of its Journal element.";
        }


        public override float Run()
        {
            if (!isRunning)
            {
                if (KickStarter.inventoryManager == null || KickStarter.runtimeDocuments == null)
                {
                    return 0f;
                }

                Document document = KickStarter.inventoryManager.GetDocument(documentID);
                if (document == null)
                {
                    ACDebug.LogWarning("Cannot open document with ID " + documentID + " - not found.");
                    return 0f;
                }

                // Open the document (this also makes it the active document)
                KickStarter.runtimeDocuments.OpenDocument(document);

                if (delay > 0f)
                {
                    // Wait, then Run () is called again with isRunning = true
                    isRunning = true;
                    return delay;
                }

                SetPage();
                return 0f;
            }
            else
            {
                isRunning = false;
                SetPage();
                return 0f;
            }
        }


        public override void Skip()
        {
            // If the cutscene is skipped, still land on the right page
            isRunning = false;
            SetPage();
        }


        private void SetPage()
        {
            MenuElement element = PlayerMenus.GetElementWithName(menuName, elementName);
            MenuJournal journal = element as MenuJournal;

            if (journal != null)
            {
                journal.showPage = Mathf.Max(1, pageNumber);

                Menu menu = PlayerMenus.GetMenuWithName(menuName);
                if (menu != null)
                {
                    menu.Recalculate();
                }
            }
            else
            {
                ACDebug.LogWarning("Cannot find Journal element '" + elementName + "' in menu '" + menuName + "'.");
            }
        }


#if UNITY_EDITOR

        public override void ShowGUI()
        {
            documentID = EditorGUILayout.IntField("Document ID:", documentID);
            pageNumber = EditorGUILayout.IntField("Page number (1-based):", pageNumber);
            menuName = EditorGUILayout.TextField("Menu name:", menuName);
            elementName = EditorGUILayout.TextField("Journal element name:", elementName);
            delay = EditorGUILayout.FloatField("Delay (seconds):", delay);
        }


        public override string SetLabel()
        {
            return "Document " + documentID + ", page " + pageNumber;
        }

#endif
    }
}
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.