UE4----C++调用蓝图类生角色加入场景和用纯C++生成角色加入场景中

2 篇文章 0 订阅

一、用纯C++生成角色加入场景中

c++创建蓝图类继承ACharacter

在构造函数中添加如下代码:

AXbNpcCharacter::AXbNpcCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder<USkeletalMesh> npcSkeletalMesh(TEXT("SkeletalMesh'/Game/Mannequin/Character/Mesh/SK_Mannequin.SK_Mannequin'"));
	static ConstructorHelpers::FObjectFinder<UBlueprint> npcAmin(TEXT("AnimBlueprint'/Game/Mannequin/Animations/ThirdPerson_AnimBP.ThirdPerson_AnimBP'"));
	UAnimBlueprintGeneratedClass* AminClass = LoadObject<UAnimBlueprintGeneratedClass>(UAnimBlueprintGeneratedClass::StaticClass(),TEXT("AnimBlueprint'/Game/Mannequin/Animations/ThirdPerson_AnimBP.ThirdPerson_AnimBP'"));	
	m_pMeshComp= CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("NpcMesh"));
	m_pMeshComp->SetSkeletalMesh(npcSkeletalMesh.Object);
	m_pMeshComp->SetAnimationMode(EAnimationMode::AnimationBlueprint);
	UClass* ap= (UClass*)npcAmin.Object->GeneratedClass;
	m_pMeshComp->SetAnimInstanceClass(ap);
	m_pMeshComp->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
  //	RootComponent = m_pMeshComp;
	m_pMeshComp->AttachTo(RootComponent);
}
在当前控制角色PlayerCharacher添加代码

void AboostCharacter::addPlayers()
{
	AXbNpcCharacter * npc_p = NewObject<AXbNpcCharacter>();


    if (FindActor(npc_p->GetName()))
    {	
		//TODO
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("the npc is exist"));
	}
	else {
		AXbNpcCharacter * npc_2 = GetWorld()->SpawnActor<AXbNpcCharacter>(npc_p->GetActorClass(), FVector(300, 34, 300), FRotator(0, 0, 0));
		// 下面可以动态替换mesh网格
		//npc_2->GetMesh()->SetSkeletalMesh(mesh);
	}
   
}
头文件设置蓝图调用

UFUNCTION(BlueprintCallable, Category = "MYGAME")
void addPlayers();
然后在打开关卡蓝图,直接蓝图调用。


二、C++调用蓝图类生角色加入场景
新建蓝集成刚建好的C++ ACharacter

玩家控制的Character 构造函数中添加代码

static ConstructorHelpers::FObjectFinder<UBlueprint> XbNpc(TEXT("Blueprint'/Game/boost/BP/npc.npc'"));
	npc = (UClass*)XbNpc.Object->GeneratedClass;
修改addPlayers函数

void AboostCharacter::addPlayers()
{
    if (FindActor(npc->GetName()))
      {	
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("the npc is exist"));
      }
     else {
		GetWorld()->SpawnActor(npc); //添加第一个
		GetWorld()->SpawnActor(npc); //添加第二个
		GetWorld()->SpawnActor(npc);	//...
     }   
}




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值