UE4类型与资源路径重定向
UE4重定向机制十分强大,可在不同范围对类型和资源路径进行重定向。
在4.24.3版本中,有两套实现方案:ActiveXXXRedirects和XXXRedirects
ActiveXXXRedirects是老的实现方案,功能较弱,未来会被淘汰
XXXRedirects又叫CoreRedirects,是新一代的重定向方案,功能更强大,可满足各种情况下的类型及资源路径的重定向
ActiveXXXRedirects老方案
具体包括4种类型:ActiveGameNameRedirects、ActiveClassRedirects、ActivePluginRedirects、ActiveStructRedirects
定义在:UnrealEngine\Engine\Source\Runtime\Engine\Classes\Engine\Engine.h
USTRUCT() struct FGameNameRedirect { GENERATED_USTRUCT_BODY() UPROPERTY() FName OldGameName; UPROPERTY() FName NewGameName; }; USTRUCT() struct FClassRedirect { GENERATED_USTRUCT_BODY() UPROPERTY() FName ObjectName; UPROPERTY() FName OldClassName; UPROPERTY() FName NewClassName; UPROPERTY() FName OldSubobjName; UPROPERTY() FName NewSubobjName; UPROPERTY() FName NewClassClass; UPROPERTY() FName NewClassPackage; UPROPERTY() bool InstanceOnly; }; USTRUCT() struct FStructRedirect { GENERATED_USTRUCT_BODY() UPROPERTY() FName OldStructName; UPROPERTY() FName NewStructName; }; USTRUCT() struct FPluginRedirect { GENERATED_USTRUCT_BODY() UPROPERTY() FString OldPluginName; UPROPERTY() FString NewPluginName; }; UCLASS(abstract, config=Engine, defaultconfig, transient) class ENGINE_API UEngine : public UObject , public FExec { // ... ... public: UPROPERTY(config) TArray<FGameNameRedirect> ActiveGameNameRedirects; UPROPERTY(config) TArray<FClassRedirect> ActiveClassRedirects; UPROPERTY(config) TArray<FPluginRedirect> ActivePluginRedirects; UPROPERTY(config) TArray<FStructRedirect> ActiveStructRedirects; // ... ... };
XXXRedirects新方案
具体包括6种类型:ClassRedirects、StructRedirects、EnumRedirects、FunctionRedirects、PropertyRedirects、PackageRedirects
定义在:CoreRedirects.h和CoreRedirects.cpp
enum ECoreRedirectFlags { None = 0, Type_Object = 0x00000001, Type_Class = 0x00000002, // ClassRedirects Type_Struct = 0x00000004, // StructRedirects Type_Enum = 0x00000008, // EnumRedirects Type_Function = 0x00000010, // FunctionRedirects Type_Property = 0x00000020, // PropertyRedirects Type_Package = 0x00000040, // PackageRedirects Type_AllMask = 0x0000FFFF, Option_InstanceOnly = 0x00010000, // 属性bInstanceOnly=True 只命中CDO创建出来的实例对象,CDO不受影响 Option_Removed = 0x00020000, // 属性Removed=True Option_MatchSubstring = 0x00040000, // 属性MatchSubstring=true OldName为当前对象的子串,即命中 Option_AllMask = 0xFFFF0000, Option_ExactMatchMask = Option_InstanceOnly | Option_Removed, }; struct COREUOBJECT_API FCoreRedirect { /** Flags of this redirect */ ECoreRedirectFlags RedirectFlags; /** Name of object to look for */ FCoreRedirectObjectName OldName; /** Name to replace with */ FCoreRedirectObjectName NewName; /** Change the class of this object when doing a redirect */ FCoreRedirectObjectName OverrideClassName; /** Map of value changes, from old value to new value */ TMap<FString, FString> ValueChanges; };
首先,要注意的是,重定向配置中填写的所有的类型要去掉前缀U、A、F、E等
重定向又分为两大类:类型重定向和资源路径重定向
类型重定向
重定向整个模块范围的类型
[CoreRedirects] ;BaseOpenCVLensCalibration.ini ;将名为MixedRealityCaptureCalibration的Module重定向到名为OpenCVLensCalibration上 +PackageRedirects=(OldName="/Script/MixedRealityCaptureCalibration", NewName="/Script/OpenCVLensCalibration") ;与上面做法等价 [/Script/Engine.Engine] +ActiveGameNameRedirects=(OldGameName="/Script/MixedRealityCaptureCalibration",NewGameName="/Script/OpenCVLensCalibration")
重定向指定Class类型
[CoreRedirects] ;BaseOpenCVLensCalibration.ini ;将MixedRealityCaptureCalibration模块中的MyTestObject类型重定向到OpenCVLensCalibration模块中的MyTestObject类型 +ClassRedirects=(OldName="/Script/MixedRealityCaptureCalibration.MyTestObject",NewName="/Script/OpenCVLensCalibration.MyTestObject") ;与上面做法等价 [/Script/Engine.Engine] +ActiveClassRedirects=(OldClassName="/Script/MixedRealityCaptureCalibration.MyTestObject", NewClassName="/Script/OpenCVLensCalibration.MyTestObject") ;---------------------------------------------------------------------------------------------------------------- [CoreRedirects] ;将对所有资源中的蓝图类型AnimNotify_PlaySound_C重定向到Engine模块中C++类型AnimNotify_PlaySound 注:这种情况下,UCLASS的底层类进行的更改,将OverrideClassName设置为/Script/CoreUObject.Class +ClassRedirects=(OldName="AnimNotify_PlaySound_C",NewName="/Script/Engine.AnimNotify_PlaySound",OverrideClassName="/Script/CoreUObject.Class") [/Script/Engine.Engine] ;将当前项目主模块中的AMyComponent类型重定向到TestModule模块中的ABaseComponent类型 +ActiveClassRedirects=(OldClassName="MyComponent",NewClassName="/Script/TestModule.BaseComponent") ;将TGame模块中的TMyCharacter类型重定向到NGame模块中的NMyCharacter类型 +ActiveClassRedirects=(OldClassName="/Script/TGame.TMyCharacter",NewClassName="/Script/NGame.NMyCharacter")
重定向指定Class类型的指定对象
[CoreRedirects] ;将Name为Wall9的StaticMeshActor对象的Name修改为Go +ClassRedirects=(OldName="/Script/Engine.StaticMeshActor",NewName="/Script/Engine.StaticMeshActor",ValueChanges=(("Wall9","Go")))
重定向指定Struct类型
[CoreRedirects] ;BaseMagicLeapScreens.ini ;将MagicLeapScreens模块中的FScreensWatchHistoryEntry结构体重定向到MagicLeapScreens模块中的MagicLeapScreensWatchHistoryEntry结构体 +StructRedirects=(OldName="/Script/MagicLeapScreens.ScreensWatchHistoryEntry", NewName="/Script/MagicLeapScreens.MagicLeapScreensWatchHistoryEntry") ;与上面做法等价 [/Script/Engine.Engine] +ActiveStructRedirects=(OldStructName="/Script/MagicLeapScreens.ScreensWatchHistoryEntry",NewStructName="/Script/MagicLeapScreens.MagicLeapScreensWatchHistoryEntry")
重定向指定Enum类型及指定Enum值
[CoreRedirects] ;BaseMagicLeapHandTracking.ini ;将MagicLeapGestures模块中的EStaticGestures枚举重定向到MagicLeapHandTracking模块中的EHandTrackingGesture枚举 +EnumRedirects=(OldName="/Script/MagicLeapGestures.EStaticGestures",NewName="/Script/MagicLeapHandTracking.EHandTrackingGesture") ;将EStereoLayerType枚举中的EStereoLayerType::SLT_TorsoLocked修改为EStereoLayerType::SLT_TrackerLocked +EnumRedirects=(OldName="EStereoLayerType",ValueChanges=(("EStereoLayerType::SLT_TorsoLocked","EStereoLayerType::SLT_TrackerLocked")) ) ;将所有模块中的ESoundDistanceModel枚举重定向到Engine模块中的EAttenuationDistanceModel枚举 ;并将ATTENUATION_Custom修改为EAttenuationDistanceModel::Custom,将ATTENUATION_Inverse修改为EAttenuationDistanceModel::Inverse +EnumRedirects=(OldName="ESoundDistanceModel",NewName="/Script/Engine.EAttenuationDistanceModel",ValueChanges=(("ATTENUATION_Custom","EAttenuationDistanceModel::Custom"),("ATTENUATION_Inverse","EAttenuationDistanceModel::Inverse")) )
重定向指定Function
[CoreRedirects] ;将ConvertTransformToRelative函数重定向到Engine模块KismetMathLibrary类中MakeRelativeTransform函数 +FunctionRedirects=(OldName="ConvertTransformToRelative",NewName="/Script/Engine.KismetMathLibrary.MakeRelativeTransform") ;将Actor类中SetActorRotation函数重定向到Actor类中K2_SetActorRotation函数 +FunctionRedirects=(OldName="Actor.SetActorRotation",NewName="Actor.K2_SetActorRotation")
重定向指定Property
[CoreRedirects] ;将GameplayEffect类中InheritableClearTagsContainer属性重定向到GameplayEffect类中RemoveGameplayEffectsWithTags属性 +PropertyRedirects=(OldName="GameplayEffect.InheritableClearTagsContainer",NewName="GameplayEffect.RemoveGameplayEffectsWithTags")
** 类型重定向示例 **
老方案:在DefaultEngine中的/Script/Engine.Engine标签中,将MyBPObject类型重定向到MyBPCObject上
[/Script/Engine.Engine] +ActiveClassRedirects=(OldClassName="MyBPObject",NewClassName="MyBPCObject")
新方案:在DefaultEngine中的CoreRedirects标签中,将MyBPObject类型重定向到MyBPCObject上
[CoreRedirects] +ClassRedirects=(OldName="MyBPObject",NewName="MyBPCObject")
资源路径重定向
在项目的DefaultEngine.ini中配置
[CoreRedirects] ;将蓝图类型/Game/Blueprints/Struct_AIVoiceTableRelated重定向到/Game/InBattle/Blueprints/Struct_AIVoiceTableRelated +PackageRedirects=(OldName="/Game/Blueprints/Struct_AIVoiceTableRelated", NewName="/Game/InBattle/Blueprints/Struct_AIVoiceTableRelated") ;将材质/Game/Game/Materials/M_MRCamSrcProcessing重定向到/Game/InBattle/Materials/M_MrcVideoProcessing +PackageRedirects=(OldName="/Game/Materials/M_MRCamSrcProcessing", NewName="/Game/InBattle/Materials/M_MrcVideoProcessing")
在引擎的BaseEngine.ini中配置
[CoreRedirects] ;将对资源/Engine/EngineAnimNotifies/AnimNotify_PlaySound的引用清空 +PackageRedirects=(OldName="/Engine/EngineAnimNotifies/AnimNotify_PlaySound",Removed=True)
具体步骤:
1. 删除/Engine/EngineAnimNotifies/AnimNotify_PlaySound文件
2. 在CoreRedirects标签中配置该文件要被删除 注:如果不配置的话,会在Message Log窗口的Load Errors分类中提示 Failed to load /Engine/EngineAnimNotifies/AnimNotify_PlaySound Referenced by XXX
在Base[Plugin名].ini中配置资源重定向
[CoreRedirects] ;BaseMDLImporter.ini ;将/DatasmithContent/Materials/MDL/目录中的所有资源重定向到/MDLImporter/Materials/MDL/目录中 +PackageRedirects=(OldName="/DatasmithContent/Materials/MDL/", NewName="/MDLImporter/Materials/MDL/", MatchSubstring=true) ;-------------------------------------------------------------------------------------------------------- [CoreRedirects] ;BaseMixedRealityCaptureFramework.ini ;将名为MixedRealityFramework的Plugin重定向到名为MixedRealityCaptureFramework上 +PackageRedirects=(OldName="/MixedRealityFramework/", NewName="/MixedRealityCaptureFramework/", MatchSubstring=true) ;与上面做法等价 [/Script/Engine.Engine] +ActivePluginRedirects=(OldPluginName="MixedRealityFramework",NewPluginName="MixedRealityCaptureFramework")
** 资源路径重定向示例 **
在windows资源管理器中强制把Meshes文件夹改名为Meshes2
场景中Bump_StaticMesh、LeftArm_StaticMesh、Linear_Stair_StaticMesh、Ramp_StaticMesh和RightArm_StaticMesh模型丢失
静态模型Bump_StaticMesh、LeftArm_StaticMesh、Linear_Stair_StaticMesh、Ramp_StaticMesh和RightArm_StaticMesh关联的RampMaterial材质丢失
在DefaultEngine中的CoreRedirects标签中,将资源路径/Game/ThirdPerson/Meshes/重定向到/Game/ThirdPerson/Meshes2/上
[CoreRedirects] +PackageRedirects=(OldName="/Game/ThirdPerson/Meshes/", NewName="/Game/ThirdPerson/Meshes2/", MatchSubstring=true)
配置后,启动一切都正常
去除依赖
配置了重定向后,可通过以下2种方法来去除对重定向的依赖
方法①:在编辑器中,对这些蓝图或其他美术资源文件进行重新保存
方法②:使用commandlet为ResavePackages的命令来保存对应的文件
UE4Editor.exe "%GameDir%\MyGame.uproject" -run=ResavePackages -projectonly // 重新保存项目中的所有文件
UE4Editor.exe "%GameDir%\MyGame.uproject" -run=ResavePackages -packagefolder="G:/svn/MyTest1/Content/ThirdPersonCPP/Meshes" // 重新保存项目G:/svn/MyTest1/Content/ThirdPersonCPP/Meshes目录中的所有文件
UE4Editor.exe "%GameDir%\MyGame.uproject" -run=ResavePackages -package=/Game/ThirdPersonCPP/Meshes/Linear_Stair_StaticMesh // 重新保存项目中的/Game/ThirdPersonCPP/Meshes/Linear_Stair_StaticMesh文件
UE4Editor.exe "%GameDir%\MyGame.uproject" -run=ResavePackages -file="G:/svn/MyTest1/fixfilelist.txt" // 重新保存项目中G:/svn/MyTest1/fixfilelist.txt列表中包含的所有文件
fixfilelist.txt中内容如下:
G:/svn/MyTest1/Content/ThirdPersonCPP/Meshes/Bump_StaticMesh.uasset
G:/svn/MyTest1/Content/ThirdPersonCPP/Meshes/LeftArm_StaticMesh.uasset
扩展阅读