Create a prefab from an instantiated object in the editor
Hello,
I'm trying to create a prefab from an object at runtime in the editor and I'm having issues. The idea is to create the prefab from a script (usingPrefabUtility.CreatePrefab(...)) but as dragging & dropping the item does not work either, I'll simply explain the issue using this "manual" process because it seems easier to reproduce.
In the editor, if I drag & drop my fbx model, change materials/shaders, then drag & drop in the prefabs folder, everything works fine.
Now, in my use case, a gameobject is created "dynamically" using the same fbx model and the materials/shaders are also modified by script to get the exact same object as in step 1. If I press "Pause" in the editor before dragging & dropping the object in the prefabs folder, the generated prefab is broken (pink texture as no materials/shaders were saved).
The only differences I see between the GameObjects from both steps are that the second one (step 2) is obviously named "myObject(Clone)" with its shaders named "shaderName (Instance)" (as it was instantiated from a script) and that the shaders are in reverse order in the components list (which should not be a problem).
To clarify, here are some screenshots (uploaded outside of this website because a question can apparently contain only 2 attached images):
http://www.noelshack.com/2016-36-1473241780-chairs.jpg
http://www.noelshack.com/2016-36-1473241780-chair-inspector-working.jpg
http://www.noelshack.com/2016-36-1473241780-chair-prefab-working.jpg
http://www.noelshack.com/2016-36-1473241780-chair-inspector-notworking.jpg
http://www.noelshack.com/2016-36-1473241780-chair-prefab-notworking.jpg
Any help would be much appreciated, feel free to ask for clarifications if needed.
======= Edit =======
Here is a minimal (non-)working example:
https://mega.nz/#!HwQyXC4Y!_Tr736a7lHkI5M5O0P6F5obkrddH1qBrFeDD2S8MZm0
If you don't want to download the project, here is the code used in it:
using UnityEngine;
using UnityEditor;
public class Test : MonoBehaviour
{
void Start () {
Object chairObject = Resources.Load("ChairA");
GameObject chairGameObject = (GameObject) Instantiate(chairObject,
new Vector3(-.5f, 0, 0), Quaternion.identity);
chairGameObject.transform.Rotate(0,250,0);
//Removing the following line make the prefab "work" for some reason
Material[] materials = chairGameObject.GetComponent<Renderer>().materials;
//Could modify materials/shaders here but simply getting a reference
//to the list of materials seems to be a problem to save the prefab...
PrefabUtility.CreatePrefab("Assets/Resources/SavedPrefabs/test.prefab",
chairGameObject);
GameObject prefab = Resources.Load("SavedPrefabs/test") as GameObject;
GameObject chairPrefab = (GameObject) Instantiate(prefab,
new Vector3(.5f, 0, 0), Quaternion.identity);
chairPrefab.transform.Rotate(0, 250, 0);
}
}
Answer by qdrien · Sep 08, 2016 at 08:55 AM
Apparently, I did fix the issue myself, by replacing:
Material[] materials = chairGameObject.GetComponent<Renderer>().materials;
by:
Material[] materials = chairGameObject.GetComponent<Renderer>().sharedMaterials;
I can now modify materials/shaders using this "shared reference" and then create the prefab which seems to be saved properly. I won't delete the question in case someone faces the same (strange, at least to me) issue in the future.
Your answer
