Injecting data into Prefabs


I have a book that consists of pages. Each page has a prefab and some data associated with it. When you turn the page, the app will instantiate the next page’s prefab (mostly textboxes), and then load the data into the instance, thus filling out the text.

// Create the cover page data
CoverPageData cpd = new CoverPageData( );
cpd.LocKey = "cover_page_text";
cpd.BtnLocKey = "start_btn_text";
cpd.BkgTexture = "title_page";

// Create the story page data
StoryPageData spd1 = new StoryPageData( );
spd1.LocKey = "page_1_text";
spd1.BkgTexture ="image_1";

StoryPageData spd2 = new StoryPageData( );
spd2.LocKey = "page_2_text";
spd2.BkgTexture ="image_2";

// Add pages to PageArr. Each page couples a prefab with data.
PageArr.add(new Page(CoverPagePrefab, cpd));
PageArr.add(new Page(StoryPagePrefab, spd1));
PageArr.add(new Page(StoryPagePrefab, spd2));

I want to keep things robust and data driven. I am new to Unity. Is this the best way to handle dealing with prefabs and data?

Is there an editor solution where I could be creating this same concept (not in code, but through editor gameobjects/prefabs instead)?


i would make one prefab for each layout and create compoennt for each prefab type which will fill it with data, then you can just instantiate right prefab and fill it with data from your data source (text asset, xml, json)

other approach would be to create the text boxes by code, so you could have just one prefab with component which will recreate the layout and then fill it with data

or you can stay in the middle - one prefab with “layers” or container objects with layouts which are hidden while one of them is displayed with correct data