Using Unity 5.4.2.
I've created an editor script that implements an exporter for some prefab data when you press an export button. It recursively finds all the prefab files matching a certain type under its own directory and loads those prefabs so they can be exported in a custom manner.
The problem I'm having is that the prefabs on disk, that are loaded with
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file);
do not match the current state in the editor. That is if you have modified some of the prefab data in the editor, it is not actually saved to disk. Note that this does not require any objects to be added to a scene, I am just exporting data directly out of components attached to prefabs.
I have tried adding AssetDatabase.SaveAssets(); to the top of my export function, which is supposed to save all the assets, but when I edit a value and then log the values loaded from the prefabs, they are not up to date. There are two (non-programatic) methods I have found so far that cause my exporter to be able to see the modified data:
If I exit Unity and restart it, the prefabs appear to get saved at that point and the exporter sees the updated data.
If I play and stop any scene, then export, the exporter can see the updated data, but oddly (according to git status) they have not been written to disk, so I suspect LoadAssetAtPath actually loads some cached version which is updated on 'play'.
Is there a way to force Unity to save everything to disk so that my exporter can operate on the most up to date values?
The following should do the trick
// Your changes go here
AssetDatabase.Save();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); // perhaps even ForceSynchronousImport?
// You loading should go here
For more information about ImportAssetOptions see this documentation https://docs.unity3d.com/ScriptReference/ImportAssetOptions.html
Note: This does not answer the original question, but identifying and working around the cause may help anyone with the same/similar problem.
My problem was when exporting computed-and-cached properties in components attached to the prefabs. My assumption that LoadAssetAtPath would load a fresh instance from disk was wrong, it returns the version already in editor memory, which means cached values aren't invalidated like they would be with a fresh load.
The solution I used was to never cache when running in the editor, using #ifdef UNITY_EDITOR.
Related
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.
Keep a long story short, ive suddenly gotten a bunch of these errors in unity when coding my game (The numbers keep going up)
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UIElements.UIR.RenderChain.Render (UnityEngine.Rect topRect, UnityEngine.Matrix4x4 projection)
Its my first template of a much larger project, but none of my objects' scripts reference anything to do with UIElements... just UI for setting the text of an ammo counter
You guys got any ideas? it doesnt affect my gameplay or my scripts it just is annoying in the console, and it happens when im not in game too. I think the amount of errors has maxed out at 35, in the time of writing this
Thanks! :D
You can try to delete the project's caching folder "library", this will make Unity reimport all your assets and generate new copies of intermediate DLLs.
Warning: It will take a long time to reimport all assets if your project is big.
I was doing a geometry dash private server with a friend then I noticed that we can modify images in ftp while someone is running the game so I want to ask what will happen if we rename the image when someone is playing?
It depends on what image you're talking about. Gamesheets get loaded on starting the game so renaming them will do nothing while running the game but detail images like the vault faces will only be loaded when needed. Since the names of these images are hardcoded you need to change those before renaming them, otherwise it will just crash due to missing files.
I have small project that I am creating to learn a bit more about Unity.
In this project I have a lot of 3D models, and I need to use only some of them. I am using external text file that the program reads when executed, in order to tell it which models to load.
For example, in the text file I can write:
Hunter, CandyHouse
and the program would instantiate those 2 models only.
My problem is that I don't want to save all the models in the Resources folder and load onluly those I need, because that would increase my program size drastically. So what I want to do is to save the models I need next to the text file and instantiate them. But I don't know how to access them from Unity.
I have found the WWW class in some answer, but they used it to load textures, when I tried to load Maya file with it, Unity just stopped responding.
Is there anyway to do it¿
Use model make a prefab.(This can decrease draw call when you need many model in scene.)
Use this prefab make a assetbundle.
put this assetbundle in your url(this could be a remote server url or a file url.Note that iOS/Android may have problem on permission of read/write).
And use the code follows to load.
WWW www = new WWW("url");
yield return www;
GameObject obj = Object.Instantiate(www.assetBundle.Load("ModelName")) as GameObject;
We could also use LoadFromCacheOrDownload,the asset will be save speficy location.So no need everytime load from web.
Last night I finished up on unity and saved my project and scene before closing unity.
This morning when I loaded up unity My Hierarchy, scene and game view are all empty, I don't even have a main camera. This has happened before so I closed off unity (Without Saving or anything) and opened it through the scene file, to my surprise again it does not work, Thinking it must have just not loaded the scene again I tried loading the scene in unity, again no result, now I've spent the last 2 hours browsing the internet trying to find the solution to the problem.
A few common answers I've seen for this problem that do not work for me:
Unity is not opening a new scene, it says at the top it is currently in the scene I want.
Pressing F does not fix anything since there is nothing in the hierarchy in the first place.
Also:
The project is saved in DropBox.
All the assets say they were last updated when I last saved and closed Unity.
Any help is greatly appreciated.
From my experience, Unity and Dropbox don't really get along, as one may lock files the other one wants to read/write, and that means sometimes files not saved properly.
If you want to share projects or just have an online backup, prefer other tools that don't scan the folder all the time, like Git.
open run prompt (windows+R)
%AppData% --> "Roaming" folder will open.
Search for Unity and delete this folder.
Go up one folder up (AppData) and do the same to Local/Unity and LocalLow/Unity