Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class BookDisplay : MonoBehaviour {
- public BookAsset thisBookAsset; //The scriptable object that contains info about this book.
- public Canvas bookCanvasLeft; //The canvas that the content of the book will be displayed on.
- public Canvas bookCanvasRight; //The canvas that the content of the book will be displayed on.
- public TextMesh frontBookTitle; //The text title of the book.
- public Text bookTextLeft; //The UI text where the book content will show.
- public Text bookTextRight; //The UI text where the book content will show.
- public Button nextBtn; //The button to turn the page.
- public Button prevBtn; //The button to go to the previous page.
- Animator anim; //Referance to the animator.
- List<string> pages; //A list of pages to be displayed on the book.
- int pageNum = -2; //The page number the coresponding page will be displayed on.
- private void Start () {
- //---------Referances-------------------------------------------------------------------------------------
- anim = (anim == null ? GetComponent<Animator>() : anim);
- frontBookTitle = (frontBookTitle == null ? GetComponentInChildren<TextMesh>() : frontBookTitle);
- frontBookTitle.text = thisBookAsset.title.Replace("\\n", "\n");
- pages = TextPages(thisBookAsset.content, 30, 20);
- pages.Insert(0, "---");
- pages.Insert(1, "\n\n\n\n\n"+ thisBookAsset.title.Replace("\\n", " ") + "\n\n\n\n" + thisBookAsset.blurb);
- //---------------------------------------------------------------------------------------------------------
- bookCanvasLeft.gameObject.SetActive(false);
- bookCanvasRight.gameObject.SetActive(false);
- }
- public void Activate()
- {
- bookCanvasLeft.gameObject.SetActive(true);
- bookCanvasRight.gameObject.SetActive(true);
- nextBtn.onClick.AddListener(NextButton);
- prevBtn.onClick.AddListener(PreviousButton);
- prevBtn.interactable = true;
- nextBtn.interactable = true;
- NextButton();
- }
- public void Deactivate()
- {
- bookCanvasLeft.gameObject.SetActive(false);
- bookCanvasRight.gameObject.SetActive(false);
- }
- //Called when next page button is clicked.
- private void NextButton()
- {
- anim.SetTrigger("flipForward");
- pageNum = pageNum + 2;
- PopulatePage(pageNum,pageNum+1);
- }
- //Called when previous page button is clicked.
- private void PreviousButton()
- {
- anim.SetTrigger("flipBack");
- pageNum = pageNum - 2;
- PopulatePage(pageNum, pageNum+1);
- }
- //Populate the right and left page of the book.
- private void PopulatePage(int lPage, int rPage)
- {
- //Display data.
- if (lPage > 0 && lPage < pages.Count) bookTextLeft.text = pages[lPage];
- else bookTextLeft.text = "";
- if (rPage > 0 && rPage < pages.Count) bookTextRight.text = pages[rPage];
- else bookTextRight.text = "";
- //Check wether or not to show next/prev button.
- if (pageNum <= 0)
- {
- prevBtn.interactable = false;
- }
- else
- {
- prevBtn.interactable = true;
- }
- if (pageNum >= pages.Count-2)
- {
- nextBtn.interactable = false;
- }
- else
- {
- nextBtn.interactable = true;
- }
- }
- /// <summary>
- /// Seperates a long string into pages returns strings as list.
- /// </summary>
- private List<string> TextPages(string text, int charPerLine, int linesPerPage)
- {
- List<string> parts = new List<string>();
- List<string> pages = new List<string>();
- //----------------Make a line---------------------------------------------------
- while (text.Length > 0)
- {
- if (text.Length <= charPerLine)
- {
- parts.Add(text);
- text = string.Empty;
- }
- else
- {
- int length = charPerLine;
- while (char.IsLetter(text[length]) && char.IsLetter(text[length - 1]))
- {
- length--;
- }
- parts.Add(text.Substring(0, length).Trim());
- text = text.Substring(length);
- }
- }
- //-----------Make a page---------------------------------------------------------
- int count = 0;
- string page = "";
- for (int i = 0; i < parts.Count; i++)
- {
- count++;
- page += parts[i] + "\n";
- if (count >= linesPerPage)
- {
- pages.Add(page);
- page = "";
- count = 0;
- }
- if (i == parts.Count - 1)
- {
- // this is the last item in the list
- pages.Add(page);
- }
- }
- return pages;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment