ファイル一覧を取得して配列に格納するブループリントの作成 #UE4 #CPP

UE4 CPP ファイル一覧を取得して配列に格納する

C++クラスの追加

image

image

image

Blueprintで呼べるHelloWorld関数を作成する

DirectoryInfo.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "DirectoryInfo.generated.h"

/**
 * 
 */
UCLASS()
class CPPSAMPLE_API UDirectoryInfo : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
public :
    UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
    static FString HelloWorld();

};

DirectoryInfo.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "CPPSample.h"
#include "DirectoryInfo.h"

FString UDirectoryInfo::HelloWorld()
{
    return FString("Hello World!!");
}


image

フルパスを指定して、ファイル名の配列を返す関数を作成する

DirectoryInfo.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "DirectoryInfo.generated.h"

/**
 * 
 */
UCLASS()
class CPPSAMPLE_API UDirectoryInfo : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
public :
    UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
    static FString HelloWorld();

    UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
    static TArray<FString> GetAllFilesInDirectory(const FString directory, const bool fullPath, const bool withoutExtention, const FString onlyFilesStartingWith, const FString onlyFilesWithExtension);

};


DirectoryInfo.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "CPPSample.h"
#include "DirectoryInfo.h"

FString UDirectoryInfo::HelloWorld()
{
    return FString("Hello World!!");
}

TArray<FString> UDirectoryInfo::GetAllFilesInDirectory(const FString directory, const bool fullPath, const bool withoutExtention, const FString onlyFilesStartingWith, const FString onlyFilesWithExtension)
{
    // Get all files in directory
    TArray<FString> directoriesToSkip;
    IPlatformFile &PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
    FLocalTimestampDirectoryVisitor Visitor(PlatformFile, directoriesToSkip, directoriesToSkip, false);
    PlatformFile.IterateDirectory(*directory, Visitor);
    TArray<FString> files;

    for (TMap<FString, FDateTime>::TIterator TimestampIt(Visitor.FileTimes); TimestampIt; ++TimestampIt)
    {
        const FString filePath = TimestampIt.Key();
        const FString fileName = FPaths::GetCleanFilename(filePath);
        bool shouldAddFile = true;

        // Check if filename starts with required characters
        if (!onlyFilesStartingWith.IsEmpty())
        {
            const FString left = fileName.Left(onlyFilesStartingWith.Len());

            if (!(fileName.Left(onlyFilesStartingWith.Len()).Equals(onlyFilesStartingWith)))
                shouldAddFile = false;
        }

        // Check if file extension is required characters
        if (!onlyFilesWithExtension.IsEmpty())
        {
            if (!(FPaths::GetExtension(fileName, false).Equals(onlyFilesWithExtension, ESearchCase::IgnoreCase)))
                shouldAddFile = false;
        }


        // Add full path to results
        if (shouldAddFile)
        {
            if(withoutExtention)
                files.Add(fullPath ? filePath : FPaths::GetBaseFilename(filePath));
            else
                files.Add(fullPath ? filePath : FPaths::GetCleanFilename(filePath));
        }
    }

    return files;
}

参照

https://docs.unrealengine.com/latest/JPN/Programming/UnrealArchitecture/TArrays/index.html

https://answers.unrealengine.com/questions/212791/how-to-get-file-list-in-a-directory.html

http://stackoverflow.com/questions/29800808/how-to-get-filename-without-extension-in-unreal

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.