Skip to content

Instantly share code, notes, and snippets.

@Dandarawy
Created February 7, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dandarawy/5cbbd8b87ec12b08aaf657c00dccc6b1 to your computer and use it in GitHub Desktop.
Save Dandarawy/5cbbd8b87ec12b08aaf657c00dccc6b1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DynamicPapersUpdater : MonoBehaviour {
public GameObject FrontPagePrefab;
public GameObject BackPagePrefab;
public BookPro book;
// Use this for initialization
void Start () {
}
public void AddPaper()
{
GameObject frontPage = Instantiate(FrontPagePrefab);
GameObject backPage = Instantiate(BackPagePrefab);
frontPage.transform.SetParent(book.transform,false);
backPage.transform.SetParent(book.transform, false);
Paper newPaper = new Paper();
newPaper.Front = frontPage;
newPaper.Back = backPage;
Paper[] papers = new Paper[book.papers.Length + 1];
for(int i=0;i< book.papers.Length;i++)
{
papers[i] = book.papers[i];
}
papers[papers.Length-1] = newPaper;
book.papers = papers;
//update the flipping range to contain the new added paper
book.EndFlippingPaper = book.papers.Length - 1;
book.UpdatePages();
}
public void RemovePaperAt(int index)
{
if(index>=0&&index<book.papers.Length)
{
List<Paper> papers = new List<Paper>(book.papers);
Paper paperToDestroy = papers[index];
papers.RemoveAt(index);
book.papers = papers.ToArray();
if (book.currentPaper >= book.papers.Length)
book.currentPaper = book.papers.Length - 1;
book.EndFlippingPaper = book.papers.Length - 1;
Destroy(paperToDestroy.Back);
Destroy(paperToDestroy.Front);
book.UpdatePages();
}
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment