Prevent Changes on GameObject from being saved - c#

I'm currently working on an Editor Script in Unity that disables specific GameObjects in a multi scene setup to make the Editor more responsive. I'm working with a setup that has up to 4 Scenes loaded at the same time. Since all these Scenes have many GameObjects in them (I'm talking thousands or even tens of thousands of Objects) the Unity Editor is getting pretty unresponsive and sloppy to use when these Scenes are loaded. Playing and Builds are fine btw, its only the Editor that is unusable.
So I'm working on a simple Editor Tool, that disables specific Parent Objects in all loaded Scenes so the Editor is usable again. This Tool works fine, but I want to prevent the changes on these GameObjects from being saved. I intentionally didn't mark the GameObjects as Dirty so Unity doesn't realize when I disable the Objects. Problem is, when I actually work on a Scene and save my work, the disabled GameObjects will be saved aswell, because at this point the Scene will be marked as Dirty. I've already looked into HideFlags but the problem there is, that I can't make Unity only ignore the changes that were made to an Object. HideFlags will only prevent the Object itself from being saved, which is not what I want.
If somebody knows a simple way to make Unity ignore changes on specific GameObjects that would be extremly helpful :)
Alternativly I would be interested in a way that enables me to run custom Code while Unity is saving the Scenes. That way I could make sure, that these GameObjects will be enabled, before Unity saves the Scene.
Thanks in advance

Don't know about preventing dirty objects from saving, doubt it is possible, but to run custom code before Unity saves a scene - easy.
Example editor script (remember it should be placed under Editor/ dir to work as any other editor script)
using UnityEditor;
[InitializeOnLoad]
static class EditorSceneManagerSceneSaved {
static EditorSceneManagerSceneSaved () {
UnityEditor.SceneManagement.EditorSceneManager.sceneSaving += OnSceneSaving;
}
static void OnSceneSaving (UnityEngine.SceneManagement.Scene scene, string path) {
UnityEngine.Debug.LogFormat ("Saving scene '{0}' to {1}", scene.name, path);
/* Do your magic here */
}
}
Credit for the example goes here, more info on the API here

Related

Unity not rendering animation in playmode

My door animation plays all fine in Editor, but in playmode, it seems not to render, but the collisions seem to work fine. Here's what happens.
The Animator is set up like this
The Animation the game should show consists of just moving two children objects, for more info. What did I do wrong, and how do I fix it?
It pretty much seems like your objects are marked as static and therefore the meshes get combined into a single static scene mesh.
Many systems in Unity can precompute information about static GameObjects in the Editor. Because the GameObjects do not move, the results of these calculations are still valid at runtime. This means that Unity can save on runtime calculations, and potentially improve performance.
As the name static already suggests: These objects are static and can not be moved by the Animator.

Load Scene is not restarting the scene properly

I'm trying to restart my game when the player loses, I play a coroutine which makes the player ignore collisions and falling off the platforms. Then when the player click a button, the same scene is reloaded.
SceneManager.LoadScene("GameScene")
But when the scene loads again, the player is still ignoring collisions and falls, it's like the scene is loaded but not the same way when the game is played for the first time.
¿How can I reload the scene properly without closing the aplication and opening it again?
Thank you.
The problem is that you are using Physics2D.IgnoreLayerCollision for this.
This property is global, it affect all scenes, and not related to the specific scene. What SceneManager.LoadScene resets is only property related to the specific scene or the objects in the scene.
You have 2 options here:
Don't use IgnoreLayerCollision. An alternative may be, for example, to disable all colliders on the player. You can use GetComponentsInChildren for example to find all the colliders.
Reset IgnoreLayerCollision manually.

How to communicate between 2 scenes in Unity?

I would like to make a color system for my player. In my game scene the player can pick up the coins and the amount of coins will be save with playerprefs , but I don' know how can I use the amount of coins in my menu scene.
And I need some help to a player color selecter too. When the player select a color than in my game scene must instiate the player with the color.
Soo I think , i need to know how to communicate between 2 scenes.
Can somebody help me with some tutorial?
There are multiple ways to achieve this, which I feel comes down to a matter of taste: save data to file, use DoNotDestroyOnLoad...
But from what I understand, the recommended way now is to create a "manager scene" which will stay alive throughout the lifetime of your app and pass data to and from your other scenes as they are opened and closed, instead of using DontDestroyOnLoad:
It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress. src
See the Unity guide here. Basically you would have 2 scenes open at the same time, at any given moment: the manager scene and whatever the actual active game scene is. Then you can communicate between scripts in the two open scenes via event delegates. The way it would work is:
Player selects color in scene1
Color is sent from scene1 to manager scene via event delegate
scene1 is unloaded and scene2 is loaded
Color is sent from manager scene to scene2
This is the approach I've been using for a project now which looks like this:
"0-Attract", "1-Sealant", "2-Paint", and "3-Conclusion" are my actual game scenes, and "Manager Scene" contains everything that exists in every other scene (thus no reason to kill and respawn them) as well as all of my "manager" scripts which handle the passing of data between scenes.
Note that multi-scene editing can be confusing at first, as there are new things you need to pay attention to (i.e., which scene is currently "active" and what that means) so be sure to go through the unity guide I posed above. Good luck!
you can Call DontDestroyOnLoad method on any object that you want that will remain wen you are moving the a new scene so it will not be destroyed when scene is closing.

Editing unity scene files post-compile

I'm working on a mod engine for a unity game. It is 2D based and stores all the level data in separate files ("level1", "level2") in the game data folder (I believe this is standard for unity). I am looking to be able to edit these in post to add/remove game objects in the scene. I wish to be able to do this programmatically in C#.
I've already had a look at the file in the hex editor, and it seems like this is possible (I can see basic game object data).
Currently im loading the scene then moving all objects around or instantiating new ones, but this is proving to be unstable because of the way the game handles objects.
If anyone could point me in the direction of how i would go about this, it would be greatly appreciated.
Update for those that are asking for additional info: Yes, by levels I mean scenes, unity saves them as “level0”, “level1”, etc
I am not the author of the game, the game was not designed with changing the scenes in mind, almost all of the interacatble objects have special riggers crafted to them, so in order to move them it requires me to be extremely careful or the game crashes.
From what you write it seems your separate files are actual Unity3D scenes. You should only have once scene and a system in place that reads and loads your data, for example from text files, then programmatically instantiate those object at run time.

What is the best way to manage Player object of SteamVR plugin for Unity3D when change of scene

I am developping an application using the new version of SteamVR plugin (2.2) for unity and i need to navigate between several scene during the game.
I have for all my scenes a Player object coming from the SteamVR plugin handle input and event, who is a singelton dontDestroyOnLoad, and i don't know the best way to handle this object during the change of scene.
There is 3 options in my opinion :
For all scene a player object is present, but steamVR don't check if an instance already exist so i add this feature to have only one player after scene initialisation.
The other solution is to destroy the current instance before loading the new scene but with the new system of event made by Valve in this version i don't know if it's a good way to manage the fact to have only one player at the time, can potentiolly create event conflict (didn't saw yet).
The last one but not really confident with it is to let 2 player per scene and one of this is the good instance of the singelton class (way more anoying to get the good instance Player, and can i have conflict between them ?)
If someone have some idea or can give me better understood of my conception issue you are welcome :)

Categories

Resources