接入Spine
This commit is contained in:
		| @ -0,0 +1,143 @@ | ||||
| /****************************************************************************** | ||||
|  * Spine Runtimes License Agreement | ||||
|  * Last updated April 5, 2025. Replaces all prior versions. | ||||
|  * | ||||
|  * Copyright (c) 2013-2025, Esoteric Software LLC | ||||
|  * | ||||
|  * Integration of the Spine Runtimes into software or otherwise creating | ||||
|  * derivative works of the Spine Runtimes is permitted under the terms and | ||||
|  * conditions of Section 2 of the Spine Editor License Agreement: | ||||
|  * http://esotericsoftware.com/spine-editor-license | ||||
|  * | ||||
|  * Otherwise, it is permitted to integrate the Spine Runtimes into software | ||||
|  * or otherwise create derivative works of the Spine Runtimes (collectively, | ||||
|  * "Products"), provided that each user of the Products must obtain their own | ||||
|  * Spine Editor license and redistribution of the Products in any form must | ||||
|  * include this license and copyright notice. | ||||
|  * | ||||
|  * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY | ||||
|  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||||
|  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||
|  * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY | ||||
|  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||||
|  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, | ||||
|  * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND | ||||
|  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
|  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||||
|  * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  *****************************************************************************/ | ||||
|  | ||||
| #include "SpineAtlasImportFactory.h" | ||||
| #include "AssetToolsModule.h" | ||||
| #include "SpineAtlasAsset.h" | ||||
| #include "Editor.h" | ||||
|  | ||||
| #define LOCTEXT_NAMESPACE "Spine" | ||||
|  | ||||
| using namespace spine; | ||||
|  | ||||
| USpineAtlasAssetFactory::USpineAtlasAssetFactory(const FObjectInitializer &objectInitializer) : Super(objectInitializer) { | ||||
| 	bCreateNew = false; | ||||
| 	bEditAfterNew = true; | ||||
| 	bEditorImport = true; | ||||
| 	SupportedClass = USpineAtlasAsset::StaticClass(); | ||||
|  | ||||
| 	Formats.Add(TEXT("atlas;Spine Atlas file")); | ||||
| } | ||||
|  | ||||
| FText USpineAtlasAssetFactory::GetToolTip() const { | ||||
| 	return LOCTEXT("SpineAtlasAssetFactory", "Animations exported from Spine"); | ||||
| } | ||||
|  | ||||
| bool USpineAtlasAssetFactory::FactoryCanImport(const FString &Filename) { | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| UObject *USpineAtlasAssetFactory::FactoryCreateFile(UClass *InClass, UObject *InParent, FName InName, EObjectFlags Flags, const FString &Filename, const TCHAR *Parms, FFeedbackContext *Warn, bool &bOutOperationCanceled) { | ||||
| 	FString FileExtension = FPaths::GetExtension(Filename); | ||||
| 	GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPreImport(this, InClass, InParent, InName, *FileExtension); | ||||
|  | ||||
| 	FString rawString; | ||||
| 	if (!FFileHelper::LoadFileToString(rawString, *Filename)) { | ||||
| 		return nullptr; | ||||
| 	} | ||||
|  | ||||
| 	FString currentSourcePath, filenameNoExtension, unusedExtension; | ||||
| 	const FString longPackagePath = FPackageName::GetLongPackagePath(InParent->GetOutermost()->GetPathName()); | ||||
| 	FPaths::Split(UFactory::GetCurrentFilename(), currentSourcePath, filenameNoExtension, unusedExtension); | ||||
|  | ||||
| 	USpineAtlasAsset *asset = NewObject<USpineAtlasAsset>(InParent, InClass, InName, Flags); | ||||
| 	asset->SetRawData(rawString); | ||||
| 	asset->SetAtlasFileName(FName(*Filename)); | ||||
| 	LoadAtlas(asset, currentSourcePath, longPackagePath); | ||||
| 	GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPostImport(this, asset); | ||||
| 	return asset; | ||||
| } | ||||
|  | ||||
| bool USpineAtlasAssetFactory::CanReimport(UObject *Obj, TArray<FString> &OutFilenames) { | ||||
| 	USpineAtlasAsset *asset = Cast<USpineAtlasAsset>(Obj); | ||||
| 	if (!asset) return false; | ||||
|  | ||||
| 	FString filename = asset->GetAtlasFileName().ToString(); | ||||
| 	if (!filename.IsEmpty()) | ||||
| 		OutFilenames.Add(filename); | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| void USpineAtlasAssetFactory::SetReimportPaths(UObject *Obj, const TArray<FString> &NewReimportPaths) { | ||||
| 	USpineAtlasAsset *asset = Cast<USpineAtlasAsset>(Obj); | ||||
|  | ||||
| 	if (asset && ensure(NewReimportPaths.Num() == 1)) | ||||
| 		asset->SetAtlasFileName(FName(*NewReimportPaths[0])); | ||||
| } | ||||
|  | ||||
| EReimportResult::Type USpineAtlasAssetFactory::Reimport(UObject *Obj) { | ||||
| 	USpineAtlasAsset *asset = Cast<USpineAtlasAsset>(Obj); | ||||
| 	FString rawString; | ||||
| 	if (!FFileHelper::LoadFileToString(rawString, *asset->GetAtlasFileName().ToString())) return EReimportResult::Failed; | ||||
| 	asset->SetRawData(rawString); | ||||
|  | ||||
| 	FString currentSourcePath, filenameNoExtension, unusedExtension; | ||||
| 	const FString longPackagePath = FPackageName::GetLongPackagePath(asset->GetOutermost()->GetPathName()); | ||||
| 	FString currentFileName = asset->GetAtlasFileName().ToString(); | ||||
| 	FPaths::Split(currentFileName, currentSourcePath, filenameNoExtension, unusedExtension); | ||||
|  | ||||
| 	LoadAtlas(asset, currentSourcePath, longPackagePath); | ||||
|  | ||||
| 	if (Obj->GetOuter()) Obj->GetOuter()->MarkPackageDirty(); | ||||
| 	else | ||||
| 		Obj->MarkPackageDirty(); | ||||
|  | ||||
| 	GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetReimport(asset); | ||||
| 	return EReimportResult::Succeeded; | ||||
| } | ||||
|  | ||||
| UTexture2D *resolveTexture(USpineAtlasAsset *Asset, const FString &PageFileName, const FString &TargetSubPath) { | ||||
| 	FAssetToolsModule &AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools"); | ||||
|  | ||||
| 	TArray<FString> fileNames; | ||||
| 	fileNames.Add(PageFileName); | ||||
|  | ||||
| 	TArray<UObject *> importedAsset = AssetToolsModule.Get().ImportAssets(fileNames, TargetSubPath); | ||||
| 	UTexture2D *texture = (importedAsset.Num() > 0) ? Cast<UTexture2D>(importedAsset[0]) : nullptr; | ||||
|  | ||||
| 	return texture; | ||||
| } | ||||
|  | ||||
| void USpineAtlasAssetFactory::LoadAtlas(USpineAtlasAsset *Asset, const FString &CurrentSourcePath, const FString &LongPackagePath) { | ||||
| 	Atlas *atlas = Asset->GetAtlas(); | ||||
| 	Asset->atlasPages.Empty(); | ||||
|  | ||||
| 	const FString targetTexturePath = LongPackagePath / TEXT("Textures"); | ||||
|  | ||||
| 	Vector<AtlasPage *> &pages = atlas->getPages(); | ||||
| 	for (size_t i = 0, n = pages.size(); i < n; i++) { | ||||
| 		AtlasPage *page = pages[i]; | ||||
| 		const FString sourceTextureFilename = FPaths::Combine(*CurrentSourcePath, UTF8_TO_TCHAR(page->name.buffer())); | ||||
| 		UTexture2D *texture = resolveTexture(Asset, sourceTextureFilename, targetTexturePath); | ||||
| 		Asset->atlasPages.Add(texture); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| #undef LOCTEXT_NAMESPACE | ||||
| @ -0,0 +1,71 @@ | ||||
| /****************************************************************************** | ||||
|  * Spine Runtimes License Agreement | ||||
|  * Last updated April 5, 2025. Replaces all prior versions. | ||||
|  * | ||||
|  * Copyright (c) 2013-2025, Esoteric Software LLC | ||||
|  * | ||||
|  * Integration of the Spine Runtimes into software or otherwise creating | ||||
|  * derivative works of the Spine Runtimes is permitted under the terms and | ||||
|  * conditions of Section 2 of the Spine Editor License Agreement: | ||||
|  * http://esotericsoftware.com/spine-editor-license | ||||
|  * | ||||
|  * Otherwise, it is permitted to integrate the Spine Runtimes into software | ||||
|  * or otherwise create derivative works of the Spine Runtimes (collectively, | ||||
|  * "Products"), provided that each user of the Products must obtain their own | ||||
|  * Spine Editor license and redistribution of the Products in any form must | ||||
|  * include this license and copyright notice. | ||||
|  * | ||||
|  * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY | ||||
|  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||||
|  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||
|  * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY | ||||
|  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||||
|  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, | ||||
|  * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND | ||||
|  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
|  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||||
|  * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  *****************************************************************************/ | ||||
|  | ||||
| #include "SpineEditorPlugin.h" | ||||
| #include "AssetTypeActions_Base.h" | ||||
| #include "SpineAtlasAsset.h" | ||||
| #include "SpineSkeletonDataAsset.h" | ||||
|  | ||||
| class FSpineAtlasAssetTypeActions : public FAssetTypeActions_Base { | ||||
| public: | ||||
| 	UClass *GetSupportedClass() const override { return USpineAtlasAsset::StaticClass(); }; | ||||
| 	FText GetName() const override { return INVTEXT("Spine atlas asset"); }; | ||||
| 	FColor GetTypeColor() const override { return FColor::Red; }; | ||||
| 	uint32 GetCategories() override { return EAssetTypeCategories::Misc; }; | ||||
| }; | ||||
|  | ||||
| class FSpineSkeletonDataAssetTypeActions : public FAssetTypeActions_Base { | ||||
| public: | ||||
| 	UClass *GetSupportedClass() const override { return USpineSkeletonDataAsset::StaticClass(); }; | ||||
| 	FText GetName() const override { return INVTEXT("Spine data asset"); }; | ||||
| 	FColor GetTypeColor() const override { return FColor::Red; }; | ||||
| 	uint32 GetCategories() override { return EAssetTypeCategories::Misc; }; | ||||
| }; | ||||
|  | ||||
| class FSpineEditorPlugin : public ISpineEditorPlugin { | ||||
| 	virtual void StartupModule() override; | ||||
| 	virtual void ShutdownModule() override; | ||||
| 	TSharedPtr<FSpineAtlasAssetTypeActions> SpineAtlasAssetTypeActions; | ||||
| 	TSharedPtr<FSpineSkeletonDataAssetTypeActions> SpineSkeletonDataAssetTypeActions; | ||||
| }; | ||||
|  | ||||
| IMPLEMENT_MODULE(FSpineEditorPlugin, SpineEditorPlugin) | ||||
|  | ||||
| void FSpineEditorPlugin::StartupModule() { | ||||
| 	SpineAtlasAssetTypeActions = MakeShared<FSpineAtlasAssetTypeActions>(); | ||||
| 	FAssetToolsModule::GetModule().Get().RegisterAssetTypeActions(SpineAtlasAssetTypeActions.ToSharedRef()); | ||||
| 	SpineSkeletonDataAssetTypeActions = MakeShared<FSpineSkeletonDataAssetTypeActions>(); | ||||
| 	FAssetToolsModule::GetModule().Get().RegisterAssetTypeActions(SpineSkeletonDataAssetTypeActions.ToSharedRef()); | ||||
| } | ||||
|  | ||||
| void FSpineEditorPlugin::ShutdownModule() { | ||||
| 	if (!FModuleManager::Get().IsModuleLoaded("AssetTools")) return; | ||||
| 	FAssetToolsModule::GetModule().Get().UnregisterAssetTypeActions(SpineAtlasAssetTypeActions.ToSharedRef()); | ||||
| 	FAssetToolsModule::GetModule().Get().UnregisterAssetTypeActions(SpineSkeletonDataAssetTypeActions.ToSharedRef()); | ||||
| } | ||||
| @ -0,0 +1,126 @@ | ||||
| /****************************************************************************** | ||||
|  * Spine Runtimes License Agreement | ||||
|  * Last updated April 5, 2025. Replaces all prior versions. | ||||
|  * | ||||
|  * Copyright (c) 2013-2025, Esoteric Software LLC | ||||
|  * | ||||
|  * Integration of the Spine Runtimes into software or otherwise creating | ||||
|  * derivative works of the Spine Runtimes is permitted under the terms and | ||||
|  * conditions of Section 2 of the Spine Editor License Agreement: | ||||
|  * http://esotericsoftware.com/spine-editor-license | ||||
|  * | ||||
|  * Otherwise, it is permitted to integrate the Spine Runtimes into software | ||||
|  * or otherwise create derivative works of the Spine Runtimes (collectively, | ||||
|  * "Products"), provided that each user of the Products must obtain their own | ||||
|  * Spine Editor license and redistribution of the Products in any form must | ||||
|  * include this license and copyright notice. | ||||
|  * | ||||
|  * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY | ||||
|  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||||
|  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||
|  * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY | ||||
|  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||||
|  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, | ||||
|  * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND | ||||
|  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
|  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||||
|  * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  *****************************************************************************/ | ||||
|  | ||||
| #include "SpineSkeletonImportFactory.h" | ||||
| #include "AssetToolsModule.h" | ||||
| #include "Developer/AssetTools/Public/IAssetTools.h" | ||||
| #include "SpineSkeletonDataAsset.h" | ||||
| #include <string.h> | ||||
|  | ||||
| #define LOCTEXT_NAMESPACE "Spine" | ||||
|  | ||||
| USpineSkeletonAssetFactory::USpineSkeletonAssetFactory(const FObjectInitializer &objectInitializer) : Super(objectInitializer) { | ||||
| 	bCreateNew = false; | ||||
| 	bEditAfterNew = true; | ||||
| 	bEditorImport = true; | ||||
| 	SupportedClass = USpineSkeletonDataAsset::StaticClass(); | ||||
|  | ||||
| 	Formats.Add(TEXT("json;Spine skeleton file")); | ||||
| 	Formats.Add(TEXT("skel;Spine skeleton file")); | ||||
| } | ||||
|  | ||||
| FText USpineSkeletonAssetFactory::GetToolTip() const { | ||||
| 	return LOCTEXT("USpineSkeletonAssetFactory", "Animations exported from Spine"); | ||||
| } | ||||
|  | ||||
| bool USpineSkeletonAssetFactory::FactoryCanImport(const FString &Filename) { | ||||
| 	if (Filename.Contains(TEXT(".skel"))) return true; | ||||
|  | ||||
| 	if (Filename.Contains(TEXT(".json"))) { | ||||
| 		TArray<uint8> rawData; | ||||
| 		if (!FFileHelper::LoadFileToArray(rawData, *Filename, 0)) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		if (rawData.Num() == 0) return false; | ||||
| 		return strcmp((const char *) rawData.GetData(), "skeleton") > 0 && strcmp((const char *) rawData.GetData(), "spine") > 0; | ||||
| 	} | ||||
|  | ||||
| 	return false; | ||||
| } | ||||
|  | ||||
| void LoadAtlas(const FString &Filename, const FString &TargetPath) { | ||||
| 	FAssetToolsModule &AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools"); | ||||
|  | ||||
| 	FString skelFile = Filename.Replace(TEXT(".skel"), TEXT(".atlas")).Replace(TEXT(".json"), TEXT(".atlas")); | ||||
| 	if (!FPaths::FileExists(skelFile)) return; | ||||
|  | ||||
| 	TArray<FString> fileNames; | ||||
| 	fileNames.Add(skelFile); | ||||
| 	AssetToolsModule.Get().ImportAssets(fileNames, TargetPath); | ||||
| } | ||||
|  | ||||
| UObject *USpineSkeletonAssetFactory::FactoryCreateFile(UClass *InClass, UObject *InParent, FName InName, EObjectFlags Flags, const FString &Filename, const TCHAR *Parms, FFeedbackContext *Warn, bool &bOutOperationCanceled) { | ||||
| 	USpineSkeletonDataAsset *asset = NewObject<USpineSkeletonDataAsset>(InParent, InClass, InName, Flags); | ||||
| 	TArray<uint8> rawData; | ||||
| 	if (!FFileHelper::LoadFileToArray(rawData, *Filename, 0)) { | ||||
| 		return nullptr; | ||||
| 	} | ||||
| 	asset->SetSkeletonDataFileName(FName(*Filename)); | ||||
| 	asset->SetRawData(rawData); | ||||
|  | ||||
| 	const FString longPackagePath = FPackageName::GetLongPackagePath(asset->GetOutermost()->GetPathName()); | ||||
| 	LoadAtlas(Filename, longPackagePath); | ||||
| 	return asset; | ||||
| } | ||||
|  | ||||
| bool USpineSkeletonAssetFactory::CanReimport(UObject *Obj, TArray<FString> &OutFilenames) { | ||||
| 	USpineSkeletonDataAsset *asset = Cast<USpineSkeletonDataAsset>(Obj); | ||||
| 	if (!asset) return false; | ||||
|  | ||||
| 	FString filename = asset->GetSkeletonDataFileName().ToString(); | ||||
| 	if (!filename.IsEmpty()) | ||||
| 		OutFilenames.Add(filename); | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| void USpineSkeletonAssetFactory::SetReimportPaths(UObject *Obj, const TArray<FString> &NewReimportPaths) { | ||||
| 	USpineSkeletonDataAsset *asset = Cast<USpineSkeletonDataAsset>(Obj); | ||||
|  | ||||
| 	if (asset && ensure(NewReimportPaths.Num() == 1)) | ||||
| 		asset->SetSkeletonDataFileName(FName(*NewReimportPaths[0])); | ||||
| } | ||||
|  | ||||
| EReimportResult::Type USpineSkeletonAssetFactory::Reimport(UObject *Obj) { | ||||
| 	USpineSkeletonDataAsset *asset = Cast<USpineSkeletonDataAsset>(Obj); | ||||
| 	TArray<uint8> rawData; | ||||
| 	if (!FFileHelper::LoadFileToArray(rawData, *asset->GetSkeletonDataFileName().ToString(), 0)) return EReimportResult::Failed; | ||||
| 	asset->SetRawData(rawData); | ||||
|  | ||||
| 	const FString longPackagePath = FPackageName::GetLongPackagePath(asset->GetOutermost()->GetPathName()); | ||||
| 	LoadAtlas(*asset->GetSkeletonDataFileName().ToString(), longPackagePath); | ||||
|  | ||||
| 	if (Obj->GetOuter()) Obj->GetOuter()->MarkPackageDirty(); | ||||
| 	else | ||||
| 		Obj->MarkPackageDirty(); | ||||
|  | ||||
| 	return EReimportResult::Succeeded; | ||||
| } | ||||
|  | ||||
| #undef LOCTEXT_NAMESPACE | ||||
		Reference in New Issue
	
	Block a user