x


How to create prefabs from Editor scripts?

I want to have a custom menu item that creates a prefab for me. I tried doing this:

public class ImportAnimations : ScriptableWizard {

    public Texture2D spritesheet;
    public TextAsset spritedoc;

    [MenuItem("Custom/Animations/ImportAnimations...")]
    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard("Import Animations from:", typeof(ImportAnimations), "Import");  
    }

    void OnWizardCreate()
    {
        GameObject go = new GameObject("TestAsset");
        go.AddComponent(typeof(Animation));
        go.SetActiveRecursively(false);
        AssetDatabase.CreateAsset(go, "Assets/Prefabs/TestAsset.prefab");
        GameObject.DestroyImmediate(go);
    }
}

This creates the prefab, but it also keeps the GameObject in the scene. If I delete the GameObject the prefab will be empty (the Animation component is lost). If I delete the prefab from my project folder before deleting the GameObject in my scene Unity crashes. (reported bug) Is there another way to do this? Or is it just a bug? (using b7)

more ▼

asked Sep 18 '10 at 03:04 PM

StephanK gravatar image

StephanK
6k 39 53 93

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

After searching a bit more I found this answer which solved my problem:

http://answers.unity3d.com/questions/1125/how-do-i-programmatically-assign-a-gameobject-to-a-prefab

Keeping the question here, so the next one who's as dumb as I will have a better chance of finding the right answer.

more ▼

answered Sep 18 '10 at 03:43 PM

StephanK gravatar image

StephanK
6k 39 53 93

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x1253
x590
x419

asked: Sep 18 '10 at 03:04 PM

Seen: 8834 times

Last Updated: Sep 18 '10 at 03:04 PM