Change Default Actor Class used for Drag and Drop of Assets

This is a copy of a question I posted here: Change Default Actor Class used for Drag and Drop of Assets - C++ - Epic Developer Community Forums
Though up to now I have not had any luck with questions posted to the AnswerHub, so I thought I would also post the question here on the forums.


I would like to override the default class used for drag and drop in the editor for Static Meshes.

Right now when you drag and drop a Static Mesh asset into the scene it will create a StaticMeshActor using that static mesh asset.

I want to use a C++ Class which inherits from StaticMeshActor, let’s call it MyStaticMeshActor and make MyStaticMeshActor the default class for Static Mesh assets that are added to the scene using drag and drop operations.

As it is now, I have to add MyStaticMeshActor to the scene and select the static mesh I wish to use from the properties, this also has the disadvantage of having the default name being MyStaticMeshActor#, which makes for more work in renaming each actor used.

Any help would be greatly appreciated!

1 Like

There is a class called UActorFactoryStaticMesh that is responsible for creating an actor from a static mesh.

1 Like

Thanks Robert,

I had heard of the UActorFactory Classes from the Extending The Editor videos from Unreal and looked at the UActorFactoryStaticMesh class, however changing UActorFactoryStaticMesh requires the Unreal be built from source, which I have set up, though I don’t really like modifying the engine any more than I have to.

I was kind of hoping there would be an option to either change the Factory used for static meshes or there would be an editor option to change the default to an inherited class, though from all my searching I don’t think this is the case.

I think the best solution for my workflow is to create a plug-in that will replace the StaticMeshActor in the scene, copying the transform, name and static mesh properties, and assign that plug-in to a button or key board shortcut.

Thanks again for the response!

1 Like

You should just be able to create your own UActorFactory-derived class (inside an editor module) and give it a higher priority than the default static mesh one has.

1 Like

Thank you I did not know that!

What I just ended up doing was creating a Toolbar Button Plugin and adding this into PluginButtonClicked()


   
    TSet ComponentsToConsider;
    ComponentsToConsider.Add("StaticMeshComponent");

    TArray SelectedActors;
    GEditor->GetSelectedActors()->GetSelectedObjects(SelectedActors);

    UClass* ReplaceWithClass = AInheritedStaticMeshActor::StaticClass();

    GEditor->ConvertActors(SelectedActors, ReplaceWithClass, ComponentsToConsider, true);


I was completely unaware that UActorFactory Classes could override each other, I’ll have to look into that.

1 Like

Can you explain to me how to do that?

In your editor module StartupModule() function you need to first register your ActorFactory so the editor can use it:

	if (GEditor)
	{
		UCustomFactory* NewFactory = NewObject<UCustomFactory>();
		GEditor->ActorFactories.Insert(NewFactory,0);
	}

In other modules they usually use “Add” rather than “Insert” but when spawning the actor the engine looks for the first actor factory that can spawn the actor from the asset. Normally this would be the UActorFactoryStaticMesh which is usually in index 1. So if you are adding it, it will add to the end of GEditor’s array of ActorFactories so your new factory wont be used.

Also don’t forget to unregister your custom factory in your module ShutdownModule function.

	if (GEditor)
	{
		GEditor->ActorFactories.RemoveAll([](UActorFactory* Factory) 
		{
			return Factory && Factory->IsA<UCustomFactory>();
		});
	}

Also be careful of when your module is loaded as when using Default I found that GEditor was null so I used PostEngineInit and it seemed to work fine.

1 Like