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
1
Question by $$anonymous$$ · Aug 08, 2016 at 02:04 PM · scripting problemskyboxpersistence

Why are changes to scene's Skybox.material NOT reset when play mode over?

My Unity app animates the scene's Skybox Material Exposure property over time (based on audio). The Material asset in my project file has Exposure=1. This works.

When I play the app inside Unity:

  1. Before play, I select the Material in Project (to see what's happening),

  2. Play the app from Unity,

  3. As soon as app starts, the Material's Exposure is set to a very low value in the Inspector by my script (as expected),

  4. The Skybox Exposure animates as expected (i.e., Skybox brightness is changing to the music), BUT the Inspector shows no changes to the Material (after the first change at start above).

==> When I exit play mode, the Exposure (in Inspector) changes to last value during play. I expected the Material to return to its pre-play values after exiting.

Note: If I try an experiment with say a Sphere (no scripting though), by manually changing the radius of the Sphere in the Inspector during play, when I exit play mode the Sphere radius resets to its original pre-play value (as I expect).

Scene controller script that changes skybox.material.exposure each frame:

 public class AnimateSkyboxFromAudio : BaseAnimateFromAudio {
     protected override void Start() {
         base.Start ();
     }
         
     protected override void MyUpdate () {
         // Get audio value for this frame.
         float rms = GetAttrByName (AudioAnalyzer.RMS);

         // Change scene's Skybox Material's Exposure.
         RenderSettings.skybox.SetFloat ("_Exposure", rms * 5);
     }
 }




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

Answer by $$anonymous$$ · Aug 11, 2016 at 08:08 AM

Someone (anon) answered this question on stackoverflow. I somehow missed the doc that says that RenderSettings.skybox refers to a shared reference. Doh! <stack overflow answer>

"I expected the Material to return to its pre-play values after exiting."

RenderSettings.skybox works differently than other GameObjects with materials. It returns shared material reference unlike a unique material returned by Renderer.material when its material property is accessed.

So, you must make a back-up of the material before modifying it. When you click stop, assign the backup material back to RenderSettings.skybox. Both of these can be done in the Start() and the OnDisable() function. You make a backup material by calling the Material class constructor with the Material parameter.(Material newMat = new Material(RenderSettings.skybox)).

If you only are changing the _Exposure property then you don't even need to make the material backup. Just backup the _Exposure property with RenderSettings.skybox.GetFloat("_Exposure"); then re-assign it when stop is clicked.

"The Exposure value (in Inspector) does not change after the first change at start (#3), during the play (seems odd),"

Call DynamicGI.UpdateEnvironment(); after you change the material values.

Example of changing the Skybox _Exposure property with a UI Slider:

 public Slider slider;
 Material backUpMaterial;
 
 void Start()
 {
     slider.minValue = 0f;
     slider.maxValue = 3f;
     slider.value = 1.3f;
 
 
     //Get Material Backup
     backUpMaterial = makeSkyboxBackUp();
 
     slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
 }
 
 void sliderCallBack(float value)
 {
     RenderSettings.skybox.SetFloat("_Exposure", value);
     DynamicGI.UpdateEnvironment();
 }
 
 Material makeSkyboxBackUp()
 {
     return new Material(RenderSettings.skybox);
 }
 
 
 //Restore skybox material back to the Default values
 void OnDisable()
 {
     RenderSettings.skybox = backUpMaterial;
     DynamicGI.UpdateEnvironment();
     slider.onValueChanged.RemoveListener(delegate { sliderCallBack(slider.value); });
 }

Since you have already modified the original copy of the skybox settings, I suggest you reset it by setting the _Exposure property to 1.3 which is the default value, then try the solution above.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Scripting Help with My Projectile System 1 Answer

Add Light Source to Sun in Environmental Lighting with Unity 5 by Script? 3 Answers

Persisting Prefabs across scenes 1 Answer

Creating Material from String (Unity 5.6) 1 Answer

Complicated save problem 2 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