diff --git a/Assets/Scripts/OpenTS2/Scenes/NeighborhoodDecorations.cs b/Assets/Scripts/OpenTS2/Scenes/NeighborhoodDecorations.cs index bd49f65..e74088c 100644 --- a/Assets/Scripts/OpenTS2/Scenes/NeighborhoodDecorations.cs +++ b/Assets/Scripts/OpenTS2/Scenes/NeighborhoodDecorations.cs @@ -8,11 +8,14 @@ using OpenTS2.Content.DBPF; using OpenTS2.Content.DBPF.Scenegraph; using OpenTS2.Files.Formats.DBPF; using UnityEngine; +using OpenTS2.Components; +using OpenTS2.Engine; namespace OpenTS2.Scenes { - public class NeighborhoodDecorations : MonoBehaviour + public class NeighborhoodDecorations : AssetReferenceComponent { + private List _roadMaterials = new List(); void Start() { 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) { var roadObject = new GameObject("road", typeof(MeshFilter), typeof(MeshRenderer)) @@ -74,6 +86,9 @@ namespace OpenTS2.Scenes { mainTexture = texture.GetSelectedImageAsUnityTexture(ContentProvider.Get()) }; + + _roadMaterials.Add(material); + AddReference(texture); roadObject.GetComponent().material = material; }