Bu konuda bildiğimiz oyun ayarları için yardımcı olabilecek bir C++ kütüphanesini sizinle paylaşıyorum.
Kodu, kod projenize ekleyip derledikten sonra Blueprint üzerinden kullanılabilirsiniz.
Helpers.h
Helpers.cpp
Ekleme: Kütüphanenin UMG Widget üzerinde kullanımı için hızlı bir örnek hazırladım.
Desteklenen ekran çözünürlüklerinin ComboBox'a yerleştirilmesi.
Oyun içi ekran görüntüsü.
Kodu, kod projenize ekleyip derledikten sonra Blueprint üzerinden kullanılabilirsiniz.
Helpers.h
Kod:
#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Helpers.generated.h"
/**
*
*/
UCLASS()
class X_API UHelpers : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
/**
* Constructor.
*/
UHelpers();
/**
* Obtain all files in a provided directory, with optional extension filter.
* - All files are returned if Ext is left blank.
* - Returns only the file name(s) not full path to file(s).
*
* @param Out OutFiles An array to insert only files name(s) not full path to file(s).
* @param In InRootDirectory The root directory (full path) to look for.
* @param In InExtension The files extension(can be empty) to look for.
* @return True if operation succeeded.
*/
UFUNCTION(BlueprintPure, Category = IO) static bool GetFiles(TArray<FString>& OutFiles, FString InRootDirectory, FString InExtension);
/**
* Deletes all files in the given folder except the read only.
* - All files will be deleted if Ext is left blank.
* - Only the files with given extension will be deleted if Ext is not empty.
*
* @param In InRootDirectory The root directory (full path) to look for.
* @param In InExtension The files extension(can be empty) to look for.
* @return True if all files deleted successfuly.
*/
UFUNCTION(BlueprintPure, Category = IO) static bool DeleteAllFilesInFolder(FString InRootDirectory, FString InExtension);
/**
* Gets a reference to the GameUserSettings class.
*
* @return Reference to the GameUserSettings.
*/
UFUNCTION(BlueprintPure, Category = Settings) static UGameUserSettings* GetGameUserSettings();
/**
* Gets a list of screen resolutions supported on this machine.
*
* @param Resolutions The string array to write the supported resolutions.
* @return Whether the operation has been succeeded or not.
*/
UFUNCTION(BlueprintPure, Category = Settings) static bool GetSupportedScreenResolutions(TArray<FString>& Resolutions);
/**
* Sets the desired screen resolution.
* - Not changes it yet!
*
* @param Width Desired screen width.
* @param Height Desired screen height.
* @param FullScreen Desired mode.
* @return Whether the operation has been succeeded or not.
*/
UFUNCTION(BlueprintPure, Category = Settings) static bool SetScreenResolution(const int32 Width, const int32 Height, const bool FullScreen);
/**
* Changes the screen resolution.
*
* @param Width Desired screen width.
* @param Height Desired screen height.
* @param FullScreen Desired mode.
* @return Whether the operation has been succeeded or not.
*/
UFUNCTION(BlueprintPure, Category = Settings) static bool ChangeScreenResolution(const int32 Width, const int32 Height, const bool FullScreen);
/**
* Gets the current video quality settings.
*
* @param AntiAliasing The anti-aliasing.
* @param Effects The effects.
* @param PostProcess The post process.
* @param Resolution The screen resolution.
* @param Shadow The shadow quality.
* @param Texture The texture quality.
* @param ViewDistance The view distance.
* @return Whether the operation has been succeeded or not.
*/
UFUNCTION(BlueprintPure, Category = Settings) static bool GetVideoQualitySettings(int32& AntiAliasing, int32& Effects, int32& PostProcess,
int32& Resolution, int32& Shadow, int32& Texture, int32& ViewDistance);
/**
* Sets the current video quality settings.
* - Neither applied nor saved yet.
*
* @param AntiAliasing The anti-aliasing.
* @param Effects The effects.
* @param PostProcess The post process.
* @param Resolution The screen resolution.
* @param Shadow The shadow quality.
* @param Texture The texture quality.
* @param ViewDistance The view distance.
* @return Whether the operation has been succeeded or not.
*/
UFUNCTION(BlueprintPure, Category = Settings) static bool SetVideoQualitySettings(const int32 AntiAliasing, const int32 Effects, const int32 PostProcess,
const int32 Resolution, const int32 Shadow, const int32 Texture, const int32 ViewDistance);
/**
* Saves the changed settings.
*
* @return Whether the operation has been succeeded or not.
*/
UFUNCTION(BlueprintPure, Category = Settings) static bool SaveVideoModeAndQuality();
};
Helpers.cpp
Kod:
#include "X.h"
#include "Helpers.h"
UHelpers::UHelpers()
: Super()
{
}
bool UHelpers::GetFiles(TArray<FString>& OutFiles, FString InRootDirectory, FString InExtension)
{
if (InRootDirectory.Len() < 1)
{
return false;
}
FPaths::NormalizeDirectoryName(InRootDirectory);
IFileManager& FileManager = IFileManager::Get();
if (InExtension == "")
{
InExtension = "*.*";
}
else
{
InExtension = (InExtension.Left(1) == ".") ? "*" + InExtension : "*." + InExtension;
}
FString FinalPath = InRootDirectory + "/" + InExtension;
FileManager.FindFiles(OutFiles, *FinalPath, true, false);
return true;
}
bool UHelpers::DeleteAllFilesInFolder(FString InRootDirectory, FString InExtension)
{
bool Result = true;
TArray<FString> Files;
GetFiles(Files, InRootDirectory, InExtension);
for (size_t i = 0; i < Files.Num(); i++)
{
FString path = FPaths::Combine(*InRootDirectory, *Files[i]);
const bool result = FPlatformFileManager::Get().GetPlatformFile().DeleteFile(*path);
if (result == false) Result = false;
}
return Result;
}
UGameUserSettings* UHelpers::GetGameUserSettings()
{
if (GEngine != nullptr)
{
return GEngine->GameUserSettings;
}
return nullptr;
}
bool UHelpers::GetSupportedScreenResolutions(TArray<FString>& Resolutions)
{
FScreenResolutionArray ResolutionsArray;
if (RHIGetAvailableResolutions(ResolutionsArray, true)) // "RHI" dependency
{
for (const FScreenResolutionRHI& Resolution : ResolutionsArray)
{
FString StrW = FString::FromInt(Resolution.Width);
FString StrH = FString::FromInt(Resolution.Height);
Resolutions.AddUnique(StrW + "x" + StrH);
}
return true;
}
return false; // failed to obtain screen resolutions
}
bool UHelpers::SetScreenResolution(const int32 Width, const int32 Height, const bool Fullscreen)
{
UGameUserSettings* Settings = GetGameUserSettings();
if (!Settings)
{
return false;
}
Settings->SetScreenResolution(FIntPoint(Width, Height));
Settings->SetFullscreenMode(Fullscreen ? EWindowMode::Fullscreen : EWindowMode::Windowed);
return true;
}
bool UHelpers::ChangeScreenResolution(const int32 Width, const int32 Height, const bool Fullscreen)
{
UGameUserSettings* Settings = GetGameUserSettings();
if (!Settings)
{
return false;
}
EWindowMode::Type WindowMode = Fullscreen ? EWindowMode::Fullscreen : EWindowMode::Windowed;
Settings->RequestResolutionChange(Width, Height, WindowMode, false);
return true;
}
bool UHelpers::GetVideoQualitySettings(int32& AntiAliasing, int32& Effects, int32& PostProcess, int32& Resolution, int32& Shadow, int32& Texture, int32& ViewDistance)
{
UGameUserSettings* Settings = GetGameUserSettings();
if (!Settings)
{
return false;
}
AntiAliasing = Settings->ScalabilityQuality.AntiAliasingQuality;
Effects = Settings->ScalabilityQuality.EffectsQuality;
PostProcess = Settings->ScalabilityQuality.PostProcessQuality;
Resolution = Settings->ScalabilityQuality.ResolutionQuality;
Shadow = Settings->ScalabilityQuality.ShadowQuality;
Texture = Settings->ScalabilityQuality.TextureQuality;
ViewDistance = Settings->ScalabilityQuality.ViewDistanceQuality;
return true;
}
bool UHelpers::SetVideoQualitySettings(const int32 AntiAliasing, const int32 Effects, const int32 PostProcess, const int32 Resolution, const int32 Shadow, const int32 Texture, const int32 ViewDistance)
{
UGameUserSettings* Settings = GetGameUserSettings();
if (!Settings)
{
return false;
}
Settings->ScalabilityQuality.AntiAliasingQuality = AntiAliasing;
Settings->ScalabilityQuality.EffectsQuality = Effects;
Settings->ScalabilityQuality.PostProcessQuality = PostProcess;
Settings->ScalabilityQuality.ResolutionQuality = Resolution;
Settings->ScalabilityQuality.ShadowQuality = Shadow;
Settings->ScalabilityQuality.TextureQuality = Texture;
Settings->ScalabilityQuality.ViewDistanceQuality = ViewDistance;
return true;
}
bool UHelpers::SaveVideoModeAndQuality()
{
UGameUserSettings* Settings = GetGameUserSettings();
if (!Settings)
{
return false;
}
Settings->ConfirmVideoMode();
Settings->ApplyNonResolutionSettings();
Settings->SaveSettings();
return true;
}
Ekleme: Kütüphanenin UMG Widget üzerinde kullanımı için hızlı bir örnek hazırladım.
Desteklenen ekran çözünürlüklerinin ComboBox'a yerleştirilmesi.
Oyun içi ekran görüntüsü.