Recovering inspector values from Unity build - c#

I made the stupid mistake of moving a C# script out of the project and back in. This, of course, cleared all of the important inspector values back to their default values from the script.
All I need is to see those values again and re-enter them. I attempted decompiling the assembly-csharp.dll from my last build using dotPeak. While this did recover the correct classes and their fields, all of the fields are not defined. Where in a Unity build are these values stored, and is it possible to decompile them there?
Thanks in advance!

As far as I know there are stored in the scene file (.unity) or, if it's a prefab, in the prefab file (.prefab) (and if it's a prefab in the scene, it's stored in the prefab file with a list of modifications in the scene file). You might have success finding some values in there, but those are serialized and you can only really read them with Asset Serialization Mode "Force Text". It might also be that they lose their value when you open Unity between moving the script out and moving it back in.
Edit:
I missed the part that you wanted to read them from the build. I don't think that's possible, as they are normally serialized (if they are not in Streaming Assets). As scene files (which I think would contain the data) are also just files and not scripts that get compiled, I think the values are in one of the serialized files.
Also: You don't happen to have any kind of version control? Because then you could rollback to an old commit that contains that data, or if that's not possible look at the specific file in question.

Related

Unity make ScriptableObject on a build

I'm new to scriptable objects, but I've seen a lot of tutorials using them for an inventory system and recipes. My question is if it is possible for a future player of my game to create new "recipes" in the final build that will be saved as scriptable objects. Similar in Minecraft where you can use data packs to create new recipes.
ScriptableObjects aren't really intended to be used that way. They are assets in your build, just like objects saved in your scenes during edit time.
One option could be to try to save them as Addressables or AssetBundles so the game could load them dynamically. I don't think any of those facilities are available at runtime, so you would have to jump through a lot of hoops to get the storage format correct. I'd recommend against that and just use regular object serialization to save stuff.
It's worth noting, when you run your game in the editor and directly update your ScriptableObjects at runtime, it will update the asset and will save with the project. Although it might seem like its working, that only works in the editor. In a real build of your game it will not be saved, and will have it's original values every time you start your game (just like the scene objects all return to their original states).

Unity Script does not update in Visual Studio

I am new to using unity and also coding in C#, the problem I'm having is that when I change the value of a variable inside visual studio the code in the c# script does not update. I am not sure what the problem is I have tried a bunch of different methods but nothing seems to work. The current version of unity I'm using is 2020.1.17.f1 on mac os, and I am using visual
studio 2019. Below you can see an example of my code. In the unity console, the correct output isn't displayed. The variable currentAge is set to 17, but the output inside of Unity is 31.
I am not sure how to fix this issue, if anyone has any ideas I would greatly appreciate the feedback.
This is no "issue", just a misunderstanding ;)
The field
public int currentAge;
is serialized.
What this means in detail you can read in Script Serialization. In short this means this value is
visible/editable via the Inspector
saved together with the according asset (ScriptableObject/prefab/scene)
loaded during the initialization and deserialization process
What you can provide in your code is only a default value until you change it via the Inspector. As soon as you change that field value via the Inspector it will get overwritten with this now serialized value. So the next time you hit Play and this script instance is deserialized from the according asset file the value will be loaded and overwrites any default value you have set in your code.
And of course changing the serialized field value will NOT cause a recompilation of your script. You will not see the change reflected in your script! Which makes totally sense: What should happen if you have multiple instances of this script on different GameObjects all with different values? ;)
Also the other way round: If you change that value later in the code you only change the default value, the one in the Inspector, the serialized one will always overrule it.
There are some(?) IDEs (I only use Rider) which are capable of sniffing into the Unity references and asset values and can tell you with what value certain serialized fields are currently serialized/saved with but usually you can only go by what is either set in the Inspector or in the Unity messages like Awake, Start, OnValidate etc.
this is not bug orsomething, it is because you chnaged the value inside the Unity editor, that updates the value but it doesn't update the value inside Visual Studio
First of all, if you set a variable in the Inspector, this is a priority and will always remain the one in the inspector, only if you do not change it in Start or Awake.
To understand, if you changed the value of the variable to 31 in the inspector, then it will remain 31 even if you change in VS.

Unity scriptable objects references lost using GIT

I'm currently working on a project where we use scritpable objects to store all configs on the game. The problem comes using git since sometimes we lose references stored on those SO.
For example I have a Config called A with a GameObject variable. On my branch "Branch1" the variable is null and on my branch "Branch2" is assigned to a prefab. I'm working on Branch1 and I move to Branch2 but the SO is not updated and the variable is still null although on Branch2 that variable actually had a value.
We have tried to reimport the asset but does not solve anything. The only thing that seems to work is a weird process. If we make a change to the SO after moving to BranchB, we save and then discard changes from git (we use sourcetree) the SO config now is fine as it should be on the current branch.
It's a weird behaviour and more weird solution we have that is leading to a lot of time lost and several errors.
Anyone has an idea of what is happening and knows how to solve it??
We are using Unity2019.1.14f1 and we use Odin for a custom inspector of the SO (maybe is relevant).
For anyone stumbling upon this question:
Save the project, under File->Save Project, and you will see your SO opening up for a commit in your preferred sourcecontrol software.
Kurt-Dekker (Thread linked below) goes into detail as to why just "Save as" and "Save" don't work, but "Save Project" does.
First answer by Kurt-Dekker in this thread: https://forum.unity.com/threads/scriptableobjects-are-not-updating-when-changed-on-the-filesystem-via-git.834109/

Build an AssetBundle runtime

I'm using Unity and I need a button that calls a function that allows me to build an AssetBundle, so I can make a software external from unity that allows me to build AssetBundle; is it possible?
Thank you all.
Build an AssetBundle runtime
This can not be done. bulding assetbundles requires the buildPipeline which is in the UnityEditor namespace which can not be accessed at runtime as it is editor only.
Making an external application for this that you call from within Unity would most likely also not work unless you know how to replicate their pipeline in your own application, and i doubt you will be able to call it form outside Unity (And even if you could it would still not work at runtime as assets will become read only)
I'm not sure why you would want to build assetbundles at runtime, but the only method i can think of that may work for this would use JSON/XML.
This would need two Instances of unity running to do it in "real time". The first instance of unity would be running your game with the object you want to make into anassetbundle. After clicking your button a function would be called that gets all the information on your GameObject (components, values, ID's, literally everything) and parse it into a XML/JSON file. This file will then be uploaded/saved somewhere where your second Unity instance can access it. The second instance of unity will then read this JSON/XML file in editor time, reconstruct the original GameObject from the data within (This can be done in editor time from script) and then put this reconstructed object through the assetbundle pipeline.
Do take note though that i've never done this myself, and am not 100% it will work. Though i am fairly confident if done correctly it should work.

Unity Asset Bundles backward compatibility

I am working in a project, and we use Unity3D ... we are loading our Asset Bundles Dynamically. But we want to add the backward compatibility between the versions of our app.
Let's say that I release version 1.05.0 and the user loaded a certain Asset Bundle, After that the user downloaded version 1.06.0 which contains some changes in some scripts that were used in version 1.05.0
Could I make version 1.06.0 work with the same Asset Bundle of version 1.05.0 ?!
or that is not doable ?!
Thanks in advance
"Some changes in some scripts" is quite vague.
Generally speaking, asset bundles do not include scripts directly, but they do include references to scripts (which may be corrupted if you move your scripts) and to properties (which may get complicated if you refactor). Unity Answers does have some more detailed information.
If all you did was some minor tweak to the Update() function of some script, you should be fine.
If you renamed your scripts or moved scripts between assemblies, you will likely have a problem.
If you create new serialized fields, an older asset bundle will not contain those fields, and they will receive their default value upon instantiation.
If you remove existing serialized fields, they will be ignored upon instantiation.
If you rename or change the type of existing serialized fields, Unity will "try" to reconcile that, but I wouldn't count on it working.
If you're looking for a more robust system, you might be better off attaching "plain old data" components just to serialize your data, then using that data to reconstruct your assets at runtime.

Categories

Resources