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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by douglassophies · Jan 06, 2013 at 11:48 AM · editormaterialsplaymodealpha-channel

Changes made during play mode, remain in editor after the game stops

Searching for my problem reveals questions about changing something during play mode and having it save in the editor. This is the opposite of my problem - I want the changes during play to revert back to before i pressed play when i stop the game, just like what normally happens. Here is my specific issue: A script runs during play mode, that fades in a background material from 0 Alpha to 255. When i stop the game, the alpha value remains at 255 for the material.

This really confuses me as i am fading in all my game objects over time and everything else works fine - they fade in and remain opaque throughout the game but start from 0 alpha next time I run it.

I am completely lost and would appreciate any help. The only difference i can see with this GO compared to the others is that its material uses its own single texture, while very other GO uses the same spritesheet.

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 douglassophies · Jan 06, 2013 at 12:44 PM 0
Share

Thank you! I do not quite understand why that is the case, but i should have played with it as shared$$anonymous$$aterial vs material is always my problem when it comes to anything visual! I guess i need you to make a post so i can mark it as the correct solution?

1 Reply

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

Answer by whydoidoit · Jan 06, 2013 at 02:40 PM

You should use material rather than sharedMaterial as changes to project items are persisted between play and edit modes. sharedMaterial accesses the project item while material makes a runtime copy. Scene items aren't persisted between the two modes.

Comment
Add comment · Show 6 · 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 filip-vondrasek · Oct 21, 2015 at 02:33 PM 1
Share

Sorry to revive an old thread, but I'm having the same issue as OP. But in my case, I have lets say thousands of instantiated objects using the same material. $$anonymous$$aking all of them copy the material using material ins$$anonymous$$d of shared$$anonymous$$aterial would take quite a long time. Do I have any options?

avatar image Bunny83 filip-vondrasek · Oct 21, 2015 at 04:09 PM 1
Share

If the object you talk about is a prefab that is instantiated only at runtime you can simply create one material instance for the prefab. When the prefab is instantiated itself it will use that one copy for all instances. That of course doesn't work for instances which are already in the scene.

You can simply add a script to some sort of manager object in your scene that instantiates the material in Awake

 Renderer prefabReference;
 
 void Awake()
 {
     var materials = prefabReference.shared$$anonymous$$aterials;
     for(int i = 0; i < materials.Length; i++)
         materials[i] = ($$anonymous$$aterial)Instantiate(materials[i]);
     prefabReference.shared$$anonymous$$aterials = materials;
 }

You can of course turn "prefabReference" into an array if you want to handle multiple prefabs. If you also have a lot of object in the scene you might want to use a "replacement dictionary" and something like this:

 Dictionary<$$anonymous$$aterial, $$anonymous$$aterial> dict = new Dictionary<$$anonymous$$aterial, $$anonymous$$aterial>();
 
 $$anonymous$$aterial Get$$anonymous$$at($$anonymous$$aterial a$$anonymous$$at)
 {
     $$anonymous$$aterial mat;
     if (!dict.TryGetValue(a$$anonymous$$at, out mat))
         dict.Add(a$$anonymous$$at, mat = ($$anonymous$$aterial)Instantiate(a$$anonymous$$at));
     return mat;
 }
 
 void Awake()
 {
     Renderer[] renderers = FindObjectsOfType<Renderer>();
     for(int i = 0; i < renderers.Length; i++)
     {
         var materials = renderers[i].shared$$anonymous$$aterials;
         for(int i = 0; i < materials.Length; i++)
             materials[i] = Get$$anonymous$$at(materials[i]);
         renderers[i].shared$$anonymous$$aterials = materials;
     }
 }

This will go through all objects in the scene and create an instance for all the shared materials in use. Thanks to the dictionary it will instantiate a material only once. So all objects that shared a single material in the beginning will again share the same instantiated material.

avatar image douglassophies · Oct 21, 2015 at 02:49 PM 0
Share

Been a long time since i used Unity but cant you simply use search and replace on the class where you have typed shared$$anonymous$$aterial ins$$anonymous$$d of material?

avatar image filip-vondrasek douglassophies · Oct 21, 2015 at 02:52 PM 1
Share

The problem is not simply in rena$$anonymous$$g shared$$anonymous$$aterial to material. The problem is that thousands of objects would have to copy the material, thus increasing memory usage and also CPU usage, because we change the material properties in each Update.

avatar image douglassophies · Oct 21, 2015 at 03:01 PM 0
Share

Ah ok. So you want to create a copy at runtime and have everything use that one copy, so if there is a change to the copies properties it affects them all. Then when the editor stops the copy is destroyed and the reference is returned to the original source. Does that sound right?

avatar image filip-vondrasek douglassophies · Oct 21, 2015 at 03:12 PM 1
Share

Precisely. :)

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

11 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

Related Questions

Pressing play Causes Re-instancing of material (Editor) 1 Answer

Enable Emission to update at run time in editor 5.6.1? 2 Answers

Why does unity editor crash when exiting play mode, if it has been playing for more than 15 seconds? 1 Answer

Close EditorWindow in Play Mode 1 Answer

Changing the materials in Editor Mode Without Getting the leak Error 3 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