星鸿阁

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

NewObject from FAssetData class

[复制链接]

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
发表于 2021-3-23 16:10:48 | 显示全部楼层 |阅读模式
TArray<FAssetData> AssetDatas;
FContentBrowserModule& ContentBrowserModule = FModuleManager:oadModuleChecked<FContentBrowserModule>("ContentBrowser");
IContentBrowserSingleton& ContentBrowserSingleton = ContentBrowserModule.Get();
ContentBrowserSingleton.GetSelectedAssets(AssetDatas);
回复

使用道具 举报

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
 楼主| 发表于 2021-3-23 16:11:11 | 显示全部楼层
Finally got it done. Here is a basic sync loading version for future strugglers:

UAssetManager& AssetManager = UAssetManager::Get();
TArray<FAssetData> AssetDataList;
AssetManager.GetPrimaryAssetDataList(FPrimaryAssetType("MeshDataAsset"), AssetDataList);
for (FAssetData asset : AssetDataList) {
     UObject* LoadedAsset = asset.GetAsset();
     UBlueprint * CastedBP = Cast<UBlueprint>(LoadedAsset);
     if (CastedBP && CastedBP->GeneratedClass->IsChildOf(UMeshData::StaticClass()))
     {
         UClass * MeshDataClass = *CastedBP->GeneratedClass;
         UMeshData * Mesh = NewObject<UMeshData>(this, MeshDataClass);
     {
{

The problem was that asset.GetAsset() returns you not the class of the blueprint, but the blueprint itself. So you need to cast it and get the generated class from it. (works like a charm in editor, needs to be tested in cooked)

This way you can load ingame items/dataobjects in a list. Each is a blueprint with a c++ class parent. Also this can be easily updated for async load
回复

使用道具 举报

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
 楼主| 发表于 2021-3-23 16:11:35 | 显示全部楼层
回复

使用道具 举报

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
 楼主| 发表于 2021-3-23 16:12:02 | 显示全部楼层
FVector objectPosition(0, 0, 0);
        FRotator objectRotation(0, 0, 0); //in degrees
        FVector objectScale(1, 1, 1);
        FTransform objectTrasform(objectRotation, objectPosition, objectScale);

        // Creating the Actor and Positioning it in the World based in the Static Mesh
        UWorld * currentWorld = GEditor->LevelViewportClients[0]->GetWorld();
        ULevel * currentLevel = currentWorld->GetLevel(0);
        // For new versions this instead:
        // UWorld* currentWorld = GEditor->GetEditorWorldContext().World();
        // ULevel * currentLevel = currentWorld->GetCurrentLevel();
        UClass * staticMeshClass = AStaticMeshActor::StaticClass();

        AActor * newActorCreated = GEditor->AddActor(currentLevel, staticMeshClass, objectTrasform, true, RF_Public | RF_Standalone | RF_Transactional);

        AStaticMeshActor * smActor = Cast(newActorCreated);

        smActor->GetStaticMeshComponent()->SetStaticMesh(myStaticMesh);
        smActor->SetActorScale3D(objectScale);
        // ID Name & Visible Name
        smActor->Rename(TEXT("MyStaticMeshInTheWorld"));
        smActor->SetActorLabel("MyStaticMeshInTheWorld");

        GEditor->EditorUpdateComponents();
        smActor->GetStaticMeshComponent()->RegisterComponentWithWorld(currentWorld);
        currentWorld->UpdateWorldComponents(true, false);
        smActor->RerunConstructionScripts();
        GLevelEditorModeTools().MapChangeNotify();
回复

使用道具 举报

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
 楼主| 发表于 2021-3-24 16:14:55 | 显示全部楼层
   AStaticMeshActor* NewBlock = GetWorld()->SpawnActor<AStaticMeshActor>(FVector(300,-200,0), FRotator(0, 0, 0));
     NewBlock->SetActorScale3D(FVector(1, 1, 0.1f));
     NewBlock->SetMobility(EComponentMobility::Movable);
     TArray<UStaticMeshComponent*> Comps;
     NewBlock->GetComponents(Comps);
     if (Comps.Num() > 0)
     {
         UStaticMeshComponent* FoundComp = Comps[0];
         FoundComp->SetStaticMesh(pCubeMesh);
     }
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|starfluidga

GMT+8, 2025-3-10 05:13 , Processed in 0.020438 second(s), 20 queries .

Made by Liga 星鸿阁

Copyright © 2020-2048, LigaStudio.

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