Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jeremy.schleining · Dec 18, 2013 at 12:15 AM · importassetdatabasedirectoryasset pathonpostprocessallassets

How to make a new path register in the AssetDatabase?

Hi all,

So I am writing an Editor script that catches fbx files on import and performs various functions on them during the OnPreprocess and OnPostprocess phase. One of the tasks I am attempting to perform is to change the filepath on import. The art team controls where files exist in the file path, so they will be putting a subfolder in the Custom User Properties of a model, which this script will then read. Once it reads it, the script should check to see if the desired file path exists, and if not, create it. Afterwards, it should move the asset to the new file path. At this point I have it creating the new file path, but it does not register with the AssetDatabase and I always get the following error:

ERROR: Parent directory is not in asset database

I am including sample code here to show what I am attempting. I have removed all code that is irrelevant to this specific task.

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 public class CustomAssetImporter : AssetPostprocessor
 {
     // contains the variables placed directly 
     // on the model by the artists
     private static Dictionary<string, string> _modelInfo;
 
     static void OnPostprocessAllAssets( string[] _imported, 
                                        string[] _deleted, 
                                        string[] _moved, 
                                        string[] _fromPath )
     {
         // for each imported item...
         foreach(string item in _imported)
         {
             // ...if the imported item is a model...
             if( item.EndsWith(".fbx") || item.EndsWith(".FBX") )
             {
                 // ...get the filepath...
                 string finalPath = CustomAssetImporter.getPath(item);
                 
                 // ... and then move the incoming fbx
                 // this is where it breaks. It always returns the following:
                 // ERROR: Parent directory is not in asset database
                 //     even though I can look at the project file structure 
                 //    and see that the folders exist. wtf?
                 string error = AssetDatabase.MoveAsset(item, finalPath);
                 Debug.Log ("ERROR: " + error);
             }
             
         }
     }
 
     private static string getPath(string _asset)
     {
         string returnInfo = "";
         string path = "Assets/Objects/";
         
         // get the path of the incoming model
         string[] incomingPath = _asset.Split( new char[] {'/'}, 
                                               StringSplitOptions.None);
         
         // modify the path string
         // subfolder is a variable placed on the fbx by the artist
         if(CustomAssetImporter._modelInfo["subfolder"] != null)
         {
             path += CustomAssetImporter._modelInfo["subfolder"];
         }
         path += "/" + incomingPath[incomingPath.Length - 1];
         
         // create file structure if it doesn't exist
         // and return the final path
         returnInfo = CustomAssetImporter.getPathFromString(path);
         
         return returnInfo;
     }
 
     public static string getPathFromString(string _path)
     {
         string[] oldPath = _path.Split( new char[] {'/'}, 
                                         StringSplitOptions.None);
         string newPath = "Assets";
         
         for(int i = 0; i < oldPath.Length; i++)
         {
             // if not Assets... We can skip Assets. If Assets doesn't 
             // exist, we have a whole different issue altogether
             if(oldPath[i] != "Assets") 
             {
                 // if not an actual file...
                 if(oldPath[i].IndexOf('.') == -1) 
                 {
                     // if the directory does not exist...
                     if( !Directory.Exists(newPath + "/" + oldPath[i]) )
                     {
                         // create the directory underneath parent...
                         string guid = AssetDatabase.CreateFolder(newPath, 
                                                                  oldPath[i]); 
                         newPath += "/" + path[i];
                     }
                     else
                     {
                         // if it does exist, move to the 
                         // next part of the path...
                         newPath += "/" + path[i];
                     }
                 }
             }
         }
 
         // step forward one frame. This is the most recent 
         // test to try to get the new file structure to
         // register in the AssetDatabase
         EditorApplication.Step();
 
         return newPath + "/";
     }
 }

Essentially, as i understand it what is happening is that while the file path is being created, for some reason it is not registering in the AssetDatabase. I have tried AssetDatabase.Refresh() already, as well as a number of random things I have found elsewhere, to no avail.

Thank you in advance for any advice or assistance. If more information is needed, I will provide whatever I can.

-Jeremy

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nschrag · Jul 15, 2014 at 11:19 PM

I believe the AssetDatabase will not and cannot be refreshed from an asset processor. Asking to import while already importing would easily hang Unity from bad recursion. So, you are correct in surmising that AssetDatabase.CreateFolder isn't in the database for this current import cycle.

Try using EditorApplication.delayCall to execute code on the next Editor "frame", along with C# anonymous function:

EditorApplication.delayCall += () => {
    string error = AssetDatabase.MoveAsset(item, finalPath);
    Debug.Log ("ERROR: " + error);
};

I suspect that if you had multiple layers of dependent AssetDatabase modifications, you could nest anonymous functions within eachother to properly stagger them.

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 graslany · Nov 27, 2014 at 10:02 AM

Since this answer was quite old I launched a new one ont his topic and finally found a solution I think: http://answers.unity3d.com/questions/840974/how-to-move-an-asset-to-a-newly-created-directory.html

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

19 People are following this question.

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

Related Questions

Edit asset path/directory before importing 0 Answers

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

OnPostprocessAllAssets is not doing anything 1 Answer

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

Load system resources (not built-in font for iOS/Android). 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