Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Gamify3D · Mar 04, 2017 at 08:37 AM · lightcomponentmonobehaviourcopy

Copying a Component's values without a new Game Object

Hello, we have an issue that seems like it can't be solved due to the way MonoBehaviours are scripted, but we wanted to ask first before giving up.

Basically, we want to store the starting values of a Light component. The color, intensity, etc. are changed numerous times at runtime, but we want to be able to change them back to the values they started at.

Originally we thought we could somehow make a deep copy of the Light component and store it in a variable of type "Light", but we can't seem to make a deep copy without making an entirely new game object (which can't be destroyed because it will take the default values with it). This is not preferable, because it would essentially create a new game object for every single light we are changing. Is there really no way to store these values other than making a separate variable for every property (color, intensity, range, spotAngle, etc.) and assigning them manually?

Comment
Add comment · Show 1
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 hexagonius · Mar 04, 2017 at 10:59 AM 1
Share

maybe this post helps you:

http://answers.unity3d.com/questions/458207/copy-a-component-at-runtime.html

1 Reply

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

Answer by Bunny83 · Mar 04, 2017 at 05:51 PM

No, there's no other way. The built-in components are not just C# classes. They have a native C++ counterpart to which they are linked to. The C# Component class is basically just a wrapper for the native object. All those values aren't stored on the C# managed side but on the native C++ side. All those "properties" are properties which actually call a native code getter / setter.

The easiest solution is to write a "LightProperties" class which can store all the properties of one light. I quickly did this:

 public class LightProperties
 {
     public float bounceIntensity;
     public Color color;
     public Texture cookie;
     public float cookieSize;
     public int cullingMask;
     public bool enabled;
     public Flare flare;
     public float intensity;
     public LightmappingMode lightmappingMode;
     public float range;
     public LightRenderMode renderMode;
     public float shadowBias;
     public int shadowCustomResolution;
     public float shadowNearPlane;
     public float shadowNormalBias;
     public UnityEngine.Rendering.LightShadowResolution shadowResolution;
     public LightShadows shadows;
     public float shadowStrength;
     public float spotAngle;
     public LightType type;
     public LightProperties() { }
     public LightProperties(Light aLight)
     {
         Save(aLight);
     }
     public void Save(Light aLight)
     {
         bounceIntensity = aLight.bounceIntensity;
         color = aLight.color;
         cookie = aLight.cookie;
         cookieSize = aLight.cookieSize;
         cullingMask = aLight.cullingMask;
         enabled = aLight.enabled;
         flare = aLight.flare;
         intensity = aLight.intensity;
         lightmappingMode = aLight.lightmappingMode;
         range = aLight.range;
         renderMode = aLight.renderMode;
         shadowBias = aLight.shadowBias;
         shadowCustomResolution = aLight.shadowCustomResolution;
         shadowNearPlane = aLight.shadowNearPlane;
         shadowNormalBias = aLight.shadowNormalBias;
         shadowResolution = aLight.shadowResolution;
         shadows = aLight.shadows;
         shadowStrength = aLight.shadowStrength;
         spotAngle = aLight.spotAngle;
         type = aLight.type;
 
     }
     public void Restore(Light aLight)
     {
         aLight.bounceIntensity = bounceIntensity;
         aLight.color = color;
         aLight.cookie = cookie;
         aLight.cookieSize = cookieSize;
         aLight.cullingMask = cullingMask;
         aLight.enabled = enabled;
         aLight.flare = flare;
         aLight.intensity = intensity;
         aLight.lightmappingMode = lightmappingMode;
         aLight.range = range;
         aLight.renderMode = renderMode;
         aLight.shadowBias = shadowBias;
         aLight.shadowCustomResolution = shadowCustomResolution;
         aLight.shadowNearPlane = shadowNearPlane;
         aLight.shadowNormalBias = shadowNormalBias;
         aLight.shadowResolution = shadowResolution;
         aLight.shadows = shadows;
         aLight.shadowStrength = shadowStrength;
         aLight.spotAngle = spotAngle;
         aLight.type = type;
     }
 }

To save the state of a Light you can simply do:

 LightProperties save = new LightProperties(yourLightComponent);

To restore the properties just do

 save.Restore(yourLightComponent);

Of course it could be done with reflection, however there are a lot deprecated properties and some you usually don't want to save (like name, hideFlags, tag, ...). Also this version is faster than any reflection approach ^^. Creating that class only took a few minutes. Just a lot of copy&paste and smart search&replace.

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 Gamify3D · Mar 06, 2017 at 05:10 PM 0
Share

I really appreciate the answer. It's unfortunate we can't make deep copies of lights, but this will work great! $$anonymous$$uch appreciated :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding components from prefab into existing GameObject 2 Answers

[HOW] Copy and Transfer Component with Values to other object? 1 Answer

Copy / Paste As New not copying deep 1 Answer

Flashlight Flicker! 2 Answers

Toggle Light Component On and Off 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