UE4雜文:派生一個AnimInstance類(6月5日 更新)

後記:一覺醒來,突然覺得像下文這麼做有點多此一舉,所以大家還是使用TryGetPawnOwner吧。

書接上文 《UE4雜文:關於官方模板動畫藍圖中TryGetPawnOwner方法的討論》

上文修改了默認動畫藍圖中的節點,雖然邏輯上清晰了,但是由於步驟多了一些,所以藍圖有點亂了,既然是常用的方法,那不如直接派生一個AnimInstance,並增加這個獲取同級的Component的方法。

MyAnimInstance類

自定義了一個GetSiblingComponentByClass方法

//MyAnimInstance.h
#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "MyAnimInstance.generated.h"

UCLASS(transient, Blueprintable, hideCategories=AnimInstance, BlueprintType, meta=(BlueprintThreadSafe), Within=SkeletalMeshComponent)
class THIRDPERSONCPP_API UMyAnimInstance : public UAnimInstance
{
	GENERATED_BODY()
	UFUNCTION(BlueprintCallable)
	UActorComponent* GetSiblingComponentByClass(TSubclassOf<UActorComponent> ComponentClass) const;
};

//MyAnimInstance.cpp
#include "MyAnimInstance.h"

UActorComponent* UMyAnimInstance::GetSiblingComponentByClass(TSubclassOf<UActorComponent> ComponentClass) const
{
	USkeletalMeshComponent* OwnerComponent = GetSkelMeshComponent();
	if (AActor* OwnerActor = OwnerComponent->GetOwner())
	{
		return OwnerActor->GetComponentByClass(ComponentClass);
	}
	return NULL;
}

新建動畫藍圖

繼承自MyAnimInstance

在這裏插入圖片描述

這樣藍圖也簡化多了

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章