• UE5.1编辑器拓展【一、脚本化资产行为,通知,弹窗,高效复制多个同样的资产】


    目录​​​​​​​

    插件制作

    添加新的类:AssetActionUtility

    添加新的模块:EditorScriptingUtilities

    路径了解

    添加debug的头文件

    代码【debug.h】内涵注释:

    写函数

    .h文件

    .cpp文件


    插件制作

    首先第一步是做一个插件:

    因为是用于编辑器的,所以在模块中我们需要进行更改:

    将类型改为Editor,下方的加载则是在默认之前:PreDefault.

    添加新的类:AssetActionUtility

    AssetActionUtility可以用于创建、删除和重命名资产,以及管理资产的引用关系。

    将创建的这个类放入插件中,并命名:

    此时此刻我们会发现是编译失败的,那是因为我们缺失了相关的模块。

    所以我们对模块进行添加:

    在添加了相应的模块之后,我们发现,竟仍然是无法识别头文件的状态:

    这种时候,我们就需要去这个模块的build.cs中去查看:

    在发现这个路径之后,我们来到我们现在插件的build.cs中去进行添加:

    1. System.IO.Path.GetFullPath(Target.RelativeEnginePath) + "/Source/Editor/Blutility/Private"
    2. System.IO.Path.GetFullPath(Target.RelativeEnginePath)是获取虚幻引擎的相对路径
    3. "/Source/Editor/Blutility/Private"是模块文件的位置

    这样就可以编译成功了。

    添加新的模块:EditorScriptingUtilities

    路径了解

    了解一下关于路径的概念:

    路径分为两种,分别是Object Path和Package Path.

    假设现在有资产a在文件夹test里面:

    Object Path的意思就是直接到资产的名字,那么地址就是/Game/test/a

    Package Path的意思就是/Game/test/

    添加debug的头文件

    这个所谓的debug头文件是自己创建的,主要是为了方便使用更多的其他debug函数。

    首先要准备3个头文件:

    #include "Misc/MessageDialog.h"

    这个头文件的作用是弹出对话框的
    #include "Framework/Notifications/NotificationManager.h"
    #include "Widgets/Notifications/SNotificationList.h"

    这两个头文件的作用是弹出通知的

    代码【debug.h】内涵注释:

    这个并不用cpp文件

    1. #pragma once
    2. #include "Misc/MessageDialog.h"
    3. #include "Framework/Notifications/NotificationManager.h"
    4. #include "Widgets/Notifications/SNotificationList.h"
    5. void DebugPrint(const FString& DebugMessage, const FColor& DebugColor)
    6. {
    7. //将打印到屏幕上
    8. if (GEngine)
    9. {
    10. GEngine->AddOnScreenDebugMessage(-1, 8, DebugColor, DebugMessage);
    11. }
    12. }
    13. void DebugPrintLog(const FString& DebugMessage)
    14. {
    15. UE_LOG(LogTemp, Warning, TEXT("%s"), *DebugMessage);
    16. }
    17. //弹出窗口,返回窗口类型
    18. EAppReturnType::Type ShowMsgDialog(EAppMsgType::Type MsgType, const FString& Message,bool bShowMessageAsWarning=true )
    19. {
    20. if (bShowMessageAsWarning)
    21. {
    22. FText MsgTitle = FText::FromString("!Warning!");
    23. //第一个输入弹出的窗口类型,第二个是警告信息,第三个是消息标题
    24. return FMessageDialog::Open(MsgType, FText::FromString(Message), &MsgTitle);
    25. }
    26. else
    27. {
    28. return FMessageDialog::Open(MsgType, FText::FromString(Message));
    29. }
    30. }
    31. //消息通知
    32. void ShowNotifyInfo(const FString& Message,float FadeOutTime)
    33. {
    34. FNotificationInfo NotifyInfo(FText::FromString(Message));
    35. NotifyInfo.bUseLargeFont = true;//是否用较大粗体显示字体
    36. NotifyInfo.FadeOutDuration = FadeOutTime;//淡出时间
    37. //添加通知
    38. FSlateNotificationManager::Get().AddNotification(NotifyInfo);
    39. }

    写函数

    在了解了一些基本知识后,我们开始在继承了AssetActionUtility的文件的头文件中写一个复制资产的函数:

    .h文件

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "AssetActionUtility.h"
    5. #include "QuicklyAssetAction.generated.h"
    6. /**
    7. *
    8. */
    9. UCLASS()
    10. class CUSTOMEDITOREXTEND_API UQuicklyAssetAction : public UAssetActionUtility
    11. {
    12. GENERATED_BODY()
    13. public:
    14. UFUNCTION(CallInEditor)
    15. void DuplicateAssets(int32 DuplicateNum);
    16. };

    然后到cpp文件中进行实现,实现的同时也要包含制作的debug头文件,用于debug:

    .cpp文件

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "AssetAction/QuicklyAssetAction.h"
    3. #include "Debug.h"
    4. #include "EditorUtilityLibrary.h"
    5. #include "EditorAssetLibrary.h"
    6. void UQuicklyAssetAction::DuplicateAssets(int32 DuplicateNum)
    7. {
    8. if (DuplicateNum<=0)
    9. {
    10. ShowMsgDialog(EAppMsgType::Ok, "Please enter a valid value >0", true);
    11. return;
    12. }
    13. //获取选择的资产数据,比如获取的是一个,还是多个资产
    14. TArray SelectedAssetsData= UEditorUtilityLibrary::GetSelectedAssetData();
    15. //用于计数的Counter,复制成功则+1,记录总共复制了多少资产
    16. uint32 Counter = 0;
    17. //迭代选择的SelectedAssetsData数组
    18. for (const FAssetData& SelectedAssetData:SelectedAssetsData)
    19. {
    20. //这里迭代的是数组中的一个资产要复制的数量
    21. for (int32 i = 0; i < DuplicateNum; i++)
    22. {
    23. FString SourceAssetPath = SelectedAssetData.ObjectPath.ToString();//获取资产路径【路径名字+资产名字】
    24. FString NewDuplicatedAssetName = SelectedAssetData.AssetName.ToString()+TEXT("_")+FString::FromInt(i+1);//新复制的资产名字
    25. FString NewPathName = FPaths::Combine(SelectedAssetData.PackagePath.ToString(), NewDuplicatedAssetName);//将包路径【没有资产名字的路径】和新的名字结合
    26. //DuplicateAsset输入的两个,第一个是源路径,第二个是目标路径
    27. //这里判断是否复制资产是否有效,有效就保存,并且Counter+1
    28. if (UEditorAssetLibrary::DuplicateAsset(SourceAssetPath, NewPathName))
    29. {
    30. //保存资产,输入路径。 第二个输入是bool,为是否为改变后进行保存【而我们要直接保存,所以是false】
    31. UEditorAssetLibrary::SaveAsset(NewPathName,false);
    32. Counter++;
    33. }
    34. }
    35. }
    36. if (Counter>0)//这里是通知有多少资产复制成功了
    37. {
    38. ShowNotifyInfo("Duplicate Success:" + FString::FromInt(Counter) + " Files!",7);
    39. }
    40. }

  • 相关阅读:
    Ubuntu提示 “unable to find the VMX binary ‘D:\VM17\vmware-vmx.exe‘“
    【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
    textarea去除红色波浪线
    LeakCanary(4)面试题系列
    GFS分布式文件系统
    CSRF漏洞简介
    Maven 打包方式探究
    Golang 面向对象深入理解
    如何选择正确的文档归档解决方案
    FineReport中字符串常用处理函数
  • 原文地址:https://blog.csdn.net/q244645787/article/details/133385100