Adventure of 빠타박스
article thumbnail
728x90
728x90
SMALL

Unreal Engine 4 /4.26버전으로 수업을 진행합니다. 해당 내용은 수업에 대한 내용과 함께 

개인적인 생각 및 정보를 토대로 글을 씀을 알립니다. 

본 내용에 대하여 상업적으로 이용 및 배포를 금합니다.

이전내용) https://ppatabox.tistory.com/122

 

 

 

 


UEC++_04.pdf
3.67MB
UEC++_04.docx
4.17MB

 

 

 

 

Global.h

#pragma once
#include "Kismet/KismetMathLibrary.h"

#include "Kismet/KismetSystemLibrary.h"

#include "Utilities/CHelpers.h"

C02_Mesh.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "C02_Mesh.generated.h"



UCLASS()
class UE_STUDY01_API AC02_Mesh : public AActor
{
	GENERATED_BODY()

protected:
	UPROPERTY(VisibleDefaultsOnly)
		class UStaticMeshComponent* Mesh;

public:	
	 
	AC02_Mesh();

protected:
	 
	virtual void BeginPlay() override;
	
private:
	UFUNCTION()
		void SetRandomColor();

private:
	class UMaterialInstanceDynamic* Material;


};

 

C02_Mesh.cpp

#include "C02_Mesh.h"
#include "Components/StaticMeshComponent.h"
#include "Global.h"
#include "Materials/MaterialInstanceConstant.h"
#include "Materials/MaterialInstanceDynamic.h"


 AC02_Mesh::AC02_Mesh()
{

	// Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
	// RootComponent = Mesh;

	 CHelpers::CreateComponent<UStaticMeshComponent>(this, &Mesh, "Mesh");

	/* ConstructorHelpers::FObjectFinder<UStaticMesh> mesh(L"StaticMesh'/Game/Meshes/Cube.Cube'");
	 if(mesh.Succeeded())
		 Mesh->SetStaticMesh(mesh.Object);*/


	 UStaticMesh* mesh;
	 CHelpers::GetAsset(&mesh, "StaticMesh'/Game/Meshes/Cube.Cube'");
	 Mesh->SetStaticMesh(mesh);

}
 void AC02_Mesh::BeginPlay()
{
	Super::BeginPlay();

	FString path = "MaterialInstanceConstant'/Game/Materials/M_Color_White.M_Color_White'";
	//UMaterialInstanceConstant* material = Cast<UMaterialInstanceConstant>(StaticLoadObject(UMaterialInstanceConstant::StaticClass(), nullptr, *path));
	
	UMaterialInstanceConstant* material;
	CHelpers::GetAssetDynamic<UMaterialInstanceConstant>(&material, path);

	Material = UMaterialInstanceDynamic::Create(material, this);
	Mesh->SetMaterial(0, Material);

	UKismetSystemLibrary::K2_SetTimer(this, "SetRandomColor", 1, true);
 }

 void AC02_Mesh::SetRandomColor()
 {
	
	 Material->SetVectorParameterValue("Color", FLinearColor::MakeRandomColor());
	 Material->SetScalarParameterValue("Roughness", UKismetMathLibrary::RandomFloatInRange(0, 1));
 }

CHelpers.h

#pragma once

#include "CoreMinimal.h"
#include "Components/SceneComponent.h"

class UE_STUDY01_API CHelpers
{
public:
	template<typename T>
	static void CreateComponent(AActor* InActor, T** OutComponent, FName InName, USceneComponent* InParent = nullptr)
	{
		*OutComponent = InActor->CreateDefaultSubobject<T>(InName);
		
		if (!!InParent)
		{
			(*OutComponent)->SetupAttachment(InParent);

			return;
		}

		InActor->SetRootComponent(*OutComponent);
	}

	template<typename T>
	static void GetAsset(T**OutObject, FString InPath)
	{
		ConstructorHelpers::FObjectFinder<T> asset(*InPath);
		*OutObject = asset.Object;
	}

	template<typename T>
	static void GetAssetDynamic(T**OutObject, FString InPath)
	{
		*OutObject = Cast<T>(StaticLoadObject(T::StaticClass(), nullptr, *InPath));
	}
	
};

 

728x90
728x90
LIST
profile

Adventure of 빠타박스

@PPATABOX

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!