Skip to content

Commit

Permalink
Only destroy textures created at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Feb 2, 2024
1 parent db4df38 commit fbafe66
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion native~/Runtime/src/UnityPrepareRendererResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ void* UnityPrepareRendererResources::prepareInMainThread(
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture =
TextureLoader::loadTexture(gltf, baseColorTexture->index);
texture.hideFlags(DotNet::UnityEngine::HideFlags::DontSave);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getBaseColorTextureID(),
Expand All @@ -1210,6 +1211,7 @@ void* UnityPrepareRendererResources::prepareInMainThread(
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture =
TextureLoader::loadTexture(gltf, metallicRoughness->index);
texture.hideFlags(DotNet::UnityEngine::HideFlags::DontSave);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getMetallicRoughnessTextureID(),
Expand All @@ -1230,6 +1232,7 @@ void* UnityPrepareRendererResources::prepareInMainThread(
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
pMaterial->normalTexture->index);
texture.hideFlags(DotNet::UnityEngine::HideFlags::DontSave);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getNormalMapTextureID(),
Expand All @@ -1251,6 +1254,7 @@ void* UnityPrepareRendererResources::prepareInMainThread(
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
pMaterial->occlusionTexture->index);
texture.hideFlags(DotNet::UnityEngine::HideFlags::DontSave);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getOcclusionTextureID(),
Expand Down Expand Up @@ -1287,6 +1291,7 @@ void* UnityPrepareRendererResources::prepareInMainThread(
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
pMaterial->emissiveTexture->index);
texture.hideFlags(DotNet::UnityEngine::HideFlags::DontSave);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getEmissiveTextureID(),
Expand Down Expand Up @@ -1387,10 +1392,14 @@ void freePrimitiveGameObject(
for (int32_t i = 0, len = textureIDs.Count(); i < len; ++i) {
int32_t textureID = textureIDs[i];
UnityEngine::Texture texture = material.GetTexture(textureID);
if (texture != nullptr)
std::string name = texture == nullptr ? "" : texture.name().ToStlString();
if (texture != nullptr && (texture.hideFlags() & UnityEngine::HideFlags::DontSave)) {
UnityLifetime::Destroy(texture);
}
}

std::string materialName =
material == nullptr ? "" : material.name().ToStlString();
UnityLifetime::Destroy(material);
}

Expand Down

0 comments on commit fbafe66

Please sign in to comment.