| @ -0,0 +1,49 @@ | ||||
| #include "CSAsyncActionBase.h" | ||||
| #include "UnrealSharpAsync.h" | ||||
|  | ||||
| void UCSAsyncActionBase::Destroy() | ||||
| { | ||||
| 	if (UGameInstance* GameInstance = GetWorld()->GetGameInstance()) | ||||
| 	{ | ||||
| 		GameInstance->UnregisterReferencedObject(this); | ||||
| 	} | ||||
|  | ||||
| 	ManagedCallback.Dispose(); | ||||
| 	MarkAsGarbage(); | ||||
| } | ||||
|  | ||||
| void UCSAsyncActionBase::InvokeManagedCallback(bool bDispose) | ||||
| { | ||||
| 	InvokeManagedCallback(this, bDispose); | ||||
| } | ||||
|  | ||||
| void UCSAsyncActionBase::InvokeManagedCallback(UObject* WorldContextObject, bool bDispose) | ||||
| { | ||||
|     ManagedCallback.Invoke(WorldContextObject, bDispose); | ||||
|  | ||||
|     if (bDispose) | ||||
|     { | ||||
|         Destroy(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void UCSAsyncActionBase::InitializeManagedCallback(FGCHandleIntPtr Callback) | ||||
| { | ||||
| 	ManagedCallback = FGCHandle(Callback, GCHandleType::WeakHandle); | ||||
|  | ||||
| 	if (UGameInstance* GameInstance = GetWorld()->GetGameInstance()) | ||||
| 	{ | ||||
| 		GameInstance->RegisterReferencedObject(this); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void UUCSAsyncBaseExporter::InitializeAsyncObject(UCSAsyncActionBase* AsyncAction, FGCHandleIntPtr Callback) | ||||
| { | ||||
| 	if (!IsValid(AsyncAction)) | ||||
| 	{ | ||||
| 		UE_LOG(LogUnrealSharpAsync, Warning, TEXT("UUCSAsyncBaseExporter::InitializeAsyncObject: AsyncAction is null")); | ||||
| 		return; | ||||
| 	} | ||||
| 	 | ||||
| 	AsyncAction->InitializeManagedCallback(Callback); | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| #include "CSAsyncLoadPrimaryDataAssets.h" | ||||
| #include "Engine/AssetManager.h" | ||||
|  | ||||
| void UCSAsyncLoadPrimaryDataAssets::LoadPrimaryDataAssets(const TArray<FPrimaryAssetId>& AssetIds, const TArray<FName>& AssetBundles) | ||||
| { | ||||
| 	UAssetManager& AssetManager = UAssetManager::Get(); | ||||
| 	AssetManager.LoadPrimaryAssets(AssetIds, AssetBundles, FStreamableDelegate::CreateUObject(this, &UCSAsyncLoadPrimaryDataAssets::OnPrimaryDataAssetsLoaded)); | ||||
| } | ||||
|  | ||||
| void UCSAsyncLoadPrimaryDataAssets::OnPrimaryDataAssetsLoaded() | ||||
| { | ||||
| 	InvokeManagedCallback(); | ||||
| } | ||||
| @ -0,0 +1,15 @@ | ||||
| #include "CSAsyncLoadSoftObjectPtr.h" | ||||
| #include "Engine/AssetManager.h" | ||||
| #include "Engine/StreamableManager.h" | ||||
|  | ||||
| void UCSAsyncLoadSoftPtr::LoadSoftObjectPaths(const TArray<FSoftObjectPath>& SoftObjectPtr) | ||||
| { | ||||
| 	UAssetManager::Get().GetStreamableManager().RequestAsyncLoad(SoftObjectPtr, | ||||
| 	FStreamableDelegate::CreateUObject(this, &UCSAsyncLoadSoftPtr::OnAsyncLoadComplete)); | ||||
| } | ||||
|  | ||||
| void UCSAsyncLoadSoftPtr::OnAsyncLoadComplete() | ||||
| { | ||||
| 	InvokeManagedCallback(); | ||||
| } | ||||
|  | ||||
| @ -0,0 +1,19 @@ | ||||
| #include "UnrealSharpAsync.h" | ||||
|  | ||||
| #define LOCTEXT_NAMESPACE "FUnrealSharpAsyncModule" | ||||
|  | ||||
| DEFINE_LOG_CATEGORY(LogUnrealSharpAsync); | ||||
|  | ||||
| void FUnrealSharpAsyncModule::StartupModule() | ||||
| { | ||||
|      | ||||
| } | ||||
|  | ||||
| void FUnrealSharpAsyncModule::ShutdownModule() | ||||
| { | ||||
|      | ||||
| } | ||||
|  | ||||
| #undef LOCTEXT_NAMESPACE | ||||
|      | ||||
| IMPLEMENT_MODULE(FUnrealSharpAsyncModule, UnrealSharpAsync) | ||||
| @ -0,0 +1,36 @@ | ||||
| // Fill out your copyright notice in the Description page of Project Settings. | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "CoreMinimal.h" | ||||
| #include "CSManagedDelegate.h" | ||||
| #include "CSManagedGCHandle.h" | ||||
| #include "UnrealSharpBinds/Public/CSBindsManager.h" | ||||
| #include "UObject/Object.h" | ||||
| #include "CSAsyncActionBase.generated.h" | ||||
|  | ||||
| UCLASS() | ||||
| class UNREALSHARPASYNC_API UCSAsyncActionBase : public UObject | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
| public: | ||||
| 	UFUNCTION(meta = (ScriptMethod)) | ||||
| 	void Destroy(); | ||||
| protected: | ||||
| 	friend class UUCSAsyncBaseExporter; | ||||
|  | ||||
| 	void InvokeManagedCallback(bool bDispose = true); | ||||
|     void InvokeManagedCallback(UObject* WorldContextObject, bool bDispose = true); | ||||
| 	void InitializeManagedCallback(FGCHandleIntPtr Callback); | ||||
| 	 | ||||
| 	FCSManagedDelegate ManagedCallback; | ||||
| }; | ||||
|  | ||||
| UCLASS(meta = (InternalType)) | ||||
| class UUCSAsyncBaseExporter : public UObject | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
| public: | ||||
| 	UNREALSHARP_FUNCTION() | ||||
| 	static void InitializeAsyncObject(UCSAsyncActionBase* AsyncAction, FGCHandleIntPtr Callback); | ||||
| }; | ||||
| @ -0,0 +1,18 @@ | ||||
| // Fill out your copyright notice in the Description page of Project Settings. | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "CoreMinimal.h" | ||||
| #include "CSAsyncActionBase.h" | ||||
| #include "CSAsyncLoadPrimaryDataAssets.generated.h" | ||||
|  | ||||
| UCLASS(meta = (InternalType)) | ||||
| class UCSAsyncLoadPrimaryDataAssets : public UCSAsyncActionBase | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
| public: | ||||
| 	UFUNCTION(meta =(ScriptMethod)) | ||||
| 	void LoadPrimaryDataAssets(const TArray<FPrimaryAssetId>& AssetIds, const TArray<FName>& AssetBundles); | ||||
| private: | ||||
| 	void OnPrimaryDataAssetsLoaded(); | ||||
| }; | ||||
| @ -0,0 +1,20 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "CoreMinimal.h" | ||||
| #include "CSAsyncActionBase.h" | ||||
| #include "CSAsyncLoadSoftObjectPtr.generated.h" | ||||
|  | ||||
| UCLASS(meta = (InternalType)) | ||||
| class UCSAsyncLoadSoftPtr : public UCSAsyncActionBase | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
| public: | ||||
| 	UFUNCTION(meta = (ScriptMethod)) | ||||
| 	void LoadSoftObjectPaths(const TArray<FSoftObjectPath>& SoftObjectPtr); | ||||
| protected: | ||||
| 	void OnAsyncLoadComplete(); | ||||
| }; | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| @ -0,0 +1,13 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "CoreMinimal.h" | ||||
| #include "Modules/ModuleManager.h" | ||||
|  | ||||
| DECLARE_LOG_CATEGORY_EXTERN(LogUnrealSharpAsync, Log, All); | ||||
|  | ||||
| class FUnrealSharpAsyncModule : public IModuleInterface | ||||
| { | ||||
| public: | ||||
|     virtual void StartupModule() override; | ||||
|     virtual void ShutdownModule() override; | ||||
| }; | ||||
| @ -0,0 +1,30 @@ | ||||
| using UnrealBuildTool; | ||||
|  | ||||
| public class UnrealSharpAsync : ModuleRules | ||||
| { | ||||
|     public UnrealSharpAsync(ReadOnlyTargetRules Target) : base(Target) | ||||
|     { | ||||
|         PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; | ||||
|  | ||||
|         PublicDependencyModuleNames.AddRange( | ||||
|             new string[] | ||||
|             { | ||||
|                 "Core",  | ||||
|             } | ||||
|         ); | ||||
|  | ||||
|         PrivateDependencyModuleNames.AddRange( | ||||
|             new string[] | ||||
|             { | ||||
|                 "CoreUObject", | ||||
|                 "Engine", | ||||
|                 "Slate", | ||||
|                 "SlateCore", | ||||
|                 "UnrealSharpBinds", | ||||
|                 "UnrealSharpCore" | ||||
|             } | ||||
|         ); | ||||
|          | ||||
|         PublicDefinitions.Add("ForceAsEngineGlue=1"); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user