Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
14
Question by Lance Sun · Dec 04, 2009 at 06:19 AM · prefabimporteditor-scripting

How do I programmatically assign a GameObject to a prefab?

When importing a mesh, I want to automatically create a prefab for that mesh, and assign that mesh to the new prefab. I have something like this:

public class MyAssetPostprocessor : AssetPostprocessor
{
    public void OnPostprocessModel(GameObject gameObject)
    {
        if (assetPath.EndsWith("_model.fbx"))
        {
            string prefabPath = assetPath.Replace("_model.fbx", ".prefab");
            Object prefab = EditorUtility.CreateEmptyPrefab(prefabPath);
            // prefab.AddGameObject(gameObject); ?
        }
    }
}

But I don't know how to add the mesh to the prefab. In the editor, I can drag-and-drop it in the project window. How do I do this programmatically?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deadlycrow · Mar 26, 2014 at 01:31 AM 0
Share

and how the code is for java? i dont really get it how it works :(

6 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by Ricardo · Dec 05, 2009 at 02:48 PM

This is a snippet from the code I use to create prefabs from a group of selected objects:

[MenuItem ("My Project/Create Simple Prefab")]
static void DoCreateSimplePrefab()
{
    Transform[] transforms = Selection.transforms;
    foreach (Transform t in transforms) {
        Object prefab = EditorUtility.CreateEmptyPrefab("Assets/Temporary/"+t.gameObject.name+".prefab");
        EditorUtility.ReplacePrefab(t.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
    }
}

You may want to try with EditorUtility.ReplacePrefab, althought caveat emptor: I've only used it at editor time, not in the asset post-processor.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image huulong · Sep 20, 2015 at 07:15 AM 0
Share

As Soviut says, use PrefabUtility ins$$anonymous$$d of EditorUtility (current version: Unity 5.2)

avatar image
11
Best Answer

Answer by AngryAnt · Dec 05, 2009 at 03:32 PM

Use CreateEmptyPrefab to prepare your prefab asset. Then construct your desired GameObject as you would like it to be ingame. Finally use ReplacePrefab to store your GameObject in your prefab and Destroy the original GameObject if you don't want it in the scene.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lance Sun · Dec 06, 2009 at 01:31 AM 0
Share

Thansk. You also answered the next question I was about to ask, which was "I want to put my FBX mesh into another empty GameObject and create a prefab out of that, so that my prefab doesn't have an X rotation of 270."... it's not completely obvious you have to instantiate it in the scene then destroy it. Thanks!

avatar image Lance Sun · Dec 06, 2009 at 03:07 AM 0
Share

I'm having some issues with destroying the original GameObject because I don't want it in the scene. If you can help, I started a new question here

avatar image
13

Answer by Soviut · Nov 11, 2012 at 08:49 PM

You should now be using PrefabUtility, not EditorUtility for all prefab operations. Not only does it have a method for creating empty prefabs but it also has a method to create prefabs from existing GameObjects:

 GameObject prefab = PrefabUtility.CreatePrefab("Assets/Whatever.prefab", (GameObject)Selection.object, ReplacePrefabOptions.ReplaceNameBased);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
7

Answer by Robotron18 · Sep 02, 2010 at 03:49 PM

You can use ReplacePrefabOptions = ReplaceNameBased without destroying scene object problem.

    Object prefab = EditorUtility.CreateEmptyPrefab("Assets/test1.prefab");
    EditorUtility.ReplacePrefab(importedObject, prefab, ReplacePrefabOptions.ReplaceNameBased);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Aug 06, 2012 at 06:11 AM 1
Share

O$$anonymous$$G dude, you just made my day...I have been working on a Editor Extension to let me update Prefabs automatically, and although I had it working the way I (almost) wanted, I was having a problem with the Prefabs getting deleted and re-instantiated again, which would move them all back to the Prefab's default position/rotation...I have been going nuts for the past 3 hours trying to figure this out, and then your post finally made sense to me and I saw a big bright light :)

A BIG THAN$$anonymous$$ YOU lol

avatar image
-3

Answer by vivek2012.du · Jul 13, 2011 at 10:28 AM

Use Prefab in your Game Here is the tutorial for that . http://lnkd.in/J6ufVr . Here is demo and code for Prefab instantiate so download and try to undestand i hope u will get the solution ..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a complex prefab during asset postprocessing 1 Answer

Can I generate prefab instances based on locators inside a 3d model file? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Attaching scripts to Prefab objects... 1 Answer

Prefab problem... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges