Detect gameobject removal from user action - unity3d

I'm working on a system that automatically generate and release scriptable object that represent uniqueness for gameobject with a given component attach.
The generation process seems to be working really fine, but I'm facing problem when I want to release the scriptable object.
The given script is tagged "ExecuteInEditMode" and is implementating OnDestroy method to advertise a manager that his scriptable object should be deleted.
The problem is that OnDestroy is called in 4 situation from what i can tell :
Click on play
Switching scene
Manual removal (the only one I want to work with)
On editor shutdown
I've been able to avoid the 2 first on with this :
if (Application.isPlaying == false && Math.Abs(Time.timeSinceLevelLoad) > 0.00001f)
But I don't find any good solution for "On editor shutdown" I've seen that EditorApplication.quitting can be use for that case, but documentation warn that this event is not called when unity crash or is forced to quit, and I don't want to lose all my scriptable object if the editor crash.
I'm looking for a robust solution to avoid this problem if someone can help me please.
Thanks, have a nice day

Just handle the application quitting event.
this event is not called when unity crash or is forced to quit, and I don't want to lose all my scriptable object if the editor crash.
When unity crashes, so will your program, so it won't work any ways. Just make sure that under normal operating procedure (so a normal shutdown) you save your modifications.
In Unity3D if you edit your scene and you don't save, if the editor crashes you lose all your changes. The same will happen for what you're building here. In order to mitigate this risk (since prevention is reasonably difficult if not impossible) you can opt to save every minute, always save each change to a temporary location, or save for example every play.
For example i added a script to my project that autosaves the scene(s) whenever i press play, so when editing the scene i press play now and then to test, and it all gets autosaved.

Related

Unity Glitching when I run the Game

I made a Model of the solar system and when I run the program this happens:
I am not sure what is causing this so if you have any idea Pls Reply.
The Scene load is being triggered multiple times:
Check if you have any code that you're using to load the scene (In Update, perhaps?) if youre using it in update, check if you're using GetKey instead of GetKeyDown (GetKey will remain true as long as you keep it pressed)
The information provided here is too little and we can only speculate at this point. please post code, more images and a better description of the problem. How did you reproduce this issue?

Identifying unity gameObjects (pre-runtime) to save/load their data

I pretty much already made a little quest game for Android, and it only just occurred to me that I have to save player's progress.
I have a lot of interactive game objects (they share a script InteractiveObject that I made), which has an integer "CurrentPosition" parameter, that tracks how the player interacted with it. I need to save that parameter for all of those game objects.
What I figured I will do is pass this data to some "manager" singleton class after every interaction, and compile it into the list.. only I have nothing to identify those game objects by. GetInstanceID is unique, but not persistent, so its useless for this purpose.
Of course, I could just add "ID" parameter ty my script, and fill it by-hand in editor.. but that seems to be a suboptimal solution. There are a lot of those objects.
Any suggestions?
You need to somehow "register" your buttons
This is basically the same idea as "give them all an ID manually" except replace manually with "using code."
The first problem you have is that all of your buttons are hand-placed in your scene and all of their interactions are done by hand and all of this was done manually with no regard to "I'm going to need to be able to identify these later" which is how you've ended up here.
Option 1:
Have your code create, place, and name your buttons. As your code is yours it will just automatically generate the required ID and retrieve the state from the save file because you built it to handle this requirement from the get-go.
As you haven't already set your project up this way, this will be a lot of work spent refactoring your project to do it this way.
Option 2:
Do it manually as you stated you don't want to do. You may have to decide that this is the least work-intensive solution that is also reliable.
Option 3:
Create a class that all it does is have a registerButton function that shoves the buttons into an array so that they have an index ID value. When your buttons run their Start() method (if they do not have a Monobehavior script yet, give them one for this purpose) calls this registerButton method, which returns an ID (its literally just the button's index in the all_the_buttons array). Use this as your serialization ID.
Note that while this is deterministic (in that the buttons will register themselves in the same order every time) it occurs in an unknown order. If you delete, clone, create, or move the button around in the scene hierarchy, you will affect what IDs your buttons have, possibly invalidating any existing save files.
you can use playerprefs to save the game data , take a look at this link https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Save data from game to editor? [duplicate]

Apparently this is some beginner's question (and that I am) but it's driving me crazy and I couldn't find any mention:
I Occasionally forget to exit Play mode and go on building my UI making objects and changes, only to realize that I'm still in Play Mode and as soon as I unpress the play button, these will be purged! I suppose the Unity Editor has its reasons for allowing editing of Scripts/Scenes while in Play Mode (would be happy to hear some examples-maybe testing scenes?) but my main question here is:
Is there some way for me to prevent this behavior? Or at least some trick that you use to prevent me from making changes while in play mode? (Other than becoming paranoid about it and checking constantly...)
Thank you
PS. sigh time to head back to Unity and rebuild that UI that I lost...
Other Unity coders have had this problem before me and they came up with a neat solution.
Setting the UI to a different colour while in playmode "Playmode tint".
You can read the details here (originally posted 2009 but I have checked it still works in latest Unity 5.3):
http://answers.unity3d.com/questions/9159/best-strategies-for-not-accidently-editing-whilst.html
There is no settings to prevent changing things during play mode but there are ways to reduce the chances of losing changes during play mode.
1.Edit->Preferences... -> Colors. Now, on the right change Playmode Tint to red. That will remind you you are making changes in play mode.
2.Click on the gear icon of each component you change during play then click Copy Component. When are done with playmode, select the component you want to keep its changes. Click the gear icon again and this time, Click Paste Component Values.
3.Write an Editor Plugin that will do that for you. This is hard but possible.
Use event to find out when entering playmode. Store all GameObject public important variables such as transform/rigidbody properties in a list.
Wait for the stop event to fire then ask your self which GameObjects to overwrite settings to. Then overwrite the properties of the selected GameObjects That's it.
Usefull APIs for this:
EditorApplication.isPlaying
EditorApplication.isPaused
EditorApplication.isPlayingOrWillChangePlaymode
EditorApplication.playmodeStateChanged += callBackFunc;
EditorApplication.HierarchyWindowItemCallback
EditorApplication.ProjectWindowItemCallback
Note: According to Unity roadmap, a feature that enables you to save playtime changes is in construction and will be released soon but the above seems to be the only way at this time.
You can select objects you want to keep while in play mode, copy them with Ctrl + C, and then just paste them back in with Ctrl + V after returning to edit mode. Then you can eithr delete the originals from the scene or copy values from individual components like #Programmer suggested.

Update application placement / Reset origin on Hololens?

Update:
void Awake() {
if(UnityEngine.VR.VRSettings.loadedDeviceName.Equals("HoloLens"))
UnityEngine.VR.InputTracking.Recenter();
}
Doesn't appear to do anything. Found this related post titled, "Reset Hololens Origin" inquiring about a similar behavior. Two individuals affiliated with Unity in someway (both bare the 'Unity technologies' profile tag) brought up InputTracking.Recenter(). One adds, "[S]ome changes landed recently, and part of those changes were hooking up InputTracking.Recenter properly on WindowsMR."
The snippet of code I include above is an attempt to implement this behavior. I most definitely could be trying to call this code in an improper/incorrect location but, as it is now, I do not see it working.
end update
When a user opens a Unity Hololens application they are given control over where the application starts by giving them a generic white box to denote the application and allowed to place it any where via the air tap gesture.
Problem:
If a user blooms out of the application but does not close this white box, the application is left in a suspended or 'tombstoned' state. When the application is opened again, the Hololens remembers the old white box placement and reloads the application their.
This isn't desired behavior as with multiple users and or a variety of locations, the application appears to not be loading to the user when the Hololens is bringing the suspended application back into the running state but not updating its position to current user gaze.
Question:
Can one detect, in awake, where the user is looking and move the application manually?
UnityEngine.WSA.Application.windowActivated : Fired when application window is activated.
Somewhere like this?
Any google searches on this question or variations in wording turn up nothing. Unsure of the possibility of moving the app itself.
If this isn't possible would the next alternative be to force the app to close when I want, say for example, in the suspended state? In an attempt to prevent this type of multiple apps open situation.

Unity3d editor: how to prevent changes from being undone upon exiting play mode?

Apparently this is some beginner's question (and that I am) but it's driving me crazy and I couldn't find any mention:
I Occasionally forget to exit Play mode and go on building my UI making objects and changes, only to realize that I'm still in Play Mode and as soon as I unpress the play button, these will be purged! I suppose the Unity Editor has its reasons for allowing editing of Scripts/Scenes while in Play Mode (would be happy to hear some examples-maybe testing scenes?) but my main question here is:
Is there some way for me to prevent this behavior? Or at least some trick that you use to prevent me from making changes while in play mode? (Other than becoming paranoid about it and checking constantly...)
Thank you
PS. sigh time to head back to Unity and rebuild that UI that I lost...
Other Unity coders have had this problem before me and they came up with a neat solution.
Setting the UI to a different colour while in playmode "Playmode tint".
You can read the details here (originally posted 2009 but I have checked it still works in latest Unity 5.3):
http://answers.unity3d.com/questions/9159/best-strategies-for-not-accidently-editing-whilst.html
There is no settings to prevent changing things during play mode but there are ways to reduce the chances of losing changes during play mode.
1.Edit->Preferences... -> Colors. Now, on the right change Playmode Tint to red. That will remind you you are making changes in play mode.
2.Click on the gear icon of each component you change during play then click Copy Component. When are done with playmode, select the component you want to keep its changes. Click the gear icon again and this time, Click Paste Component Values.
3.Write an Editor Plugin that will do that for you. This is hard but possible.
Use event to find out when entering playmode. Store all GameObject public important variables such as transform/rigidbody properties in a list.
Wait for the stop event to fire then ask your self which GameObjects to overwrite settings to. Then overwrite the properties of the selected GameObjects That's it.
Usefull APIs for this:
EditorApplication.isPlaying
EditorApplication.isPaused
EditorApplication.isPlayingOrWillChangePlaymode
EditorApplication.playmodeStateChanged += callBackFunc;
EditorApplication.HierarchyWindowItemCallback
EditorApplication.ProjectWindowItemCallback
Note: According to Unity roadmap, a feature that enables you to save playtime changes is in construction and will be released soon but the above seems to be the only way at this time.
You can select objects you want to keep while in play mode, copy them with Ctrl + C, and then just paste them back in with Ctrl + V after returning to edit mode. Then you can eithr delete the originals from the scene or copy values from individual components like #Programmer suggested.