|
Post by ryanmccauley211 on Feb 7, 2023 20:23:33 GMT
How do I go about adding pages programmatically at startup? I create the component with:
endlessBook = gameObject.AddComponent<EndlessBook>();
Then I add a page like:
_endlessBook.AddPageData(); The issue is that when it initializes the pages are not there. If I do it on Update() they show but I would like to know if I can do it on initialization?
Thanks in advance and for this great asset.
|
|
|
Post by echo17 on Feb 8, 2023 9:48:53 GMT
Thanks for the kind words!
Be sure you are adding your pages in the Start method of your script, not the Awake. EndlessBook has to do some initialization in its Awake method. Generally speaking, in Unity you should not be making function calls or retrieving the state (variable values, etc.) on any other components in your Awake method because that is when each component does their initialization. The Start method is the best place because you can be assured all other components should be ready to use.
Here is the code I put in Start to add a new page:
private void Start() { book.AddPageData(); }
I did notice in your code you set a variable called endlessBook, but you later reference it as _endlessBook (with an underscore). That might be causing some issues.
FYI, I just submitted a new version of EndlessBook (1.11.0) that will allow you to pass an optional material to AddPageData and InsertPageData. This will make it easier to set up the material without having to retrieve the page data, set the material, then refresh the book.
|
|
|
Post by ryanmccauley211 on Feb 8, 2023 12:42:32 GMT
Sorry I made a typo in the last post leaving out the underscore, but the underscore is in the code. Might be easier if I show the full method.
private EndlessBook _endlessBook;
// Start is called before the first frame update
void Start()
{
_endlessBook = gameObject.AddComponent<EndlessBook>();
_endlessBook.SetPageFillerMaterial(defaultPageFillerMaterial);
_endlessBook.AddPageData();
_endlessBook.enabled = true;
}
So I am using the start method as you recommend and I do not override the Awake() method, but pages still do not get added for some reason. Do you have any other ideas why the above wouldn't work?
EDIT:
I realised that it seems to be this line causing the issue
_endlessBook.SetPageFillerMaterial(defaultPageFillerMaterial);
It calls SetPageNumber() and when I comment out this line the pages show up, even when I call it after AddPageData() it still doesn't work correctly. Maybe I am thinking about it wrong but my idea was to set the default filler then add pages so that the pages get initialised with the filler material?
Thanks again.
|
|
|
Post by echo17 on Feb 9, 2023 10:37:15 GMT
Thank you for the added code, that helped me see what is the problem. You are adding a book component in the start method? This means the book will not be initialized by the time you call any methods after that. The book component should be added at design time, not run time. if you are needing a book to show up dynamically, I would suggest creating the object at design time and just hide it. You can then show it when you need to.
|
|