Hi,
I have a job to use PlayAnimation interface(UUserWidget::PlayAnimation) by C++, but the first parameter is the type of UWidgetAnimation, and the animation was created in UMG Editor. How can i get the animation by C++?
You may have solved this but I’m posting a response in case it helps someone else.
I have a UMyUserWidget (name changed to protect the innocent!) class derived from UUserWidget. The following member function can be used to get a pointer to each UWidgetAnimation. I call this ONCE, when the class is instantiated, to set up a list of animation pointers that can be referenced in functions like PlayAnimation(). NOTE: Doing this in the constructor may be too early so you need to find an appropriate place that fits with your particular usage.
void UMyMenuWidget::AssignAnimations()
{
UProperty* prop = GetClass()->PropertyLink;
// Run through all properties of this class to find any widget animations
while( prop != nullptr )
{
// Only interested in object properties
if( prop->GetClass() == UObjectProperty::StaticClass() )
{
UObjectProperty* objectProp = Cast<UObjectProperty>(prop);
// Only want the properties that are widget animations
if( objectProp->PropertyClass == UWidgetAnimation::StaticClass() )
{
UObject* object = objectProp->GetObjectPropertyValue_InContainer( this );
UWidgetAnimation* widgetAnim = Cast<UWidgetAnimation>(object);
if( widgetAnim != nullptr )
{
// DO SOMETHING TO STORE OFF THE ANIM PTR HERE!
// E.g. add to a TArray of some struct that holds info for each anim
}
}
}
prop = prop->PropertyLinkNext;
}
}
This is the only way I could find to address this issue without using blueprint visual script.
Useful note: GetFName() on a UWidgetAnimation returns the name assigned in UMG editor with “_INST” appended. You can use GetFName() on the UProperty to get the name as set in UMG.
You saved my life, man!
Thanks a lot, calling this in NativeContruct works fine! However this is quite ugly nevertheless. See the feature request for some kind of UPROPERTY(meta = (BindWidgetAnimation)) here: Please add UPROPERTY(meta = (BindWidget)) support for UWidgetAnimation! - Feedback for Unreal Engine team - Unreal Engine Forums
Yeah. I agree it’s ugly. A more immediate and official solution would be nice. Could always hide it away in a function so you don’t need to look at it.
Hellp . I have a good way.I did it in version 4.21.
.h
TMap<FString, class UWidgetAnimation*> FindWidgetAnimation;
.cpp
// 获取UI动画
UWidgetBlueprintGeneratedClass* WidgetClass = GetWidgetTreeOwningClass();
FindWidgetAnimation.Empty();
//循环遍历
for (int i = 0; i < WidgetClass->Animations.Num(); i++) {
FString Name = WidgetClass->Animations[i]->GetName();
FindWidgetAnimation.Add(Name, WidgetClass->Animations[i]);
}
if (WidgetClass->Animations[0] != NULL) {
UE_LOG(LogTemp, Warning, TEXT("Yes,this is Name is = %s"), *WidgetClass->Animations[0]->GetName());
}
else {
UE_LOG(LogTemp, Warning, TEXT("并没有获取到"));
}
this is perfect thank you so much!
Since Unreal Engine 4.21 I use the Keyword “BindWidgetAnim”
.h
UPROPERTY(meta = (BindWidgetAnim))
UWidgetAnimation* WidgetAnimationTest;
Both names, the animation in the editor and the animation variable in the header, must be the same
Thank you!
Since Unreal Engine 5.0, the UPROPERTY must be
UPROPERTY(Transient, meta = (BindWidgetAnim))
UWidgetAnimation* myAnimationName = nullptr;
Like for widget binding, the name of variable and name into editor must be the same.
cool ! man
thanks
IIII LOVEEEEE YOU!!!
Define proper type of animation ptr in base class and add binding specifier to its uproperty marco.
So wired it is