Save current state of scene and load it - c#

How do i save current scene state and load it later?
I'm making a game where you are building a bridge and then press start to run over it with a car and if you fail you have a restart button that reloads the scene from origin state(without anything built on it). However, i want the player to be able to press an "Edit" button that will go back to the state right before you pressed "start" so you can keep building on your bridge without having to rebuild the whole bridge over and over again. So, how do i do this?

If you don't want to code it yourself, you can use the PlayerPrefs of Unity. If you don't know how to use it look at the documentation (https://docs.unity3d.com/ScriptReference/PlayerPrefs.html) or you can also find tuto on Youtube, even some examples.

Related

Unity (new) InputSystem does respond to mouse clicks

I am building this simple game, where a bunch of fellows that I have are supposed to seek a point indicated by a mouse click. The issue is that the Editor does not seem to notice mouse clicks at runtime. I have used the exact Input Action Asset before for detecting presses of "g" button, but it seemed to have stopped working when I played with it some more sometime later. I have since removed the asset and created w new Action Assets (one that I created manually and another that I created through the Input Action component button). Neither of them works. I have no idea what is going on and I have been looking at this for several hours now. Can someone please explain what I might have done wrong?
Different functions I used to try and get the Editor to respond to my code are below. MouseClick() is the original function that I needed to run, but did not work, onFire() is my attempt at running the default one that is given if Actions Asset is isnantiated through the component.
So it turns out that the issue was that the functions are supposed to be capitalized at the start.
Have you tried using the performed event ?
Also your Fire action needs to be a button type

Unity load scene in editor playmode synchronously

Main Question
I'm trying to load a scene in the editor while in playmode and get the exact behaviour of:
SceneManager.LoadScene(ScenePath, LoadSceneMode.Single);
Except without requiring the scene to be in the build settings.
I would have expected this to work:
EditorSceneManager.LoadSceneInPlayMode(ScenePath, new UnityEngine.SceneManagement.LoadSceneParameters(LoadSceneMode.Single));
However the scene appears to load asynchronously, which I don't want for my particular use case.
Is there a way to acomplish what I'm looking for here?
Additional details (my use case and "why" I want this)
What I'm doing is creating a loader for scene sets that will additively load a bunch of scenes with different constraints on their loading behaviour (such as: load async, always reload when transitioning same->same, and other features). I have this working fine in the editor while I'm not in play mode, I have it working fine in a built player, but the last case is getting this to work while in the editor and in play mode.
The reason I need "single" loading at all is when you transition from one scene set to another and they share no common scenes. Since I can't unload every scene, I'm just skipping the unload step all together and having the first synchronously loading scene load with LoadSceneMode.Single to cause everything else to unload.
On the off chance it's relevant (I really hope not) I'm trying to do this from inside a custom editor's OnInspectorGUI call. I'm triggering it from a button press in there.
Options I'm aware of:
EditorSceneManager.OpenScene with OpenSceneMode.Single
problem: Does not work in play mode
EditorSceneManager.LoadSceneInPlayMode with LoadSceneParameters(LoadSceneMode.Single)
problem: Scene is not loaded synchronously
SceneManager.LoadScene with LoadSceneMode.Single
problem: Scene is required to be in the build settings
EditorSceneManager.LoadScene with LoadSceneMode.Single
problem: Scene is required to be in the build settings
Turns out I had a misunderstanding of what SceneManager.LoadScene(ScenePath, LoadSceneMode.Single); would even do.
A friend had pointed out the docs clearly state the load will happen within 1 frame, not actually at the call-site.
If you're facing a similar issue there are a few options. You could load async and subscribe to that callback to perform activation them. Another option is to subscribe to SceneManager.sceneLoaded and perform activation there.

How to open any specific scene when the game opens

My question is that, how can I set any scene in unity as first scene (means it will open first when the game starts).
As I started making my game in unity, instead of making main menu I made LEVEL 1, and as a result when the game opens it directly starts the game by showing level 1.
So how can I set my main menu as the first scene
To further upon Retired Ninja's comment, to set a specific default scene when the game opens, you need to set the ordering of the scenes so that the one you want to start with is first, see the picture below:
The main menu screen comes first in the list of scenes, this means it will run on startup.
More details here where it has been answered on the Unity Forums:
https://answers.unity.com/questions/44953/set-first-scene.html

Unity hovering buildings before you place them for game

In strategy games it's common to have after you click the button to build a building, you are able to "hold" the building on the cursor, so you can place it where you want it to go. To do this, I need to have it initialize, then have it follow the users cursor with raycast.
What I need it to do:
Need to have the building initialize.
Need to have the building follow the cursor using raycast
On click, the building needs to place in a permanent spot, then destroy the old building.
The permanent building gets stored into an empty GameObject using the Transform.SetParent line. Creating a public variable for this above will allow it to be set in the game manager, rather than hard coded into a script.
In general, you will need to create code to update the position of the building to match the position of the cursor. You can get that by getting the RaycastHit point. You can do that by updating the translation of the building on every update to the current hit point.
You will likely need to keep track of the user being in some sort of building placement mode. A simple way to do that is by using a state machine.
Then, when the user clicks (or fires, or whatever you decide the appropriate mechanism is) you use that position to store the permanent position.
It's hard to give more details without know how you are tracking and storing the buildings. I'd assume it's something like an array, in which case, the second step simply involves adding the building to the array.

detect printscreen

simply put I want my program which runs in the background to check whether the print screen button has been pressed (which mostly triggers a function within another program to generate a screenshot of the current window. eg a game screen).
I have seen lots of possible answers, but most seem overkill for just detecting the print screen button.
I want to have a program in the background check so that it can move the screenshot that has just been made/saved to a certain location. after the button had been pressed.
I hope this question is clear, if not I'll try to explain more clearly.
ps
On a side note, would it be better to try and detect the printscreen button or to simply check the specified folder every 1 or 5 minutes whether a new image has been put in that folder and then move it if true.
EDIT
I decided to use the FileSystemWatch, however it seems to be to fast if i use the on create event as sometimes the file is still being made and it can't move the file yet.
Would the best action be to use a timer to delay ?
You could just watch the folder location for updates using a FileSystemWatcher. There are many examples around the place.
Global keyboard capture in C# application
You should be able to use a global key hook to handle the print screen key press.

Categories