UE4 Plugin - Editor Toolbar Button

Modify according to the example on the Internet

  1. Add plugin Editor Toolbar Button name: EditorTools

insert image description here
2. Edit the code in the file EditorTools.cpp

//当点击工具栏上的按钮是就会在场景中创建一个圆锥体
void FEditorToolsModule::PluginButtonClicked() {
    
    
#if 0
	// Put your "OnButtonClicked" stuff here
	FText DialogText = FText::Format(
		LOCTEXT("PluginButtonDialogText", "Add code to {0} in {1} to override this button's actions"),
		FText::FromString(TEXT("FEditorToolsModule::PluginButtonClicked()")),
		FText::FromString(TEXT("EditorTools.cpp"))
	);
	FMessageDialog::Open(EAppMsgType::Ok, DialogText);
#else
	//通过当前编辑视图实例获取当前World
	UWorld* World = GCurrentLevelEditingViewportClient->GetWorld();
	if (World) {
    
    
		//在视图中放置StaticMeshActor
		AStaticMeshActor* Actor = World->SpawnActor<AStaticMeshActor>(FVector(0, 0, 100), FRotator::ZeroRotator);
		//加载引擎Asset资源
		UStaticMesh* ConeMesh = LoadObject<UStaticMesh>(NULL, TEXT("/Engine/BasicShapes/Cone.Cone"), NULL, LOAD_None, NULL);
		UMaterial* Mat = LoadObject<UMaterial>(NULL, TEXT("/Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial"), NULL, LOAD_None, NULL);
		if (ConeMesh) {
    
    
			//设置Mesh材质
			ConeMesh->SetMaterial(0, Mat);
			//设置MeshComponent的Mesh
			Actor->GetStaticMeshComponent()->SetStaticMesh(ConeMesh);
		}
		//刷新编辑中的视图,如果不加这行,则需要鼠标触发一下视图,视图才会刷新
		GEditor->RedrawLevelEditingViewports(true);
	}
#endif
}

After compilation, an EditorTools button will appear on the toolbar.
insert image description here
Click this button, and a cone will appear in the scene.
insert image description here

aaa

Guess you like

Origin blog.csdn.net/wb175208/article/details/127890940