Created
February 7, 2018 13:33
-
-
Save Dandarawy/5cbbd8b87ec12b08aaf657c00dccc6b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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