当前位置: 首页 > article >正文

UE5 C++: 插件编写04 | 增加和删改前缀

准备工作

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> PrefixMapFast 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"));
	}
}

插件效果

优化补充:删除MI类型的默认前后缀

M_ xxxxx_Inst

示例代码

		//如果类型是MI_old name里有M_和_Inst,移除他们。
		if (SelectedObject->IsA<UMaterialInstanceConstant>())
		{
			OldName.RemoveFromStart(TEXT("M_"));
			OldName.RemoveFromEnd(TEXT("_Inst"));
		}


http://www.kler.cn/news/323133.html

相关文章:

  • 蓝桥杯【物联网】零基础到国奖之路:十二. TIM
  • 数据结构 ——— 顺序表oj题:编写函数,合并两个有序数组
  • 【分布式微服务云原生】windows+docker+mysql5.7.44一主一从主从复制
  • TDengine 在业务落地与架构改造中的应用实践!
  • RK3568笔记六十三:基于LVGL的Linux相机
  • 基于python+flask+mysql的音频信息隐藏系统
  • 9.27 C++模板
  • Spring Boot 进阶-Spring Boot 开发第一个Web接口
  • 监控易监测对象及指标之:全面监控Oracle ODBC数据库
  • 汽车传感器的针脚数量因传感器类型和应用而异。
  • 图论系列(dfs)9/24
  • 解决你的IDE在使用的时候测试单元@Test在创建Scanner对象是键盘键入不了的问题;
  • jupyter快捷键
  • 猎板PCB大讲堂:PCB谐振效应及其对设计的影响
  • 探索高效中文分词:elasticsearch-analysis-hanlp 插件深度解析
  • Spring Cloud Alibaba-(4)Sentinel【流控和降级】
  • 每日一题|2516. 每种字符至少取 K 个|双指针、最长子串、字典
  • WebRTC中的维纳滤波器实现详解:基于决策导向的SNR估计
  • Ubuntu一些文件及问题研究分析
  • LabVIEW提高开发效率技巧----使用状态机架构
  • 华为云技术深度解析:Flexus X实例与GitLab的云端协作实践
  • pgsql
  • uniapp view增加删除线
  • 二维数组的创建和初始化
  • 插入排序(insertion sort)
  • self-supervised, weakly supervised, and supervised respectively区别
  • Django中媒体文件的配置
  • UnityHub下载任意版本的Unity包
  • C++ STL初阶(14): map和set
  • C#:动态为Object对象添加新属性的方法