Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
2
Question by Lance Sun · Dec 06, 2009 at 02:53 AM · prefabimporteditor-scripting

Creating a complex prefab during asset postprocessing

In OnPostprocessModel, I want to prepare a GameObject that is parented to another GameObject, and then create a prefab out of that. This works fine, but it leaves an instance in the scene. When I try to destroy the object after creating the prefab, the editor crashes. Here's the code:

public class MyAssetPostprocessor : AssetPostprocessor { public void OnPostprocessModel(GameObject gameObject) { // create an empty gameobject and add the mesh as a child GameObject prefabRoot = new GameObject("foo"); gameObject.transform.parent = prefabRoot.transform;

     // create the prefab, and connect the dummy prefab root to it
     Object prefab = EditorUtility.CreateEmptyPrefab("Assets/test.prefab");
     EditorUtility.ReplacePrefab(prefabRoot, prefab, ReplacePrefabOptions.ConnectToPrefab);

     // cleanup the gameobject instance in the scene
     Object.DestroyImmediate(prefabRoot);
 }

}

I'm not convinced I'm heading down the right path. What's the correct way to create complex prefabs during asset postprocessing, involving the asset which is being imported?

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 Lance Sun · Dec 06, 2009 at 03:00 AM 0
Share

I get a little more mileage (e.g. editor doesn't crash) if I detach the game object being imported from the prefab root before I destroy the prefab root.

gameObject.transform.parent = null;

But a new object still appears in the scene as a disconnected prefab instance.

1 Reply

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

Answer by AngryAnt · Dec 07, 2009 at 11:12 AM

The GameObject being passed to OnPostprocessModel is not an in-scene GameObject, but the one going into your assets. Trouble starts when you attach this GameObject to an in-scene GameObject.

What you should do is:

  • Instantiate a new scene GO based on your Assets GO:
    • GameObject newGO = Instantiate (gameObject, Vector3.zero, Quaternion.Identity);
  • Modify this newly created GO, parent etc.
  • Do your prefab dance.
  • Destroy the new GO - not the original Assets one.
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 07, 2009 at 11:31 AM 0
Share

Ah, I see. What I'm seeing now is that if I destroy the prefab root GO, the whole prefab still remains up in the scene as a disconnected prefab. If I destroy the instantiated scene GO (but not prefab root) then only the prefab root remains in the scene, as a disconnected prefab.... I'll keep futzing with it.

avatar image Lance Sun · Dec 07, 2009 at 11:37 AM 0
Share

$$anonymous$$, it works now. I was using ReplacePrefabOptions.ConnectToPrefab ins$$anonymous$$d of ReplacePrefabOptions.Default.

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

No one has followed this question yet.

Related Questions

How do I programmatically assign a GameObject to a prefab? 6 Answers

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