Note: This script is no longer necessary from AC 1.78 onwards, as pages can now be assigned textures from within the Editor.
Journals and Documents provide a way of displaying multiple pages of text to the player. This script allows you to associate each page with a texture, and display that texture in a Graphic menu element.
To use it:
- Create a new journal or document as normal using the Menu system. The pages must have text - if you don't want to show them, they can be hidden from view.
- Create a new Graphic element in the Menu for page textures to be displayed in.
- Add the page text to the Speech Manager by clicking "Gather text".
- Add the script below (JournalPageTexture.cs) as a component in your scene. This should be on a prefab present in all scenes (such as the Player) for it to be recognised game-wide.
- Fill in the Inspector fields, referencing the Menu and Element names involved. Use the "Page Textures" array to define a texture for each page. To connect a texture asset to a page, refer to that page's ID number as listed in the Speech Manager.
JournalPageTexture.cs:
using UnityEngine;
using System.Collections;
using AC;
public class JournalPageTexture : MonoBehaviour
{
[SerializeField] private PageTexture[] pageTextures;
[SerializeField] private string journalMenuName = "Journal";
[SerializeField] private string journalElementName = "PageText";
[SerializeField] private string graphicElementName = "Texture";
[SerializeField] private Texture2D emptyTexture;
private void OnEnable ()
{
EventManager.OnMenuTurnOn += OnMenuTurnOn;
EventManager.OnMenuElementShift += OnMenuElementShift;
}
private void OnDisable ()
{
EventManager.OnMenuTurnOn -= OnMenuTurnOn;
EventManager.OnMenuElementShift -= OnMenuElementShift;
}
private void OnMenuTurnOn (Menu menu, bool isInstant)
{
if (menu.title == journalMenuName)
{
UpdatePageTexture ();
}
}
private void OnMenuElementShift (MenuElement _element, AC_ShiftInventory shiftType)
{
if (PlayerMenus.GetMenuWithName (journalMenuName).elements.Contains (_element))
{
UpdatePageTexture ();
}
}
private void UpdatePageTexture ()
{
MenuJournal journal = PlayerMenus.GetElementWithName (journalMenuName, journalElementName) as MenuJournal;
int pageIndex = journal.showPage - 1;
MenuGraphic graphic = PlayerMenus.GetElementWithName (journalMenuName, graphicElementName) as MenuGraphic;
Texture2D texture = emptyTexture;
if (pageIndex < journal.pages.Count)
{
int pageID = journal.pages [pageIndex].lineID;
texture = GetTextureWithID (pageID);
}
if (texture)
{
graphic.graphic.texture = texture;
graphic.graphic.ClearCache ();
}
}
private Texture2D GetTextureWithID (int ID)
{
foreach (PageTexture pageTexture in pageTextures)
{
if (pageTexture.ID == ID)
{
return pageTexture.texture;
}
}
return null;
}
[System.Serializable]
private struct PageTexture
{
public int ID;
public Texture2D texture;
}
}