- Home /
Saving usermade content
Hi all!, i made an editor script which allows players to snap objects together and creating a new object all together Now i want them to allow to save their creation in a folder as a loadable item like a prefab. What is the best way of doing so. I believe LoadAssetAtPath and PrefabUtility can only be used within unity's editor is there another way of doing this?
Answer by HappyMoo · Jan 02, 2014 at 09:06 PM
Sae your data in an object you derive from ScriptableObject.
http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.html
You can save your data with:
AssetDatabase.CreateAsset(asset, "Assets/MyData.asset");
AssetDatabase.SaveAssets();
And if you click on your asset file, you can see the Editor for it in the Inspector... which you can customize of course like for other Types.
Then you can drag those assets directly onto a
public YourClass data;
field or you can load it from the AssedDatabase like other Assets or from Resourcs-Folders during runtime.
Thank you for your answer, i've tried using your sample code as following:
creation = GameObject.FindWithTag ("rootelement");
AssetDatabase.CreateAsset(creation, "Assets/Resources/user_vehicles/"+creationname+".prefab");
AssetDatabase.SaveAssets();
And getting a bunch of errors thrown at me, like - Persistent object inconsistency - Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object ins$$anonymous$$d. Destroying the transform component is not allowed. - Some objects were not cleaned up when closing the scene
Am i missing something ?
yes, you shouldn't save a whole gameObject, but have a custom class that saves your data and save that... that's the power of ScriptableObject.. that it actually doesn't need a gameObject to exist.
But maybe I missunderstood what you want. If you want to create a simple prefab like if someone would drag a gameObject from the hierarchy into the project, you can do that like this:
Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/myname.prefab");
PrefabUtility.ReplacePrefab(creation.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
I tried that before the problem is 'editorutility' i think, it implies that it doesn't work in a standalone build? and only in the editor ?
Yes, but the same is true for any editor script. If you need to save stuff in a custom format as a game feature, you're on your own... i.e. you can use mono/.net to write the stuff out however you want... You can create a Text file: http://msdn.microsoft.com/en-us/library/aa735748(VS.71).aspx
or write an xml file etc.
Answer by jamesflowerdew · Jan 03, 2014 at 02:03 AM
The following may not be the best answer, but it is one answer.
This kind of stuff can usually done by sending raw data (although it is more work). You'd save out the data then use this to rebuild the assets on load. Obviously you could access "PlayerPrefs" to within limits, and in standalone mode possibly System.IO stuff:
public static bool WriteTextFile(string nData, string nAddress){
System.IO.File.WriteAllText(Application.dataPath + nAddress, nData);
AssetDatabase.ImportAsset (nAddress);
return(true);
}//nData is the string data, nAddress is the address of the file
I'm not sure how well this System.IO stuff works in exes/apps etc though. I know it flunks in web players for what I guess is security reasons (Web apps that write files are frowned upon), and it seems unreliable in the editor at this end (saved assets need manually re-imported). On the web, PHP would probably be your friend (you'd post your data and retrieve it from a server).
I hope that this is helpful, and would love to know if you found a more elegant solution.
Yeah, Right now i'm serializing the objects loading/saving them through large chunks of data written away to the playerprefs for now, it seems to be the best solution.