目录
在之前一章中,我们创建了插件,用来扩展编辑器的使用:
UE5.1编辑器拓展【一、脚本化资产行为,通知,弹窗,高效复制多个同样的资产】-CSDN博客
现在我们需要的是能够更快的对资产的名字进行一个更改,比如对前缀名的添加,或者是后缀的删除等等。
要更改资产的名字,首先我们要先了解几个相关的编辑器函数,以及FString的使用:
首先是RenameAsset,这个函数是属于UEditorUtilityLibrary,使用的时候就是输入资产和新的名字。
然后是FString的使用,这里只需要+即可,比如你要加前缀Prefix,则Prefix+name即可,后缀也是一样。
当然这只是一种做法,除此之外,还有通过判断类型来直接添加名字。
如果是需要自定义选择前缀名,那么我们需要的是输入一个FString和一个bool。
通过bool来判断你是要添加前缀还是后缀。
首先遍历选择的资产,然后检查是否有效,然后判断是添加前缀还是后缀,最后进行添加。
然后右下角通知添加前缀成功。
- void UQuicklyAssetAction::AddAssetPreOrEndfixes(FString Assetfixes, bool PrefixOrPostfix)
- {
- TArray
SelectedObjects = UEditorUtilityLibrary::GetSelectedAssets(); - uint32 Counter = 0;
- for (UObject* SelectedObject : SelectedObjects)
- {
- if (!SelectedObject)//检查是否有效
- {
- continue;
- }
- FString NewName;
- if (PrefixOrPostfix)//添加前缀
- {
- NewName = Assetfixes + SelectedObject->GetName();
- }
- else//添加后缀
- {
- NewName = SelectedObject->GetName()+ Assetfixes;
- }
- UEditorUtilityLibrary::RenameAsset(SelectedObject, NewName);
- Counter++;
- }
- if (Counter>0)
- {
- if (PrefixOrPostfix)
- {
- ShowNotifyInfo("Prefix add "+FString::FromInt(Counter)+" files",7);
- }
- else
- {
- ShowNotifyInfo("Postfix add " + FString::FromInt(Counter) + " files", 7);
- }
- }
- }
可以看到里面有相关注释和行为:

然后点击:


首先我们做一个映射,这个映射的作用就是判断你的资产是属于哪种类的存在:
- TMap
PrefixMap = - {
- {UBlueprint::StaticClass(),TEXT("BP_")},
- {UStaticMesh::StaticClass(),TEXT("SM_")},
- {UMaterial::StaticClass(),TEXT("M_")},
- {UMaterialInstanceConstant::StaticClass(),TEXT("MI_")},
- {UMaterialFunction::StaticClass(),TEXT("MF_")},
- {UParticleSystem::StaticClass(),TEXT("PS_")},
- {USoundCue::StaticClass(),TEXT("SC_")},
- {USoundWave::StaticClass(),TEXT("SW_")},
- {UTexture::StaticClass(),TEXT("T_")},
- {UTexture2D::StaticClass(),TEXT("T2D_")},
- {UUserWidget::StaticClass(),TEXT("UI_")},//用户控件无效,需要继续寻找
- {USkeletalMeshComponent::StaticClass(),TEXT("SK_")},
- {UNiagaraSystem::StaticClass(),TEXT("NS_")},
- {UNiagaraEmitter::StaticClass(),TEXT("NE_")}
- };
做好这个映射之后【有些可能有问题,需要自己去重新查找一下相关的类】
接下来和之前一样,去遍历我们选择的资产,然后寻找资产的类,来获取我们做的映射中的value值【前缀】。
然后在获取资产名字来判断这个资产是否已经有了和这个一样的前缀,有了就不加了。
然后继续做一个判断,假如你做了一个材质实例,那么其名字必然是M_XXX_Inst,我们如果直接添加了名字,就会变成MI_M_XXX_Inst,所以在添加之前,我们需要先判断一下,前缀是不是M_和后缀是不是_Inst。
是的话,就进行删除,然后在重命名。
- void UQuicklyAssetAction::WithClassAddAssetPrefixes()
- {
- //将选择的资产放入数组
- TArray
SelectedObjects = UEditorUtilityLibrary::GetSelectedAssets(); - uint32 Counter = 0;
- //遍历选择的资产
- for (UObject* SelectedObject : SelectedObjects)
- {
- //如果无效则跳入下一次迭代
- if (!SelectedObject)
- {
- continue;
- }
- //返回的是FString指针,通过选择资产的类作为key来找到PrefixMap映射中的value,如M_
- FString* PrefixFound =PrefixMap.Find(SelectedObject->GetClass());
- //判断是否有效和存在
- if (!PrefixFound || PrefixFound->IsEmpty())
- {
- //无效则打印没有找到,然后输出这个类的类的名字
- DebugPrint(TEXT("Failed to find Prefix for class ") + SelectedObject->GetClass()->GetName(), FColor::Red);
- DebugPrintLog(TEXT("Failed to find Prefix for class ") + SelectedObject->GetClass()->GetName());
- continue;
- }
- //获取老资产的名字
- FString OldName = SelectedObject->GetName();
- //判断资产的名字是否以PrefixFound开头:如M_
- if (OldName.StartsWith(*PrefixFound))
- {
- DebugPrint(OldName + " already have the prefix!", FColor::Red);
- DebugPrintLog(OldName + " already have the prefix!");
- continue;
- }
- //这里做材质实例的检查
- if (SelectedObject->IsA
()) - {
- //检查有没有,有就移除
- OldName.RemoveFromStart("M_");
- OldName.RemoveFromEnd("_Inst");
- }
- //做新的名字
- const FString NewNameWithPrefix = *PrefixFound + OldName;
- //重命名和计数
- UEditorUtilityLibrary::RenameAsset(SelectedObject, NewNameWithPrefix);
- Counter++;
- }
- if (Counter>0)
- {
- ShowNotifyInfo("Success rename Asset " + FString::FromInt(Counter) + "Assets", 7);
- }
- }
需要注意的是这种方法我们需要对相关类型的头文件进行添加,以及相关的模块。
模块添加:

头文件添加

- #include "Materials/Material.h"
- #include "Materials/MaterialInstanceConstant.h"
- #include "Particles/ParticleSystem.h"
- #include "Sound/SoundCue.h"
- #include "Sound/SoundWave.h"
- #include "Engine/Texture.h"
- #include "Blueprint/UserWidget.h"
- #include "Components/SkeletalMeshComponent.h"
- #include "NiagaraSystem.h"
- #include "NiagaraEmitter.h"


