准备工作
UObject* Asset
UObject* Asset
通常指的是一个指向UObject
的指针。UObject
是Unreal Engine中的基类,几乎所有的引擎对象都继承自UObject
。这个指针可以引用任何派生自UObject
的对象,比如蓝图、材质、贴图、音频资源等资产。
如果你看到UObject* Asset
,它通常用于指向一个已加载或正在使用的引擎资产。你可以使用UObject*
来动态加载资产。例如,使用StaticLoadObject()
函数从文件路径加载一个资产。
UObject* Asset = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/MyAssetPath.MyAsset"));
Data Structure
TArray<FString> PrefixArray | Slow in look up(查找缓慢) |
TMap<UClass*,FString> PrefixMap | Fast in look up |
inside the map you will have two elements, one is the key, one is the value
and for the key, it is our UClass (material/texture/blueprint)
TMap<UClass*, FString> PrefixMap
类型映射(TMap
),它将一个键值对映射起来。
-
UClass*
是指向一个类对象的指针。UClass
示类的元类对象,通过UClass*
,可以引用 UE里的各种类类型, 如material,texture,blueprint。 -
FString
是一个字符串类型,存储文本数据,对应地如"M_" "T_" "BP_"。
build-in function
PrefixMap.Find(SelectedObject->GetClass())
includes and TPairs
#include "Materials/Material.h"
#include "Materials/MaterialInstanceConstant.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"
{UBlueprint::StaticClass(),TEXT("BP_")},
{UStaticMesh::StaticClass(),TEXT("SM_")},
{UMaterial::StaticClass(), TEXT("M_")},
{UMaterialInstanceConstant::StaticClass(),TEXT("MI_")},
{UMaterialFunctionInterface::StaticClass(), TEXT("MF_")},
{USoundCue::StaticClass(), TEXT("SC_")},
{USoundWave::StaticClass(), TEXT("SW_")},
{UTexture::StaticClass(), TEXT("T_")},
{UTexture2D::StaticClass(), TEXT("T_")},
{UUserWidget::StaticClass(), TEXT("WBP_")},
{USkeletalMeshComponent::StaticClass(), TEXT("SK_")},
{UNiagaraSystem::StaticClass(), TEXT("NS_")},
//{UNiagaraEmitter::StaticClass(), TEXT("NE_")}
示例代码
quickAssetAction.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AssetActionUtility.h"
#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"
#include "QuickAssetAction.generated.h"//should be the last one, otherwise will have error
/**
*
*/
UCLASS()
class SUPERMANAGER_API UQuickAssetAction : public UAssetActionUtility
{
GENERATED_BODY()
public:
//UFUNCTION(CallInEditor)
//void TestFuckingFunc();
UFUNCTION(CallInEditor)
void BatchDuplication(int32 NumOfDuplicates);
UFUNCTION(CallInEditor)
void AddPrefixes();
private:
TMap<UClass*, FString>PrefixMap =
{
{UBlueprint::StaticClass(),TEXT("BP_")},
{UStaticMesh::StaticClass(),TEXT("SM_")},
{UMaterial::StaticClass(), TEXT("M_")},
{UMaterialInstanceConstant::StaticClass(),TEXT("MI_")},
{UMaterialFunctionInterface::StaticClass(), TEXT("MF_")},
//{UParticleSystem::StaticClass(), TEXT("PS_")},
{USoundCue::StaticClass(), TEXT("SC_")},
{USoundWave::StaticClass(), TEXT("SW_")},
{UTexture::StaticClass(), TEXT("T_")},
{UTexture2D::StaticClass(), TEXT("T_")},
{UUserWidget::StaticClass(), TEXT("WBP_")},
{USkeletalMeshComponent::StaticClass(), TEXT("SK_")},
{UNiagaraSystem::StaticClass(), TEXT("NS_")},
//{UNiagaraEmitter::StaticClass(), TEXT("NE_")}
};
};
quickAssetAction.cpp
void UQuickAssetAction::AddPrefixes()
{
TArray<UObject*>SelectedObjects = UEditorUtilityLibrary::GetSelectedAssets();
uint32 Counter = 0;
for (UObject* SelectedObject : SelectedObjects)
{
if (!SelectedObject) continue;//空指针检查 SelectedObject 是 nullptr
FString* PrefixFound = PrefixMap.Find(SelectedObject->GetClass());
//这一行查找 SelectedObject 对象对应的类(SelectedObject->GetClass())在 PrefixMap 中的值。
// PrefixMap 是一个自己命名的TMap<UClass*, FString>键值对的映射,
// 其中键是 UClass*(对象的类)如material,值是 FString(M_)。
if (!PrefixFound || PrefixFound->IsEmpty())
{
Print(TEXT("Failed to find prefix for class ") + SelectedObject->GetClass()->GetName(), FColor::Red);
continue;
}
FString OldName = SelectedObject->GetName();
if (OldName.StartsWith(*PrefixFound))
{
Print(OldName + TEXT(" already has prefix added"), FColor::Red);
continue;
}
const FString NewNameWithPrefix = *PrefixFound + OldName;
UEditorUtilityLibrary::RenameAsset(SelectedObject, NewNameWithPrefix);
++Counter;//将计数器 Counter 的值增加 1。它用于记录处理了多少个对象。
//在脚本完成后,下面那串代码输出一条信息,例如 "成功处理了 X 个对象"。
}
if(Counter>0)
{
ShowNotifyInfo(TEXT("Successfully renamed " + FString::FromInt(Counter) + " assets"));
}
}