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
2
Question by mansiva2000 · Jul 10, 2013 at 08:21 PM · colornewpropertiesefficiencyproperty fields

Efficiency of static properties

While I was implementing a class for mixing colors I started creating a list of properties defining key colors I wanted to use in the same way they are done in Unity's Color struct:

 struct BubbleColor {
     public enum Name { white, red, orange, yellow, green, blue, purple, brown };
 
     public Name name;
     public Color color;
 
     private BubbleColor(Name name, Color color)
     {
         this.name = name;
         this.color = color;
     }
 
     // I'm only putting a couple of colors to show how it's done
     public static BubbleColor purple {
         get { return new BubbleColor(Name.purple, new Color (0.5f, 0f, 1f, 1f)); }
     }
     public static BubbleColor brown {
         get { return new BubbleColor(Name.brown, new Color (0.6f, 0.25f, 0.1f, 1f)); }
     }
 }

The reason I added an enum to the class is because I need to compare colors often, but what worries me is that will result in creating a bunch of new colors every time I test:

 if (myColor.name == BubbleColor.purple.name) {}

Does calling BubbleColor.purple actually create a new instance of BubbleColor each time or is it somehow stored in the class considering that it's static. I ended up doing something like for fear of creating new instances all the time:

 public static readonly BubbleColor purple     = new BubbleColor(Name.purple,    new Color (0.5f, 0f,   1f));
 public static readonly BubbleColor brown     = new BubbleColor(Name.brown,    new Color (0.6f, 0.3f, 0.1f));
Comment
Add comment · Show 2
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 Nanity · Jul 10, 2013 at 08:47 PM 0
Share

Very well-arranged program$$anonymous$$g style btw. $$anonymous$$eep it up!

avatar image mansiva2000 · Jul 10, 2013 at 09:09 PM 0
Share

Thanks :-)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by shaderop · Jul 10, 2013 at 08:45 PM

Your worries aren't misplaced. The static property will create a new instance every time it's called, because it's just syntax sugar for a method call. The compiler might be able to optimize some of those allocations, but it's best not to rely on that.

Your second solution with the public static readonly fields is a much better solution IMHO, and I personally find it more readable.

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 mansiva2000 · Jul 11, 2013 at 01:05 AM 0
Share

Thanks for the advice!

avatar image
1

Answer by Nanity · Jul 10, 2013 at 08:42 PM

Easiest way would be to check:

 if (BubbleColor.purple == BubbleColor.purple)
    Debug.Log("It's stored in the static struct");
 else
    Debug.Log("Created new on each call");
Comment
Add comment · Show 2 · 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 shaderop · Jul 10, 2013 at 08:50 PM 0
Share

This might work for reference types that don't override the Equals method, but I'm pretty sure it won't work for structs.

avatar image mansiva2000 · Jul 10, 2013 at 09:04 PM 0
Share

You're right, and would have been quicker than writing this question but I wanted to know if there was some hidden "magic" somewhere.

Anyway, I did try it and effectively it does create a new instance on each call ;)

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

17 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

Related Questions

Surface Shader issue: property color gets changed 0 Answers

property.FindPropertyRelative("property") works only for attributes but not actual properties? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Get mesh name with mouse click, then change color (and other properties) of mesh 2 Answers

Changing two different objects renderer colour 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