Guest User

Untitled

a guest
Jan 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class BookDisplay : MonoBehaviour {
  7.  
  8.     public BookAsset thisBookAsset;     //The scriptable object that contains info about this book.
  9.     public Canvas bookCanvasLeft;       //The canvas that the content of the book will be displayed on.
  10.     public Canvas bookCanvasRight;      //The canvas that the content of the book will be displayed on.
  11.  
  12.     public TextMesh frontBookTitle;     //The text title of the book.
  13.     public Text bookTextLeft;           //The UI text where the book content will show.
  14.     public Text bookTextRight;          //The UI text where the book content will show.
  15.     public Button nextBtn;              //The button to turn the page.
  16.     public Button prevBtn;              //The button to go to the previous page.
  17.  
  18.     Animator anim;                      //Referance to the animator.
  19.     List<string> pages;                 //A list of pages to be displayed on the book.
  20.     int pageNum = -2;                   //The page number the coresponding page will be displayed on.
  21.  
  22.  
  23.     private void Start () {
  24.         //---------Referances-------------------------------------------------------------------------------------
  25.         anim = (anim == null ? GetComponent<Animator>() : anim);
  26.         frontBookTitle = (frontBookTitle == null ? GetComponentInChildren<TextMesh>() : frontBookTitle);
  27.        
  28.         frontBookTitle.text = thisBookAsset.title.Replace("\\n", "\n");
  29.         pages = TextPages(thisBookAsset.content, 30, 20);
  30.         pages.Insert(0, "---");
  31.         pages.Insert(1, "\n\n\n\n\n"+ thisBookAsset.title.Replace("\\n", " ") + "\n\n\n\n" + thisBookAsset.blurb);
  32.         //---------------------------------------------------------------------------------------------------------
  33.  
  34.         bookCanvasLeft.gameObject.SetActive(false);
  35.         bookCanvasRight.gameObject.SetActive(false);
  36.     }
  37.  
  38.     public void Activate()
  39.     {
  40.         bookCanvasLeft.gameObject.SetActive(true);
  41.         bookCanvasRight.gameObject.SetActive(true);
  42.  
  43.         nextBtn.onClick.AddListener(NextButton);
  44.         prevBtn.onClick.AddListener(PreviousButton);
  45.  
  46.         prevBtn.interactable = true;
  47.         nextBtn.interactable = true;
  48.  
  49.         NextButton();
  50.  
  51.     }
  52.     public void Deactivate()
  53.     {
  54.         bookCanvasLeft.gameObject.SetActive(false);
  55.         bookCanvasRight.gameObject.SetActive(false);
  56.     }
  57.  
  58.  
  59.     //Called when next page button is clicked.
  60.     private void NextButton()
  61.     {
  62.         anim.SetTrigger("flipForward");
  63.  
  64.         pageNum = pageNum + 2;
  65.         PopulatePage(pageNum,pageNum+1);
  66.        
  67.     }
  68.  
  69.     //Called when previous page button is clicked.
  70.     private void PreviousButton()
  71.     {
  72.         anim.SetTrigger("flipBack");
  73.  
  74.         pageNum = pageNum - 2;
  75.         PopulatePage(pageNum, pageNum+1);
  76.     }
  77.  
  78.     //Populate the right and left page of the book.
  79.     private void PopulatePage(int lPage, int rPage)
  80.     {
  81.         //Display data.
  82.         if (lPage > 0 && lPage < pages.Count) bookTextLeft.text = pages[lPage];
  83.         else bookTextLeft.text = "";
  84.         if (rPage > 0 && rPage < pages.Count) bookTextRight.text = pages[rPage];
  85.         else bookTextRight.text = "";
  86.  
  87.  
  88.         //Check wether or not to show next/prev button.
  89.         if (pageNum <= 0)
  90.         {
  91.             prevBtn.interactable = false;
  92.         }
  93.         else
  94.         {
  95.             prevBtn.interactable = true;
  96.         }
  97.  
  98.  
  99.         if (pageNum >= pages.Count-2)
  100.         {
  101.             nextBtn.interactable = false;
  102.         }
  103.         else
  104.         {
  105.             nextBtn.interactable = true;
  106.         }
  107.  
  108.     }
  109.  
  110.  
  111.  
  112.     /// <summary>
  113.     /// Seperates a long string into pages returns strings as list.
  114.     /// </summary>
  115.     private List<string> TextPages(string text, int charPerLine, int linesPerPage)
  116.     {
  117.         List<string> parts = new List<string>();
  118.         List<string> pages = new List<string>();
  119.         //----------------Make a line---------------------------------------------------
  120.         while (text.Length > 0)
  121.         {
  122.             if (text.Length <= charPerLine)
  123.             {
  124.                 parts.Add(text);
  125.                 text = string.Empty;
  126.             }
  127.             else
  128.             {
  129.                 int length = charPerLine;
  130.                 while (char.IsLetter(text[length]) && char.IsLetter(text[length - 1]))
  131.                 {
  132.                     length--;
  133.                 }
  134.  
  135.                 parts.Add(text.Substring(0, length).Trim());
  136.                 text = text.Substring(length);
  137.             }
  138.         }
  139.         //-----------Make a page---------------------------------------------------------
  140.         int count = 0;
  141.         string page = "";
  142.  
  143.         for (int i = 0; i < parts.Count; i++)
  144.         {
  145.             count++;
  146.             page += parts[i] + "\n";
  147.  
  148.             if (count >= linesPerPage)
  149.             {
  150.                 pages.Add(page);
  151.                 page = "";
  152.                 count = 0;
  153.             }
  154.             if (i == parts.Count - 1)
  155.             {
  156.                 // this is the last item in the list
  157.                 pages.Add(page);
  158.             }
  159.         }
  160.         return pages;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment