- Home /
Possible to move directory without reimporting assets?
When I drag a sub directory from a directory to another in the Project view, the directory gets moved instantly-ish. When I move the same sub directory using an editor script, Unity reimports all of the assets in the moved directory.
I have tried two different ways of moving files in my script:
FileUtil.MoveFileOrDirectory("Assets/Resources/DirName", "Assets/Audio/DirName");
And
try {
Directory.Move("Assets/Resources/DirName", "Assets/Audio/DirName");
}
catch (Exception e) {
Debug.Log("Move failed: "+e.Message);
}
Is there any way of moving a folder using a script, without triggering a reimport?
Answer by mandarin · May 30, 2013 at 01:34 PM
The solution was to move the files using AssetDatabase.MoveAsset(). That will make sure to keep the meta files intact and it doesn't trigger a reimport.
Here's an example:
DirectoryInfo dir = new DirectoryInfo("Assets/Dir");
FileInfo[] files = dir.GetFiles("*.wav");
foreach (FileInfo file in files) {
string status = AssetDatabase.MoveAsset(
"Assets/Dir/" + file.Name,
"Assets/NewDir/" + file.Name);
// Everything is OK if status is empty
if (status != "") {
Debug.Log(status);
}
}
Remember to include
using System.IO;
at the top of your class.
It still triggers a reimport, once you tabbed out and in to the Editor. (5.6.5f1)
Answer by Dave-Carlile · May 30, 2013 at 12:25 PM
The only relevant documentation that I'm aware of says to only move folders from within the editor.
"Never move any assets or organize this folder from the Explorer (Windows) or Finder (OS X). Always use the Project View!
There is a lot of meta data stored about relationships between asset files within Unity. This data is all dependent on where Unity expects to find these assets. If you move an asset from within the Project View, these relationships are maintained. If you move them outside of Unity, these relationships are broken. You'll then have to manually re-link lots of dependencies, which is something you probably don't want to do."
Calling functions to do the moving is most likely the equivalent of using Explorer or Finder.
Hi Dave.
Thanks. After a couple of tries, I figured there were some meta-files that got out of sync. I did however find a solution. I'll post it as an answer to my own question.
What was your solution? How did you get the meta-files in sync again?
Not sure why this answer was downvoted. This is a perfectly acceptable solution to move assets and ensure the assets is not re-imported. It is also useful for rena$$anonymous$$g folders or assets.
Answer by KirillKuzyk · Jun 13, 2017 at 08:55 PM
I was wondering how to move folders with AssetDatabase.MoveAsset() for a long time.
Finally, I have an answer. To do this, you need to create a destination folder hierarchy. For example:
This code will only work if a folder Assets/WebGLTemplates/Resources exists.
AssetDatabase.MoveAsset("Assets/Resources/sounds", "Assets/WebGLTemplates/Resources/sounds");
Your answer
Follow this Question
Related Questions
OnSceneGUI mouse Dragging 0 Answers
Can't select objects or move them. 2 Answers
Editor bug: "fps keys" don't work anymore 1 Answer
Editor in game? 0 Answers
Force reimport ScriptableObject 1 Answer