Fix memory leaks in roads

This commit is contained in:
Nahuel Rocchetti 2023-07-30 00:42:45 -03:00
parent 0c2094fa53
commit 23d0931fbf

View file

@ -8,11 +8,14 @@ using OpenTS2.Content.DBPF;
using OpenTS2.Content.DBPF.Scenegraph; using OpenTS2.Content.DBPF.Scenegraph;
using OpenTS2.Files.Formats.DBPF; using OpenTS2.Files.Formats.DBPF;
using UnityEngine; using UnityEngine;
using OpenTS2.Components;
using OpenTS2.Engine;
namespace OpenTS2.Scenes namespace OpenTS2.Scenes
{ {
public class NeighborhoodDecorations : MonoBehaviour public class NeighborhoodDecorations : AssetReferenceComponent
{ {
private List<Material> _roadMaterials = new List<Material>();
void Start() void Start()
{ {
var decorations = NeighborhoodManager.CurrentNeighborhood.Decorations; var decorations = NeighborhoodManager.CurrentNeighborhood.Decorations;
@ -43,6 +46,15 @@ namespace OpenTS2.Scenes
} }
} }
// Clean up the road materials we created. Textures should get garbage collected.
private void OnDestroy()
{
foreach(var mat in _roadMaterials)
{
mat.Free();
}
}
private void RenderRoad(RoadDecoration road) private void RenderRoad(RoadDecoration road)
{ {
var roadObject = new GameObject("road", typeof(MeshFilter), typeof(MeshRenderer)) var roadObject = new GameObject("road", typeof(MeshFilter), typeof(MeshRenderer))
@ -74,6 +86,9 @@ namespace OpenTS2.Scenes
{ {
mainTexture = texture.GetSelectedImageAsUnityTexture(ContentProvider.Get()) mainTexture = texture.GetSelectedImageAsUnityTexture(ContentProvider.Get())
}; };
_roadMaterials.Add(material);
AddReference(texture);
roadObject.GetComponent<MeshRenderer>().material = material; roadObject.GetComponent<MeshRenderer>().material = material;
} }