Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit bc471e0

Browse files
authored
Mapbuilder fixes (#111)
* Fix some warnings and null ref exceptions in the map builder
1 parent 52e11bb commit bc471e0

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

workers/unity/Assets/Fps/Scripts/Config/MapBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void CleanAndBuild(
9595
PlaceTiles();
9696
PlaceGround();
9797
FillSurround();
98+
CollapseTileMeshes();
9899
MakeLevelObjectStatic();
99100

100101
spawnPointSystemTransform.gameObject.GetComponent<SpawnPoints>()?.SetSpawnPoints();
@@ -120,6 +121,11 @@ public void CleanAndBuild(
120121
$"x{numTotalTilesWide * mapBuilderSettings.UnitsPerTile + mapBuilderSettings.UnitsPerBlock} units\n");
121122
}
122123

124+
private void CollapseTileMeshes()
125+
{
126+
tileParentTransform.GetComponent<TileCollapser>().CollapseMeshes();
127+
}
128+
123129
private void InitializeGroupsAndComponents()
124130
{
125131
if (gameObject.GetComponentInChildren<SpawnPoints>() == null)

workers/unity/Assets/Fps/Scripts/Editor/MapBuilderVisualisationWindow.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ public void OnGUI()
8080
}
8181
}
8282

83+
EditorGUI.BeginDisabledGroup(mapBuilder == null);
8384
if (GUILayout.Button("Clear Map"))
8485
{
8586
mapBuilder.Clean();
8687
}
88+
89+
EditorGUI.EndDisabledGroup();
8790
}
8891

8992
private bool GetGenerationUserConfirmation(int numTiles)

workers/unity/Assets/Fps/Scripts/GameLogic/Respawning/SpawnPoints/SpawnPoints.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ public void SetSpawnPoints()
5353
SpawnYaw = spawnPointTransform.eulerAngles.y,
5454
SpawnPitch = 0
5555
};
56-
Destroy(spawnPoints[n]);
56+
57+
if (Application.isPlaying)
58+
{
59+
Destroy(spawnPoints[n]);
60+
}
61+
else
62+
{
63+
DestroyImmediate(spawnPoints[n]);
64+
}
5765
}
5866
}
5967

workers/unity/Assets/Fps/Scripts/GameLogic/WorldTiles/TileCollapser.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using Improbable.Gdk.Core;
23
using MeshUtilities;
34
using UnityEngine;
45

@@ -7,7 +8,7 @@ public class TileCollapser : MonoBehaviour
78
private readonly Dictionary<string, CombinedMeshAndMaterialsData> collapsedInstances =
89
new Dictionary<string, CombinedMeshAndMaterialsData>();
910

10-
private void Awake()
11+
public void CollapseMeshes()
1112
{
1213
for (var i = 0; i < transform.childCount; i++)
1314
{
@@ -16,7 +17,7 @@ private void Awake()
1617
if (!collapsedInstances.ContainsKey(child.name))
1718
{
1819
var combined = TileCombinedMeshProvider.GetCombinedMeshes(child);
19-
combined.combinedMesh.name = child.name + "_Mesh";
20+
combined.combinedMesh.name = $"{child.name}_Mesh";
2021
collapsedInstances.Add(child.name, combined);
2122
}
2223

@@ -29,12 +30,12 @@ private void DestroyMeshRenderers(Transform obj)
2930
{
3031
foreach (var meshRenderer in obj.GetComponentsInChildren<MeshRenderer>())
3132
{
32-
Destroy(meshRenderer);
33+
UnityObjectDestroyer.Destroy(meshRenderer);
3334
}
3435

3536
foreach (var meshFilter in obj.GetComponentsInChildren<MeshFilter>())
3637
{
37-
Destroy(meshFilter);
38+
UnityObjectDestroyer.Destroy(meshFilter);
3839
}
3940
}
4041

workers/unity/Assets/Fps/Scripts/GameLogic/WorldTiles/TileCombinedMeshProvider.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using UnityEngine;
4+
using Object = UnityEngine.Object;
45

56
namespace MeshUtilities
67
{
@@ -69,16 +70,20 @@ private static Dictionary<Material, List<CombineInstance>> GatherMaterialsAndMes
6970
for (var j = 0; j < meshFilters.Length; j++)
7071
{
7172
var meshFilter = meshFilters[j];
72-
7373
var meshRenderer = meshFilter.GetComponent<MeshRenderer>();
7474

75-
var material = meshRenderer.sharedMaterial;
76-
var combineInstance = new CombineInstance();
75+
if (meshFilter.sharedMesh == null)
76+
{
77+
Debug.LogWarning("Skipping missing mesh", meshFilter.gameObject);
78+
continue;
79+
}
7780

81+
var material = meshRenderer.sharedMaterial;
82+
var objMesh = Object.Instantiate(meshFilter.sharedMesh);
7883

79-
var objMesh = meshFilter.mesh;
8084
ApplyScaleToVertexColours(ref objMesh, meshFilter.transform.localScale);
8185

86+
var combineInstance = new CombineInstance();
8287
combineInstance.mesh = objMesh;
8388
combineInstance.transform = meshFilter.transform.localToWorldMatrix;
8489

0 commit comments

Comments
 (0)