Reimport a specific folder?

I think the subject line says it all.

But, is it possible to make an Editor script (callable through the Editor menus) where we call the Editor rightmouse command “Reimport” on a folder, but hardcode it to the folder “Resource/Models”?

Just wondering if this is possible?

6 Answers

6

This little script will import all the assets contained in the folder you currently have selected, and all it’s sub-folders. Just save this script in a folder in your project called Editor. Next, go to Assets > Reimport Folder and it will reimport the folder you currently have selected.

import System.IO;

@MenuItem( "Assets/Reimport Folder" )

static function ImportFolder()
{
	var realPath : String = Application.dataPath;
	realPath = realPath.Remove( realPath.Length - 6 );	
	var selectedPath : String = realPath + AssetDatabase.GetAssetPath( Selection.activeObject );	
	var fileEntries : String[] = Directory.GetFiles( selectedPath, "*", SearchOption.AllDirectories );
	for( var file : String in fileEntries )
	{
		file = file.Replace( "\\", "/" );
		file = file.Remove( 0, realPath.Length );
		AssetDatabase.ImportAsset( file );
	}
}

cool! very interesting. Didnt know I could use AssetDatabase object without having the real Unity asset server.

AssetDatabase is for everyone, it's the (local) database of assets in your project.

As Warwick says, it's the API you use from inside Unity to reference stuff inside your project folder. Also means you can, in edit mode, load in stuff without using Resources.Load.

Yummy! thanks for making this so obvious to me now. Thanks a lot! Great!

AssetDatabase is for everybody, it's the (nearby) database of advantages in your venture.

For people looking for an easier way. I found that calling AssetDatabase.ImportAsset method with the ImportAssetOptions.ImportRecursive and ImportAssetOptions.DontDownloadFromCacheServer options re imports everything in a folder.

`string folderPath = “Assets/Art/”;

AssetDatabase.ImportAsset(folderPath ,
ImportAssetOptions.ImportRecursive
| ImportAssetOptions.DontDownloadFromCacheServer);`

(Whoops, deleted my response...) Neat, thanks! For context, Kruskal's algorithm is just a way of generating a minimum spanning tree. Which takes a graph representation and removes all unnecessary edges. There's definitely a better and more complex explanation, which I have no idea how to write, so I'll just send an image to better demonstrate: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d2/Minimum_spanning_tree.svg/1200px-Minimum_spanning_tree.svg.png

i needed this to reload screenshots that i use as savegame thumbnails.
just saying that this did the job for me:

AssetDatabase.Refresh();

Why would you need to reimport?

Any directories within the resource folder can be accessed with Resources.Load() and obviously if you overwrite a resource, which you have pointed to in your could it will be automatically “reimported” anyway. If this isn’t what you looking for, comment back :slight_smile:

we often ajust the fbx models and needs to reimport certain folders. Sometimes we also add new fbx or remove some of the old ones, so we remove them all from a folder and replace them with the "active" one. Then we do a reimport.

Unfortunately, if multiple files are modified at once (when I switch branches in git) sometimes Unity is unable to reimport files correctly. There appear some missing references or unintended overrides in scenes or prefabs. And if you modify and checkout these prefabs/scenes, the corruptions also made themselves into the repo. So every time I checkout a new commit, I need to reimport both "Assets/Prefabs" and "Assets/Scenes" folders.

Air resistance is the <i>drag</i> value, and it's calculated every physics cycle using something like: rb.velocity = rb.velocity * (1.0f - (rb.drag / Time.fixedDeltaTime)); It's a pretty lousy implementation, but that's PhysX's fault... Yes, if you have the default fixed time step of 50 physics cycles per second, a <i>drag</i> value of 50 will stop the object's movement every frame... And yes, it IS dependent on the <i>Time.fixedDeltaTime</i> value.

I still don’t get it. You copy the script in a txt flile, name it randomly and place it inside Editor folder in Unity installation dirrectory? It doesn’t work for me. I hope that this script would solve my problem: when I import a big pack of assets linked together(by copying all the folders of the pack inside Assets folder), when Unity imports all, it crashes. If I import only parts of assets, all the links are broken.

To just get a Single Asset to refresh I’ve got following out of @Joshua 's Code in c#

		public static void ReImport(this GameObject go){
			string selectedPath;
			if(go.scene.IsValid()){
				Object realGO = PrefabUtility.GetCorrespondingObjectFromSource(go);
				selectedPath = AssetDatabase.GetAssetPath(realGO);
				AssetDatabase.ImportAsset(selectedPath);
				Debug.Log(" Scene Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+selectedPath+"</i></color>");
			}else{
				selectedPath = AssetDatabase.GetAssetPath(go);
				AssetDatabase.ImportAsset(selectedPath);
				Debug.Log(" Asset was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+selectedPath+"</i></color>");
			}
		}

Its been a while since I knew what a minimum spanning tree or Kruskal's algorithm are, but if you just need a graph representation then you should have all that information ready to go, since a graph is just a set of vertices and edges between vertices. You can call GetEdges from your Delaunator object to get the set of edges, and the vertices should just be the points that you fed into the constructor.