Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by supermarten · Nov 21, 2015 at 07:45 PM · prefabimporting assetsmeshfilterassetpostprocessorprefabutility

Create prefab from model in AssetPostprocessor without losing mesh connections?

I'm trying to create a prefab from a model in OnPostprocessModel. The prefab gets created and the model hierarchy is added but the Meshes in the Meshfilters are all set to None. Can anyone explain this behaviour?

What I do is basically this:

   public void OnPostprocessModel(GameObject go) {
       PrefabUtility.CreatePrefab("Assets/test.prefab", go);
   }


,

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dan_at_sop · Nov 26, 2015 at 02:09 PM

I had this same problem just today - surprisingly close to you!

I figured you have the save the prefab's meshes again. This fixed it for me:

 MeshFilter[] mfs = toPrefab.GetComponentsInChildren<MeshFilter>();
                 foreach(MeshFilter mf in mfs) {
                     Debug.Log("Saving mesh: "+mf.name +"_M" );     
                     char[] invalids = System.IO.Path.GetInvalidFileNameChars();
                     String newName = String.Join("_", mf.name.Split(invalids, StringSplitOptions.RemoveEmptyEntries) ).TrimEnd('.');
                     AssetDatabase.CreateAsset(mf.mesh, "Assets/TempPrefabs/SavedMesh/" + newName +"_M" + ".asset");
                 }
 
 
                 PrefabUtility.CreatePrefab("Assets/TempPrefabs/Resources/"+toPrefab.gameObject.name+".prefab",toPrefab.gameObject,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 Carocrazy132 · Feb 21, 2020 at 07:18 AM 0
Share

This fixed the issue for me, I confirmed hours ago that my mesh and texture were being imported correctly, but when I checked my prefab it was just a blank $$anonymous$$esh Renderer and $$anonymous$$esh Filter. Saving the asset allows the prefab to be created as expected.

avatar image
1

Answer by AtsumuOno · Jun 14, 2017 at 10:04 AM

https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPostprocessModel.html

Any references to game objects or meshes will become invalid after the import has been completed. Thus it is not possible to create a new prefab in a different file from OnPostprocessModel that references meshes in the imported fbx file.

Please try the OnPostprocessAllAssets method.

 static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
     {
         foreach (string assetPath in importedAssets)
         {
             if (assetPath.Contains("Your target asset path") && System.IO.Path.GetExtension(assetPath) == "Your target asset extension")
             {
                var go =  (GameObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject));
                PrefabUtility.CreatePrefab("Assets/test.prefab", go);
             }
         }
     }
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
0

Answer by say_forever · Nov 23, 2015 at 12:16 PM

 public Transform prefab;
     void Start() {
             Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
     }

reference this

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 supermarten · Nov 23, 2015 at 01:50 PM 0
Share

Sorry, I was asking how to create the prefab, not how to instantiate it. That it is made through an assetpostprocessor is also important. I made this more clear in the description

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

8 People are following this question.

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

Related Questions

can't save data in prefab and getting an error while press "Apply" in prefab! 0 Answers

Mesh missing in prefab 0 Answers

can't save data in prefab and getting an error while press "Apply" in prefab! 0 Answers

PrefabUtility.LoadPrefabContentsIntoPreviewScene states that it returns a GameObject, signature is void. 1 Answer

PrefabUtility.CreatePrefab() results in "not a valid asset file name" error? 2 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