- Home /
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.
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?
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.
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?
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.
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?
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.
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?
Your answer
Follow this Question
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