Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DevMan5000 · Jul 02, 2015 at 11:47 AM · uiprefabprefabsdatangui

Injecting data into Prefabs

Hi,

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)?

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by komodor · Jul 02, 2015 at 11:51 AM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DevMan5000 · Jul 02, 2015 at 03:51 PM 0
Share

Thanks for your response, komodor.

"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)"

This is the approach I would like to use, but the "fill it with data part" is the crux of my problem.

In the code above, I have an array and I am pairing a prefab with data to be loaded into it. I would like to do this in a data driven, editor way, ins$$anonymous$$d of in code.

Is there a way in the editor to have an array which pairs prefab and data?

 Element 0:
 Prefab: CoverPagePrefab
 Loc$$anonymous$$ey: "coverPage"
 Btn1Loc$$anonymous$$ey: "coverPageBtn1"
 Btn2Loc$$anonymous$$ey: "coverPageBtn2"
 Btn3Loc$$anonymous$$ey: "coverPageBtn3"
 
 Element 1:
 Prefab: StoryPagePrefab
 Loc$$anonymous$$ey: "storyPage1"
 Img: Page1Img

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Runtime Changes to UI Object From Prefab Unexpectedly Changing All Prefab Objects 1 Answer

SetActive true not working on UI object 2 Answers

UIPrefab from prefab Canvas to live Canvas 1 Answer

Canvas with GUI elements in prefab act strangely 0 Answers

Display text above prefabs in Unity 4.6 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges