- // Fill out your copyright notice in the Description page of Project Settings.
- #include "MySoundAndEffects.h"
- #include "Animation/AnimInstance.h"
- #include "AudioController.h"
- // Sets default values for this component's properties
- UAudioController::UAudioController()
- {
- // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
- // off to improve performance if you don't need them.
- PrimaryComponentTick.bCanEverTick = true;
- // ...
- audio_lfoot = CreateDefaultSubobject<UAudioComponent>("audio_lfoot");
- audio_rfoot = CreateDefaultSubobject<UAudioComponent>("audio_rfoot");
- }
- //UAudioController::UAudioController(USkeletalMeshComponent* mesh_)
- //{
- // // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
- // // off to improve performance if you don't need them.
- // PrimaryComponentTick.bCanEverTick = true;
- //
- // // ...
- //
- //
- //
- //}
- void UAudioController::my_init(USkeletalMeshComponent* mesh_) {
- this->mesh = mesh_;
- //UAudioController();
- audio_lfoot->SetupAttachment(mesh_, FName(*bonename_lfoot));
- audio_rfoot->SetupAttachment(mesh_, FName(*bonename_rfoot));
- }
- // Called when the game starts
- void UAudioController::BeginPlay()
- {
- Super::BeginPlay();
- // ...
- }
- // Called every frame
- void UAudioController::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- // ...
- if (mesh) {
- UAnimInstance* anim=mesh->GetAnimInstance();
- if (anim) {
- TArray<const struct FAnimNotifyEvent*> AnimNotifies=anim->NotifyQueue.AnimNotifies;
- range(i,0,AnimNotifies.Num()) {
- FString NotifyName=AnimNotifies[i]->NotifyName.ToString();
- /*GEngine->A/ddOnScreenDebugMessage
- (-1, 5.f, FColor::Red, FString::FromInt(i)+" "+ NotifyName);*/
- if (NotifyName=="lfoot_touchground") {
- audio_lfoot->SetSound(sb_walks[0]);
- audio_lfoot->Play();
- }
- else if (NotifyName == "rfoot_touchground") {
- audio_rfoot->SetSound(sb_walks[1]);
- audio_rfoot->Play();
- }
- else {
- }
- }
- }
- }
- else {
- throw std::exception("mesh not exist!!");
- }
- }
来源: http://www.cnblogs.com/wzj998/p/7512799.html