I am trying to develop a plugin. At some point, the plugin has to save material function uassets.
One of the material function (topFunction) uses other material material functions (subFunctions). When I click the button, the plugin saves all of them.
After they are saved, I cannot edit&save nor delete top function manually. However I can edit&save or delete sub functions manually (deleting topFunction in the first place or in the last place does not change the outcome). All functions show memory references as a warning when I tried to delete them. I can edit&save or delete topFunction only when I restarted UE4.
What am I doing wrong? How to save them from the plugin, so that no memory reference will interfere with manual operation? Is there any way to see the memory reference?
Top Function Material Code:
FString pluginName("PluginName");
FString projectPluginsDir = FPaths::ProjectPluginsDir();
FString projectPluginDir = FString::Printf(TEXT("%s%s/"), *projectPluginsDir, *pluginName);
FString projectPluginContentDir = FString::Printf(TEXT("%sContent/"), *projectPluginDir);
FString projectContentDir = FPaths::ProjectContentDir();
FString projectContentPluginDir = FString::Printf(TEXT("%s%s/"), *projectContentDir, *pluginName);
FPackageName::RegisterMountPoint(*FString::Printf(TEXT("/Game/%s/"), *pluginName), *projectContentPluginDir);
FString assetName = FString::Printf(TEXT("MF_Top"));
FString packageName = FString::Printf(TEXT("/Game/PluginName/%s"), *assetName);
UPackage* package = CreatePackage(nullptr, *packageName);
UMaterialFunction* topFunction = NewObject<UMaterialFunction>(package, UMaterialFunction::StaticClass(), *assetName, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone);
FAssetRegistryModule::AssetCreated(topFunction);
std::vector<UMaterialExpressionMaterialFunctionCall*> subFunctionExpressions;
for (unsigned int i = 0; i < count; i++) {
UMaterialExpressionMaterialFunctionCall* subFunctionExpression = NewObject<UMaterialExpressionMaterialFunctionCall>(
topFunction, UMaterialExpressionMaterialFunctionCall::StaticClass(), *subAssetName,
EObjectFlags::RF_Public | EObjectFlags::RF_Standalone);
subFunctionExpression->SetMaterialFunction(materialFunctions.at(i));
subFunctionExpression->UpdateFromFunctionResource();
subFunctionExpressions.push_back(subFunctionExpression);
topFunction->DependentFunctionExpressionCandidates.Add(subFunctionExpression);
topFunction->FunctionExpressions.Add(subFunctionExpression);
}
FString FilePath = FString::Printf(TEXT("%s%s%s"), *projectContentPluginDir, *assetName, *FPackageName::GetAssetPackageExtension());
bool bSuccess = UPackage::SavePackage(package, topFunction, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *FilePath, GLog, nullptr, false, false, SAVE_NoError);