Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jul 22, 2020 at 05:25 AM by aadityakirans for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by aadityakirans · Jul 06, 2020 at 01:54 AM · uiprefabsruntime-generation

[SOLVED] UI issue when instantiating UI prefabs at runtime.

Hello, I had been working on this mobile app that was primarily UI based. There was this timeline and gallery module inside it. To make the modules extensible and modular, I tried to set everything up so that the UI elements are all spawned at runtime, this way, I thought that to change anything later on, all I needed to do was to tweak some values in the inspector.

But what happened was that on spawning the prefabs, in lower resolutions, the UI was fine but at higher resolutions, the UI seemed to overflow to the right of the screen as shown in the images below.

alt text

alt text

The code that I use to spawn the prefabs on screen is as follows:

void SpawnTimelineElement() { for (int i = 0; i < spawnCount; i++) { GameObject element = Instantiate(timelineElementPrefab) as GameObject; element.transform.SetParent(contentScreen.transform, false); element.GetComponent<TimelineElement>().PopulateElement(timelineData.dataList[i]); } }

This function is called in void Start();. The timeline in this case is inside a Scroll view and the content size fitter has horizontal fit: min size and vertical fit: proffered size. Vertical layout group has the padding and spacing 0, child alignment to the upper left, use child scale ticked on width and height and child force expanded only ticked on width.

I managed to get this thing to work on all resolutions when I placed all the objects in the scene instead of spawning them on start but that really isn't an ideal solution.

Earlier I didn't use false along with the set parent method in the code above. Because of this the UI used to break in unpredictable ways, I saw this in some forum and I used it but still, things aren't perfect. Any help is greatly appreciated and thanks in advance.

small-display.png (198.0 kB)
iphonw-x-png.png (180.8 kB)
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by aadityakirans · Jul 22, 2020 at 05:22 AM

Hey so a friend of mine called Gopee helped me solve this issue for the timeline. Here are the 3 things that he did to fix the issue.

1) Added a layout element to the UI prefab that was being instantiated and checked proffered height on it.
2) Changed the vertical layout group parameters on the Content in which all these prefabs are instantiated. Currently the parameters that are checked on the Vertical Layout group are control child size ticked on width and child force expand ticked on width.
3) But doing all this was somehow weirdly affecting the scale of the prefabs instantiated. So, in the code where the prefab was instantiated, the following lines were added.

 RectTransform rect = element.transform as RectTransform;                                  
 
 rect.localScale = Vector3.one;
Comment
Add comment · 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
0

Answer by ranch000 · Jul 07, 2020 at 08:42 AM

In your vertical Layout group, See that The following properties are checked:

Child Control Size : Width

Child Force Expand : Width Height

Also ensure if the scroll view itself is anchored and has no fixed width and height

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 aadityakirans · Jul 10, 2020 at 02:41 PM 0
Share

Thank you sir but as you have said, the Scroll view I've already anchored. Please see the image below. Is this a Unity Bug I wonder???

alt text

scrollview-anchor.png (167.3 kB)

Follow this Question

Answers Answers and Comments

219 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Children of prefab disappear in Hierarchy when prefab parent is clicked,Children of prefab disappear in Hierarchy when prefab is clicked 2 Answers

how do i link menu options to a button? 0 Answers

I need help with prefabs 1 Answer

How to make a crouch button for mobile? 2 Answers

Injecting data into Prefabs 1 Answer


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