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 /
avatar image
0
Question by bittam · Jun 25, 2011 at 08:31 PM · importassetdatabase

Running AssetDatabase.ImportPackage on multiple packages

I'm trying to make a script which takes an array of *.unitypackage files and runs AssetDatabase.ImportPackage on each one. It seems that the script is running so fast that Unity only imports the past package in the list. Has anyone else ran into this issue and / or found a workaround?

Here's the code snippit I'm working with:

 private static void ImportPackages()
 {
     //go through the entire resources directory
     string resourcePath = Application.dataPath + "/Resources";
     DirectoryInfo directory = new DirectoryInfo(resourcePath);

     //find all *.unitypackage files
     FileInfo[] unityPackageFileArray = directory.GetFiles("*.unitypackage", SearchOption.AllDirectories);

     for (int i = 0; i < unityPackageFileArray.Length; ++i)
     {
         //pared down filename starting from the Assets/ directory
         string localFilePath = unityPackageFileArray[i].FullName;
         int assetIndex = localFilePath.IndexOf("Assets\\");

         if (assetIndex < 0)
         {
             assetIndex = 0;
         }
         localFilePath = localFilePath.Substring(assetIndex, localFilePath.Length - assetIndex);
         localFilePath = localFilePath.Replace('\\', '/');

         Logger.Log("importing file: " + localFilePath);

         //import them into the project
         AssetDatabase.ImportPackage(localFilePath, true);
         
         AssetDatabase.Refresh();
     }        
 }


Thanks, ~Matt

Comment
Add comment · Show 5
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 Waz · Jun 26, 2011 at 06:06 AM 0
Share

Are you passing false for the interactive argument?

avatar image bittam · Jun 26, 2011 at 03:48 PM 0
Share

Yes I am, but even when I change it to true, it doesn't stop for each unitypackage. $$anonymous$$aybe this is a unity bug?

avatar image Graham-Dunnett ♦♦ · Jun 26, 2011 at 07:12 PM 0
Share

$$anonymous$$aybe add the script you use to your question. I would expect iterating over your array and calling ImportPackage on each one would work.

avatar image bittam · Jun 27, 2011 at 12:49 AM 0
Share

I just edited the original post to contain the code snippit I'm working with. Thanks guys.

avatar image bittam · Jul 28, 2011 at 02:03 AM 0
Share

i just downloaded the latest version of the unity editor (3.4.0f5) and i reran this code. i am still getting the same error. any other thoughts on why this is happening? has anyone else had the chance to run my original code and verify this functionality is broken?

thanks all!

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ADI · Jan 17, 2012 at 03:15 AM

I'd contacted Unity Support on this question, for a similar implementation using ImportPackage. The behavior we'd seen was the same - the import only occurs for the last package in the set. There is no workaround. Support confirmed this after we had provided examples of our attempts. ImportPackage doesn't work when called in close succession. Subsequent instances abort prior ones leaving only the last able to complete.

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 siberman · May 15, 2013 at 11:29 AM 0
Share

Any news on this? quite annoying.

avatar image Dizamok · Jul 10, 2013 at 03:24 PM 0
Share

I'm having the exact same problem. Does anyone know how to successfully import multiple packages from code/script ?

avatar image
0

Answer by zd · Jul 11, 2013 at 12:29 AM

I was able to get this working by instantiating an EditorWindow and using the Update function to spread out my ImportPackage calls. I had to put in a delay of about 10 frames between each call. You can't call ImportPackage on every update. I added the file paths to a list of strings.

 void Update()
 {
         if (packagePaths.Count > 0)
         {
             // wait for delay. Can't import multiple packages in close succession
             if (delayFrames > 0)
             {
                 delayFrames--;
                 return;
             }
             
             ProcessPackage(packagePaths[0]);
             packagePaths.RemoveAt(0);
             
             delayFrames = 10;
             
             return;
         }
 }
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 Dizamok · Jul 11, 2013 at 07:58 AM 0
Share

Thank you, it does work, you're a life saver !

However this obviously won't work if you want to use interactive import (second argument of ImportPackage).

avatar image zd · Jul 11, 2013 at 05:43 PM 0
Share

Glad to help. FYI, I ended up putting a delay between the AssetDatabase.ImportPackage and AssetDatabase.Refresh calls as well.

avatar image
0

Answer by avee · Jul 26, 2013 at 12:03 PM

This has been fixed in Unity 4.2, but still it seems that the actual import happens after all those successive ImportPackage calls. So if for instance you have a code like this:

 foreach(var path in packagePaths) {
     AssetDatabase.ImportPackage(path, false);
     someClass.prefab = (GameObject)Resources.LoadAssetAtPath(assetPath, typeof(GameObject));
 }

Is not going to work, someClass.prefab will be == null since the packages will actually be imported after all of those ImportPackage calls. Probably at the end of the editor's update frame.

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

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

9 People are following this question.

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

Related Questions

How to make a new path register in the AssetDatabase? 2 Answers

import package with script 1 Answer

Generated sprite asset cannot be tinkered like an image in editor 0 Answers

AssetDatabase.LoadAssetAtPath is null? Any way to load Sprite asset? 1 Answer

EditorSceneManager.OpenScene fails after AssetDatabase.Refresh 1 Answer


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