星鸿阁

 找回密码
 立即注册
搜索
热搜: 活动 交友 动画
查看: 1099|回复: 0

Editor上编译蓝图的方法

[复制链接]

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
发表于 2022-1-23 23:28:08 | 显示全部楼层 |阅读模式
  1. <font size="6">// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

  2. #include "testmyplugin.h"
  3. //--#include "FWindowTestStyle.h"
  4. #include "FWindowTestCommands.h"
  5. #include "LevelEditor.h"
  6. #include "Editor.h"
  7. //#include "Kismet2/KismetEditorUtilities.h"
  8. <span style="background-color: yellow;">#include "Kismet2/KismetEditorUtilities.h"</span>
  9. #include "LevelEditorViewport.h"
  10. #include "Engine/SkeletalMesh.h"
  11. //#include "Editor/EditorEngine.h"
  12. #include "Engine/Selection.h"
  13. #include "ContentBrowserModule.h"
  14. #include "IContentBrowserSingleton.h"
  15. #include "Engine/StaticMeshActor.h"
  16. #include "Animation/SkeletalMeshActor.h"
  17. #include "Components/SkeletalMeshComponent.h"


  18. #define LOCTEXT_NAMESPACE "FtestmypluginModule"
  19. #define PIE 3.1415926
  20. void FtestmypluginModule::StartupModule()
  21. {
  22.         // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
  23.           // 注册编辑器插件风格
  24. //--        FWindowTestStyle::Initialize();
  25. //--        FWindowTestStyle::ReloadTextures();

  26.         // 注册编辑器插件命令
  27.         FWindowTestCommands::Register();
  28.         //FtestmypluginModule::TestAction();
  29.         PluginCommands = MakeShareable(new FUICommandList);
  30.         PluginCommands->MapAction(
  31.                 FWindowTestCommands::Get().OpenPluginWindow,
  32.                 FExecuteAction::CreateRaw(this, &FtestmypluginModule::GetSeltion)
  33.         );

  34.         FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");

  35.         {
  36.                 TSharedPtr<FExtender> NewToolbarExtender = MakeShareable(new FExtender);
  37.                 /**
  38.                  * Extends a tool bar at the specified extension point
  39.                  *
  40.                  * @param        ExtensionHook                        Part of the menu to extend.  You can extend the same point multiple times, and extensions will be applied in the order they were registered.
  41.                  * @param        HookPosition                        Where to apply hooks in relation to the extension hook
  42.                  * @param        CommandList                                The UI command list responsible for handling actions for the toolbar items you'll be extending the menu with
  43.                  * @param        ToolbarExtensionDelegate        Called to populate the part of the toolbar you're extending
  44.                  *
  45.                  * @return        Pointer to the new extension object.  You can use this later to remove the extension.
  46.                  */
  47.                 NewToolbarExtender->AddToolBarExtension("Content",
  48.                         EExtensionHook::Before,
  49.                         PluginCommands,
  50.                         FToolBarExtensionDelegate::CreateRaw(this, &FtestmypluginModule::AddToolbarButton));
  51.                 LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(NewToolbarExtender);
  52.         }

  53.         //FExecuteAction::CreateRaw(this, &FtestmypluginModule::TestAction);

  54. }
  55. /*void FtestmypluginModule::AddToolbarButton(FToolBarBuilder& Builder)
  56. {
  57.         Builder.AddToolBarButton(FWindowTestCommands::Get().OpenPluginWindow);
  58. }*/
  59. void FtestmypluginModule::AddToolbarButton(FToolBarBuilder& Builder)
  60. {
  61.         Builder.AddToolBarButton(FWindowTestCommands::Get().OpenPluginWindow);
  62. }

  63. void FtestmypluginModule::ShutdownModule()
  64. {
  65.         // This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
  66.         // we call this function before unloading the module.
  67.         FWindowTestCommands::Unregister();
  68. }
  69. void FtestmypluginModule::TestAction()
  70. {
  71.         UE_LOG(LogTemp, Warning, TEXT("It Works!!!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
  72. }
  73. FVector huayuan(int shu,float r)
  74. {
  75.         FVector newpos(0,0,0);

  76.                 //int y = sin(i * PIE / 180) * r + posy;  // sin、cos用的是弧度,需要转换
  77.                 newpos.Y = sin(shu * PIE / 180) * r;
  78.                 newpos.X = cos(shu * PIE / 180) * r;

  79.                 return newpos;
  80.         //        putpixel(x, y, 0xFFFFFF);     // 画点
  81.         //        Sleep(10);

  82.         
  83. }
  84. void FtestmypluginModule::GetSeltion()
  85. {
  86.         TArray<FAssetData> AssetDatas;  //选择列表容器
  87.         FContentBrowserModule& ContentBrowserModule = FModuleManager::LoadModuleChecked<FContentBrowserModule>("ContentBrowser");
  88.         IContentBrowserSingleton& ContentBrowserSingleton = ContentBrowserModule.Get();
  89.         ContentBrowserSingleton.GetSelectedAssets(AssetDatas); //获取选择列表

  90.         //ContentBrowserSingleton.GetSelectedAssets(AssetDatas);
  91.         //UWorld * currentWorld = GEditor->LevelViewportClients[0]->GetWorld();
  92.         UClass * staticMeshClass =  AActor::StaticClass();
  93.         UWorld * currentWorld = GCurrentLevelEditingViewportClient->GetWorld();
  94.         ULevel * currentLevel = currentWorld->GetLevel(0);

  95.         FVector objectPosition(0, 0, 0);
  96.         FRotator objectRotation(0, 0, 0); //in degrees
  97.         FVector objectScale(1, 1, 1);
  98.         FTransform objectTrasform(objectRotation, objectPosition, objectScale);
  99.         cishu += 1;
  100.         for (int kk = 0; kk < AssetDatas.Num(); kk++)
  101.         {
  102.                
  103.                 if(AssetDatas[kk].AssetClass == FName("Blueprint"))
  104.                 {
  105.                         UBlueprint* Object;
  106.                         
  107.                         Object = Cast<UBlueprint>(AssetDatas[kk].GetAsset());
  108.                         FKismetEditorUtilities::CompileBlueprint(Object);
  109.                         AActor* NewBlock = currentWorld->SpawnActor(Object->GeneratedClass);
  110.                         NewBlock->SetActorLabel(TEXT("BP_" + Object->GetName()));
  111.                 //        UE_LOG(LogTemp, Warning, TEXT("content is %s"), *AssetDatas[kk].AssetClass.ToString());
  112.                 }
  113.                 else if (AssetDatas[kk].AssetClass == FName("StaticMesh"))
  114.                 {
  115.                         UStaticMesh* Object;
  116.                         Object = Cast<UStaticMesh>(AssetDatas[kk].GetAsset());
  117.                         int jiaodu = cishu * 10;
  118.                         if(jiaodu > (360* shengchang))
  119.                         {
  120.                                 shengchang += 1;
  121.                         }
  122.                         AStaticMeshActor* NewBlock = currentWorld->SpawnActor<AStaticMeshActor>(huayuan(jiaodu,500+100* shengchang), FRotator(0, jiaodu, 0));
  123.                         TArray<UStaticMeshComponent*> Comps;
  124.                         NewBlock->GetComponents(Comps);
  125.                         if (Comps.Num() > 0)
  126.                         {
  127.                                 UStaticMeshComponent* FoundComp = Comps[0];
  128.                                 FoundComp->SetStaticMesh(Object);
  129.                         }
  130.                         NewBlock->SetActorLabel(TEXT("SM_" + Object->GetName()));
  131.                         //ConstructorHelpers::FObjectFinder<UStaticMesh> pCube = Object;
  132.                 //        pCube.Object
  133.                         //00currentWorld->SpawnActor(Object->GetClass());
  134.                         //UE_LOG(LogTemp, Warning, TEXT("content is %s"), *AssetDatas[kk].AssetClass.ToString());
  135.                 }
  136.                 else if (AssetDatas[kk].AssetClass == FName("SkeletalMesh"))
  137.                 {
  138.                         USkeletalMesh* Object;
  139.                         Object = Cast<USkeletalMesh>(AssetDatas[kk].GetAsset());
  140.                 //00        currentWorld->SpawnActor(Object->GetClass());
  141.                         ASkeletalMeshActor* NewBlock = currentWorld->SpawnActor<ASkeletalMeshActor>(FVector(300, -200, 0), FRotator(0, 0, 0));
  142.                         TArray<USkeletalMeshComponent*> Comps;
  143.                         NewBlock->GetComponents(Comps);
  144.                         if (Comps.Num() > 0)
  145.                         {
  146.                         
  147.                                 USkeletalMeshComponent* FoundComp = Comps[0];
  148.                                 FoundComp->SetSkeletalMesh(Object);
  149.                         }
  150.                         NewBlock->SetActorLabel(TEXT("SK_"+Object->GetName()));
  151.                         //UE_LOG(LogTemp, Warning, TEXT("content is %s"), *AssetDatas[kk].AssetClass.ToString());
  152.                 }

  153.                 //00 Object = Cast<UBlueprint>(AssetDatas[kk].GetAsset());
  154.                 //UClass *CC = AssetDatas[kk].GetClass();
  155.                 //AssetDatas[kk].GetClass()
  156.                 //currentWorld->SpawnActor(Object->class);
  157.                 //UBlueprint* gen = Cast<UBlueprint>(Object);
  158.         //00        currentWorld->SpawnActor(Object->GeneratedClass);
  159.                 //AActor * newActorCreated = GEditor->AddActor(currentLevel, UObject::StaticClass(), objectTrasform, true, RF_Public | RF_Standalone | RF_Transactional);
  160.                 UE_LOG(LogTemp, Warning, TEXT("content is %s"),*AssetDatas[kk].AssetClass.ToString());
  161.         }


  162.         USelection* SelectedActors = GEditor->GetSelectedActors();
  163.         int cout = 0;
  164.         
  165.         if (SelectedActors)
  166.         {
  167.                 TArray<UObject*> SelectedActorsArr;
  168.                 SelectedActors->GetSelectedObjects(AActor::StaticClass(), SelectedActorsArr);
  169.                
  170.                 /*if (SelectedActorsArr.Num() != 1)
  171.                 {
  172.                         UE_LOG(LogTemp, Error, TEXT("SelectedActorNum != 1"));
  173.                         return;
  174.                 }*/
  175.                 for (int jj = 0; jj < SelectedActorsArr.Num(); jj++)
  176.                 {
  177.                         FTransform SelectedActorTransform;
  178.                         UObject* curObj = SelectedActorsArr[jj];
  179.                         AActor* Actor = Cast<AActor>(curObj);
  180.                         if (Actor)
  181.                         {
  182.                                 SelectedActorTransform = Actor->GetTransform();
  183.                                 //UStaticMeshComponent* Mesh;
  184.                                 //Mesh->GetStaticMesh()->GetNumVertices
  185.                                 //UStaticMeshComponent* Mesh;
  186.                                 TArray<UStaticMeshComponent*> Comps;
  187.                                 Actor->GetComponents(Comps);
  188.                                 
  189.                                 /*if (Comps.Num() > 0)
  190.                                 {


  191.                                         SelMesh = Comps[0];
  192.                                         cout = SelMesh->GetStaticMesh()->GetNumVertices(0);
  193.                                 }*/
  194.                                 for (int i = 0; i < Comps.Num(); i++)
  195.                                 {
  196.                                         //UStaticMeshComponent* SelMesh;
  197.                                         //SelMesh = Comps[i];
  198.                                         cout += Comps[i]->GetStaticMesh()->RenderData.Get()->LODResources[0].GetNumTriangles();
  199.                                 }
  200.                                 //        UActorComponent *a  = Actor->GetComponentByClass(UStaticMeshComponent::StaticClass());
  201.                                 //        Mesh = Cast<UStaticMeshComponent>(Actor->GetComponentByClass(UStaticMeshComponent::StaticClass()));
  202.                                         //UE_LOG(LogTemp, Warning, TEXT("SelectedActor Transform:%s"), *SelectedActorTransform.ToString());
  203.                                 UE_LOG(LogTemp, Warning, TEXT("Triangles Number : %i"), cout);
  204.                         }
  205.                         else
  206.                         {
  207.                                 return;
  208.                         }
  209.                 }
  210.         }
  211. }

  212. #undef LOCTEXT_NAMESPACE
  213.         
  214. IMPLEMENT_MODULE(FtestmypluginModule, testmyplugin)</font>
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|starfluidga

GMT+8, 2025-3-9 23:31 , Processed in 0.013362 second(s), 21 queries .

Made by Liga 星鸿阁

Copyright © 2020-2048, LigaStudio.

快速回复 返回顶部 返回列表