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
3
Question by Tinus · Jun 14, 2010 at 08:47 PM · staticinitializationconstructordesign-patterns

Static Initialization

Is there any way to achieve static initialization in Unity?

Java allows you to do this within a class (static block):

static { // code }

C# allows you to do this within a class (static constructor):

static MyClass() { // code }

However, in Unity the static constructor only seems to get called after recompilation. This is the same as normal constructors then; but while we have Awake() to replace those, I'm unaware of anything that can be used for static initialization.

What would I use it for? Well, there's a bunch of design patterns that I'm aching to use in my game, such as Pluggable Factory. That pattern relies on the fact that subclasses of a product class can register themselves with a Maker before it is used to construct anything.

Using Pluggable factories allows you to specify any number of additional product subclasses without having to alter the code of the Maker that produces them, which means it is very easily to extend a framework with additional functionality.

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
3
Best Answer

Answer by Mike 3 · Jun 14, 2010 at 08:58 PM

You can use normal, non-monobehaviour classes with unity

This lets you use the static constructor for your factory without any problems

That way you can instantiate all your factories from a monobehaviour, and they'll all work without issue

Comment
Add comment · Show 3 · 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 Tinus · Jun 15, 2010 at 10:14 AM 0
Share

Actually, that is what I'm doing. However, a class' static constructor only seems to be called after I've made a change in that class' code to force recompilation...

At least, that is how it seems to work in the editor. I haven't tested it with built executables.

avatar image Tinus · Jun 15, 2010 at 10:22 AM 0
Share

"That way you can instantiate all your factories from a monobehaviour, and they'll all work without issue"

Wait, are you suggesting using a monobehaviour to call initialization routines on all the factories? That could work, but it kind of violates encapsulation. You then still need an external class to know about all subclasses, which forces additional maintenance, which means they're not very 'pluggable'.

Or am I missing something? I might be confusing some things..

avatar image Mike 3 · Jun 15, 2010 at 10:57 AM 0
Share

I was actually proposing your subclasses weren't $$anonymous$$onoBehaviours either (You could send in a GameObject as a dependency to the constructor or an inherited function if you need it to act on something). Obviously that isn't always a viable solution though. Re. your first comment - the static constructor should work fine in a built player

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

No one has followed this question yet.

Related Questions

class constructor/order/initialization confusion 1 Answer

Is it possible to control the order of a static's initialization? 2 Answers

What the best way to initialize constant static data in a MonoBehavior? 0 Answers

Design pattern: Loading custom data into prefab objects 0 Answers

Assigning a GameObject Script Variable Before Start is Called 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