• 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 /
  • Help Room /
avatar image
Question by Ziplock9000 · Jun 03, 2017 at 06:09 PM · prefabsfontprefab-instance

Update a prefab before it's initially calculated on instantiation?

I have a small prefab that is a progress bar that displays a value, label via TextMeshPro and obviously a progress bar which is just a filled image. I programmatically instantiate this prefab and set it's values vias code.

The problem is that behind the scenes UI calculations are performed twice. Once when the prefab is instantiated, with the sizes, labels etc all as they were when the prefab was designed. Then again when I set the values of the prefab instance.

Sure you'll never see these initial values, but nevertheless there's calculations going on behind the scenes for fonts etc that is just wasted CPU cycles.

It's almost as if you can pass values into a prefab constructor.. or tell it not to do ANY calculations/ rasterization of fonts, etc until you tell it?

Maybe it doesn't work like that and uses a "dirty" flag and does the calculations on the first frame it needs to be displayed? I just dont know.

Some clarification would be nice.

Comment

People who like this

0 Show 0
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

Answer by Soraphis · Feb 24, 2019 at 12:02 AM

well this post is a bit dated, but its not outdated, so I'll just answer it:

 

before i'm going to answer your question a word of warning:

Sure you'll never see these initial values, but nevertheless there's calculations going on behind the scenes for fonts etc that is just wasted CPU cycles.

what you're doing is premature optimization / micro-optimatzions. spending time on stuff like this is wasted time, unless you're instantiating 20+ progressbars each frame. optimize your Update/FixedUpdate methods. don't render stuff that is not visible. don't allocate memory if you don't have to (so the GC won't bother you). and always use the profiler before optimization, so you know where to optimize for the best benefit.

 

to answer your question:

if you call Instantiate the gameobject is not immediately instantiated or rendered. but you instantly get a reference to the GO. just make all those changes you'd make right after the instantiation. the gameobject is initialized before the next frame, it will use those new values then.

While Awake is called before your changes apply, Start will have those changes, and Start is called before the first Update method on the object is called -> before it renders the first time.

alternatively:

disable the prefab before instantiation. this will make the gameobject instantiate disabled, do your changes to the components and enable it. in this case the awake method will have the updated values, too.


again: always use the profiler before optimization. don't trust anybody, test/benchmark stuff yourself.

edit: this site has the worst markdown renderer ever -.-

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

107 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

Related Questions

Any other ways for instantiated prefabs to reference game-objects in the scene? 0 Answers

How to make a prefab a parent to other prefabs? 1 Answer

Using Prefab Information in a Class? 0 Answers

Internal Unity Error when trying to save a Prefab. 2 Answers

Problems with instantiating 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