• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by callen · Jan 21, 2016 at 05:19 PM · particlesparticlesystemparticle system

New particle system structs - how does this work?

I'm trying to tweak my particle systems in script, and I notice the needed "emissionRate" is deprecated in favor of an "emission" property.

ParticleSystem.emission is read-only, and is a struct, so trying to set PSystem.emission.rate = 5 does not work. The typical solution is to copy the struct, change it, and assign it back to the object (like you might do with transform.position or something).

But as this thread's creator realizes, that doesn't work because PSystem.emission is readonly. However, @richardkettlewell suggests not needing to reassign the struct, and that all you need is a temp var.

He says this code should work. And, in fact does:

 ParticleSystem.EmissionModule em = particles.emission;
 em.rate = new ParticleSystem.MinMaxCurve(5);    

But everything I've learned in 10 years of C# coding says that this shouldn't work. My new "em" variable should be a copy of emission, and modifying it should not be able to affect the source struct. In fact this is why the line "PSystem.emission.rate = 5" gives a compile error, because it knows this won't work.

But somehow, it does work. Anyone know how? Afaik it seems impossible.

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
1
Best Answer Wiki

Answer by karl_jones · Jan 21, 2016 at 05:31 PM

The particle modules are just interfaces to the particle system. They are not independent objects. Internally they contain a reference to the particle system and calling get/set calls down into the c++ code. We do this to avoid GC but still keep params in per module groups.

Comment
Add comment · Show 7 · 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 Dave-Carlile · Jan 21, 2016 at 05:33 PM 0
Share

The documentation says emission is a struct. Is that not the case?

edit: rather, it says Emission$$anonymous$$odel is a struct, but the question remains.

avatar image karl_jones ♦♦ Dave-Carlile · Jan 21, 2016 at 05:42 PM 0
Share

So this is kind of how it works:

 struct Particle$$anonymous$$odule
 {
    private ParticleSystem ps; // access to the C++ particle system
 
    int someValue
    {
         get
         {
             return ps.GetSomeValue();
         }
         set
         {
             ps.SetSomeValue(value);
         }
     }
 }


So the struct is only an interface which is why you dont need to reassign it however you can keep the struct and continue to use it. No need to keep getting it.

avatar image callen · Jan 21, 2016 at 05:49 PM 1
Share

That makes sense. The only other question to me, then, would be why structs ins$$anonymous$$d of classes? If they were classes, we could use the more direct system.module.property = value.

The "avoid GC" claim, also made in the thread I linked, doesn't make sense to me. You're only creating this data once per particle system, no? Since we can't assign the module objects directly, there's no risk of creating any more garbage than we allocated on object creation.

I'm sure, just like the original question, I'm missing something here, but it seems like an empty class makes as much sense as an empty struct?

avatar image karl_jones ♦♦ · Jan 21, 2016 at 06:06 PM 0
Share

I agree classes would be a more logical approach. We will discuss it some more internally.

avatar image karl_jones ♦♦ karl_jones ♦♦ · Jan 25, 2016 at 11:07 AM 0
Share

Update: We use classes ins$$anonymous$$d of structs because there would be managed allocations when accessed if they were classes.

avatar image karl_jones ♦♦ · Jan 21, 2016 at 06:08 PM 0
Share

I think by gc we mean we need to copy the data from cpp into the struct however this way we don't need to. AFAI$$anonymous$$

avatar image callen karl_jones ♦♦ · Jan 21, 2016 at 06:53 PM 0
Share

Thanks for all the insight!

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

38 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

Related Questions

Is there a way to determine what mesh(es) are used by the particle system when multiple meshes are assigned? 0 Answers

I want to pause and clear a particle system and be able to start it again, but I am using the Sparkle Rising particle which has no Particle System? 1 Answer

How to make particles react to movement of particle system 4 Answers

Smoke Particle Not Playing Continuously 2 Answers

How to change particle colliders [2D] 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