0

My original prefabs(which is attached models, shader, animation, material, scripts, etc) were disappeared by accident. I can only download unity3d file through server. Downloaded objects is visible in UnityEditor. But I can't store them as prefabs(using drag and drop OR script). All components missed.

Question:
- Why this is impossible(gameobject is loaded in RAM but why can't I store them in disk)?
- How can I recover them?

KimLikeAStone
  • 99
  • 1
  • 11

1 Answers1

2

The short answer would be no, you can not just create a prefab at runtime.

The reason for this is that making prefabs is editor only relying on prefabUtility which is part of the UnityEditor namespace, hence only available outside of runtime (which relies on the UnityEngine namespace).

Assets also get pretty much all get locked to read-only during run time.

However that does not mean it is completely impossible to restore your GameObjects as Prefabs.

You could write your own script that serializes the GameObjects you want as prefabs into a format like JSON or XML, then make an editor script that de-serializes this data and reconstruct a new prefab out of this. You would have to figure out how to do this precisely on your own though as i personally do not have any examples on how to best handle this.

more on JSON serialization can be found here

Remy
  • 4,843
  • 5
  • 30
  • 60
  • I appreciate your answer :-). But I have one more question. Can I recover mesh,animation clip, animation controller etc although I don't have these assets in my local project? I think json serialization helps to recover all the reference to actual assets but can't recover actual assets. Am I correct? – KimLikeAStone Jul 27 '18 at 01:40
  • Correct. I'm afraid you won't be able to recover any actual assets that are not in your project folder. The closest you will probably get to that is: Open an instance of unity with your current project, download the assetbundles and then opening a second instance of unity with a new project, in this new project you could then start reconstructing the Assetbundles using your original scene's assetbundles as a reference. Then save objects in your new project as prefab and copy them to your original project.. this is by no means perfect, and quite tedious... but probably closest you'll get – Remy Jul 30 '18 at 11:57