建立自己的客製化Asset
发表于2016-04-20
首先這個方法只針對繼承UObject的Class。
假設你的Class叫 MyObject
那你的MyObject.h 大概會長這樣
#pragma once
#include "MyObject.generated.h"
UCLASS(Blueprintable)
class UMyObject : public UObject
{
GENERATED_UCLASS_BODY()
public:
UPROPERTY(EditAnywhere, Category = "My Object Properties")
FString Name;
};
MyObject.cpp
長這樣
#include "MyObject.h"
UMyObject::UMyObject(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
這時你只需要建一個工廠 Factory
不意外的話叫 MyObjectFactory
所以 MyObjectFactory.h 長這樣
#pragma once
#include "UnrealEd.h"
#include "MyObjectFactory.generated.h"
UCLASS()
class UMyObjectFactory : public UFactory
{
GENERATED_UCLASS_BODY()
// UFactory interface
virtual UObject* FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn) override;
// End of UFactory interface
};
MyObjectFactory.cpp 長這樣
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "MyObjectFactory.h"
#define LOCTEXT_NAMESPACE "MyObject"
/////////////////////////////////////////////////////
// UMyObjectFactory
UMyObjectFactory::UMyObjectFactory(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bCreateNew = true;
bEditAfterNew = true;
SupportedClass = UMyObject::StaticClass();
}
UObject* UMyObjectFactory::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn)
{
UMyObject* NewObjectAsset = ConstructObject(Class, InParent, Name, Flags | RF_Transactional);
return NewObjectAsset;
}
#undef LOCTEXT_NAMESPACE
基本上就把 UMyObject 換成你的Class就可以了。
但實際使用上面會有一個問題,你還是沒有辨法建立Actor的Asset,
只能把自己的Actor拉進場景之後編輯,不能在Content Browser 中編輯,
這個問題我後來看Paper2D的源碼解決了,
不過太複雜了,詳細有空再解說。
要用轉換成Actor拉入場景
除了原來的兩個class
還要加入
XXXcomponent
XXXactor
XXXactorfactory
需要自定義的縮圖就加入
XXXThumbnailRenderer