Search Unity

Keep inspector values set during game play.

Discussion in 'Wish List' started by Antitheory, Apr 13, 2011.

  1. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    There NEEDS to be an option to keep the inspector values you set during gameplay mode in the editor. I realize that it's possible to create your own hack-job workaround to achieve this, but such a necessary feature should not have to be hand-coded for each and every component.

    Frequently I come across the scenario where I need to fine-tune half-a-dozen or more variables to get things looking nice. It's a major pain in the ass to have to either

    a) remember/write-down the values I set for each component I tweaked
    b) write a custom script to save/retrieve these values

    Why would you bother allowing me to change these values during gameplay if I can't actually save these changes?!
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    You don't have to hand code every component.

    You can use CustomInspectors/Windows and Reflection to achieve your goal in a totally generic way.

    It would be nice to be able to change those values at runtime, but it's also nice to be able to modify and change all over the place at runtime and not have those changes exist when the gameplay ends.

    It's not a small script that would reflexively allow modification of variables, but it's certainly doable in a non-hack way.
     
  3. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    Well I've gone ahead and done exactly what you said... I already had this idea and luckily had another similar script I had written for scaling values while scaling the size of objects.... A few simple modifications and now it works for saving data too... with one giant problem:

    It works fine when the game isn't running, you can set your values save them, alter them, and reload the set you previously saved. This works in the game too, but the problem is it doesn't work from game -> editor, which is the entire goal in writing the script.

    The problem is that I need to keep the data even after the game stops running... Interestingly it does sort of work. It actually keeps all the references when the game stops. I can see this in the log. It successfully updates the values of the stored references... however these don't show up in the inspector.... This lead me to realize something obvious. Unity is trying to destroy the GameObjects when the game ends, and recreate them as they were when the game started, which is very handy indeed.

    So I think I need to modify my script to re-traverse the object and switch my reference objects to the "new" objects Unity creates when it shifts back into editor mode. The only serious drawback is that values which are themselves references to other Unity objects will no longer be useful, as they reference objects which should have been destroyed when the game ended. There are workarounds for that too I guess... but thankfully what I care most is getting structs saved, which should work fine. I will post the scripts when finished.
     
  4. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    You'll need to store it in a serializable form. i.e. if you record an object, it'll store all it's values in a Dictionary, and then as soon as the Application.isPlaying stops flush the whole dictionary and make the necessary modifications.

    You can use EditorApplication.update delegate to continually check isPlaying.

    Store the name hierarchy GameObject.name + parent + parent.parent etc.... and use GameObject.Find. And then store the changed values.

    I realize this is oversimplifying, I may try to make something like this in a week or so, but atm I've got no time/need.
     
  5. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    Here is what I have so far

    The code isn't commented that well but most of it is fairly self-explanatory if you are familiar with Reflection. Included is a very simple Editor script which, when placed in a folder in your assets named Editor, will allow you save values from your GameObjects/Components during gameplay and then load them in when the game isn't running. Go to GameObject "Save Values" to access this editor-window. It will save whatever object(s) you have selected... You can also save the children of the objects too if you want. (There is a toggle in the editor window called "Save Children")

    You MUST check-off "From Game" in the editor window that pops-up if you saved the data while the game was running and you want to apply it during editor mode. (I included this option because if you don't check this, then the script runs faster as it doesn't need to use GameObject.Find.)

    Edit:There is no "From Editor" option because apparently Unity destroys the Dictionary when the game starts. There might be a way around this, but realistically you probably wouldn't want to do this anyways...

    The Only 'Savable' toggle allows you to only save fields/properties marked with the [Savable] attribute. By default this is off... if you leave it off you can tell the script to NOT save values by giving them a [Savable(false)] attribute.

    The Only Fields toggle allows you to target fields only... omitting Properties. This is useful because only fields show up in the Inspector. By default the script also skips fields/properties marked with Unserializable and HideInInspector... you can manually change the code if you don't want/need that. With "Only Fields" turned on you will effectively skip anything that is not seen in the Inspector.

    Draw-backs:

    If you look at the code you will see there is theoretically the ability to save reference-type objects as well as value-type objects (structs). Specifically there are options for saving reference to other GameObjects and Components. It works, and you can try it out if you like but it's not "fully-functional". The main issue is that if your game is dynamically creating GameObjects as it runs these cannot be reloaded once you are back in the editor. I guess this is possible though, but it seems like more trouble than it's worth.

    Future:

    I am going to write some serialization/deserialization methods for the ValueReference class so that you can save game-data to an xml file for reloading later.

    View attachment $ObjectSaver.zip
     
    Last edited: Apr 14, 2011
    plmx and -JohnMore- like this.
  6. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    Check the asset store. There is a tool that does this.
     
  7. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    Well now there's a free one in this thread too.
     
  8. Ben Blaut

    Ben Blaut

    Joined:
    May 15, 2012
    Posts:
    27
    Have you worked on this further Antitheory? The xml method would be extremely helpful.
     
  9. Ben Blaut

    Ben Blaut

    Joined:
    May 15, 2012
    Posts:
    27
  10. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    This thread is a very good find for me today. It definitely put a smile on my face by solving a HUGE problem for me.

    @ Antitheory - Thank you very much!! :D :D :D