3d object automatically from Assets to Scene?

Is there any trick to bring 3d object automatically into scene view, whenever new .fbx object is saved into assets folder?

What I’m trying to do is fully automatic system that creates “turntable” screenshots of any 3d object that is moved into Unity’s “Assets” folder.

I already have the program to take “turntabe” screenshots, but the user needs to drag 3d object from “Assets” to “Hierarchy” manually and then press “play”. I’d like to automate this completely so that this program could run on server, without any user intervention.

(I’m considering separate plugin for importing 3d objects at runtime, but first I’d like to know if there is any way to do this without any 3rd party plugins.)

Hello,

For this you will need an Editor script, this is code that will run whilst in edit mode and not only when playing the game.

Make a new folder called ‘Editor’ and inside it make a new javascript called ‘AddNewImport’ and add this code:

class AddNewImport extends AssetPostprocessor {
	function OnPostprocessModel (editorGo : GameObject) {
		GameObject.Instantiate(editorGo, Vector3.zero, Quaternion.identity);
	}
}
  1. If the script is not within a folder called Editor it will complain
  2. If you rename the script make sure you also change the class name within the code to the same thing
  3. You can change the default location and rotation of the object by changing the instantiate code, e.g. changing Vector3.zero to Vector3(0, 10, 0) will change the default position from (0, 0, 0) to (0, 10, 0)

Scribe


Edit


So I finally got round to spending some time on this, I ended up having to have two scripts. The first goes in a folder called ‘Editor’ the second goes on an object in scene, probably best to be something you only have one instance of but for testing I attached it to my camera.

Asset2SceneEditor.js

class Asset2Scene extends AssetPostprocessor {
	function OnPostprocessModel (editorGo : GameObject) {
		Asset2Scene.modelPath = assetPath; //change to 'ScriptName'.modelPath if other script is not called Asset2Scene
	}
}

put this in the Editor folder, the only thing you might have to change is line 3, see comment in code

Asset2Scene.js

@script ExecuteInEditMode()

var withReplacement : boolean = true;
private var GOmaster : GameObject;
private var GOclone : GameObject;

static var modelPath : String;

function Update () {
	if(!EditorApplication.isPlaying){
		if(modelPath != null){
			InstantiateModel(modelPath);
	 	}
	}
}

function InstantiateModel (goPath : String) {
	if(GOclone != null && withReplacement){
		DestroyImmediate(GOclone);
	}
	
	GO = AssetDatabase.LoadAssetAtPath(goPath, GameObject) as GameObject;
	GOclone = GameObject.Instantiate(GO, Vector3.zero, Quaternion.identity);
	modelPath = null;
}

This should be attached to an object in your scene such as you camera, you should only ever have 1 instance of this script in scene.

There should be one exposed variable in the inspector which is called With Replacement. If this is true, new imports will destroy the scene instance of the last imported asset, not the actual asset. If it is set to false it will import the new asset and keep the old one in scene.

Hope this helps, sorry for the delay.

Scribe