UE4自动导入模型

前言:基本思路就是选择一个FBX文件的路径,利用UFbxFactory的ImportObject 导入模型, 在这之前要先去利用Upackage去创建对应的路径。

1.首先创建一个继承与Object的类名字叫做AutoLoad
Autoload.h文件

UCLASS()
class DAY1_API UAutoLoad : public UObject
{
	GENERATED_BODY()
public:

	void InitImport(FString Name);

	UObject* autoInput(FString path);
	
	//最重要的FBX导入器 , 调用ImportObject 导入模型
	class UFbxFactory* FBXmanage;


	//路径
	FString Absolute_Base;


	//需要保存的资源的文件夹
	FString BasePKGPath;

	//取消导入
	bool&& Canceled = false;
};

2.Autoload.cpp

 //这个方法是为了创建一个FBX文件的路径, 也可以直接调用autoimprot给绝对路径
void UAutoLoad::InitImport(FString Name)
{
	Absolute_Base = FPaths::ProjectDir() + "/Model/";
	FString Path = Absolute_Base / Name;
	autoInput(Path);
}


//主要的导入函数
UObject* UAutoLoad::autoInput(FString path)
{
	//实例化FBXFactory
	FBXmanage = NewObject();
	//给FBXFactor 创建Task
	UAssetImportTask* ImprtTask = NewObject();
	
	BasePKGPath = TEXT("Asset");

	//先去判断文件夹有没有
	if (BasePKGPath!=TEXT(""))
	{
		//没有就去创建
		if (FPaths::DirectoryExists(FPaths::ProjectContentDir()+BasePKGPath))
		{
			IPlatformFile& R = FPlatformFileManager::Get().GetPlatformFile();
			if (!R.CreateDirectoryTree(*(FPaths::ProjectContentDir()+BasePKGPath)))
			{
				UE_LOG(LogTemp, Error, TEXT("Error  Can't Create Directory"));
			}
		}
	}

	//设置参数
	if (FBXmanage)
	{
		//导入的类型
		FBXmanage->ImportUI->MeshTypeToImport = FBXIT_StaticMesh;
		FBXmanage->ImportUI->OriginalImportType = FBXIT_StaticMesh;

		//ImprotTask参数设置
		ImprtTask->bAutomated = true;
		ImprtTask->bSave = true;
		
		//如果FBXFactir 没有默认的AssetTask,就去设定一个上面声明的
		if (!FBXmanage->AssetImportTask)
		{

			FBXmanage->SetAssetImportTask(ImprtTask);
		}
		/*************************
		自动导入, 设置导入的辅助类
		**************************/
		/*UnFbx::FFbxImporter* FbxImporter = UnFbx::FFbxImporter::GetInstance();*/
	/*	UnFbx::ApplyImportUIToImportOptions(FBXmanage->ImportUI, *FbxImporter->ImportOptions);*/
		FBXmanage->ImportUI->StaticMeshImportData->bCombineMeshes = true;
		FBXmanage->ImportUI->StaticMeshImportData->ImportUniformScale = 1.0f;
		FBXmanage->ImportUI->bImportMaterials = true;
		FBXmanage->ImportUI->bImportTextures = true;
		FBXmanage->ImportUI->bAutoComputeLodDistances = true;
		FBXmanage->ImportUI->StaticMeshImportData->bAutoGenerateCollision = true;
		if (FBXmanage->FactoryCanImport(path))
		{
			UE_LOG(LogTemp, Error, TEXT("可以导入这个模型"));
		}
		//现在开始建立Package文件
		FString Name = FPaths::GetBaseFilename(path);
		FString PKGNames = "/Game/" + BasePKGPath;
		int ExtPos = Name.Find(TEXT("/"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);

		if (ExtPos!=INDEX_NONE)
		{
			Name.Right(ExtPos);
		}

		PKGNames = PKGNames + "/" + Name;
		UPackage* DirectFolder = FindPackage(nullptr, *PKGNames);
		if (!DirectFolder&&FEditorFileUtils::IsMapPackageAsset(PKGNames))
		{
			ensure(false);
		}
		if (!FPackageName::DoesPackageExist(PKGNames))
		{
			DirectFolder = CreatePackage(nullptr, *PKGNames);  //创建目录
			if (DirectFolder)
			{
				DirectFolder->FullyLoad();
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Create DirectFloder Failed"));
			}
		}

		//导入完成的回调
	/*	FEditorDelegates::OnAssetPostImport.AddUFunction(this, "ok");*/
		FBXmanage->ImportObject(UStaticMesh::StaticClass(), DirectFolder, *Name
			, EObjectFlags::RF_Transactional | EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, path, nullptr, Canceled
		);

		//到此导入完成

		//如果是复合模型, 需要重新导入
		//FString PkgNames = FPaths::GetBaseFilename(Paths);
			//UObject* Pkg = StaticFindObject(UObject::StaticClass(), DirectFloder, *PkgNames);
			//if (Pkg)
			//{
			//	FReimportManager::Instance()->Reimport(Pkg, false, true);
			//}

	}
	return nullptr;
}

你可能感兴趣的:(UE4)