-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delete left-over Game Objects when changing LOD levels and when tiles… #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
| ||
|
||
using System.Collections; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using UnityEngine; | ||
|
@@ -10,7 +8,6 @@ | |
#if SUBOBJECT | ||
using Netherlands3D.SubObjects; | ||
#endif | ||
using System.Diagnostics; | ||
|
||
namespace Netherlands3D.CartesianTiles | ||
{ | ||
|
@@ -41,8 +38,7 @@ public override void HandleTile(TileChange tileChange, System.Action<TileChange> | |
switch (action) | ||
{ | ||
case TileAction.Create: | ||
Tile newTile = CreateNewTile(tileKey); | ||
tiles.Add(tileKey, newTile); | ||
tiles.Add(tileKey, CreateNewTile(tileKey)); | ||
break; | ||
case TileAction.Upgrade: | ||
tiles[tileKey].unityLOD++; | ||
|
@@ -54,51 +50,46 @@ public override void HandleTile(TileChange tileChange, System.Action<TileChange> | |
InteruptRunningProcesses(tileKey); | ||
RemoveGameObjectFromTile(tileKey); | ||
tiles.Remove(tileKey); | ||
callback(tileChange); | ||
callback?.Invoke(tileChange); | ||
return; | ||
default: | ||
break; | ||
} | ||
tiles[tileKey].runningCoroutine = StartCoroutine(DownloadBinaryMesh(tileChange, callback)); | ||
} | ||
|
||
private Tile CreateNewTile(Vector2Int tileKey) | ||
{ | ||
Tile tile = new Tile(); | ||
tile.unityLOD = 0; | ||
tile.tileKey = tileKey; | ||
tile.layer = transform.gameObject.GetComponent<Layer>(); | ||
return new Tile | ||
{ | ||
unityLOD = 0, | ||
tileKey = tileKey, | ||
layer = this | ||
}; | ||
} | ||
|
||
private void RemoveGameObjectFromTile(Vector2Int tileKey) | ||
{ | ||
if (!tiles.TryGetValue(tileKey, out var tile)) return; | ||
|
||
return tile; | ||
var tileGameObject = tile.gameObject; | ||
if (!tileGameObject) return; | ||
|
||
RemoveGameObject(tileGameObject); | ||
} | ||
private void RemoveGameObjectFromTile(Vector2Int tileKey) | ||
|
||
private static void RemoveGameObject(GameObject tileGameObject) | ||
{ | ||
if (tiles.ContainsKey(tileKey)) | ||
{ | ||
Tile tile = tiles[tileKey]; | ||
if (tile == null) | ||
{ | ||
return; | ||
} | ||
if (tile.gameObject == null) | ||
{ | ||
return; | ||
} | ||
string meshname = tile.gameObject.GetComponent<MeshFilter>().sharedMesh.name; | ||
MeshFilter mf = tile.gameObject.GetComponent<MeshFilter>(); | ||
if (mf != null) | ||
{ | ||
Destroy(tile.gameObject.GetComponent<MeshFilter>().sharedMesh); | ||
} | ||
Destroy(tiles[tileKey].gameObject); | ||
MeshFilter mf = tileGameObject.GetComponent<MeshFilter>(); | ||
if (mf) { | ||
Destroy(mf.sharedMesh); | ||
} | ||
Destroy(tileGameObject); | ||
} | ||
|
||
private IEnumerator DownloadBinaryMesh(TileChange tileChange, System.Action<TileChange> callback = null) | ||
{ | ||
var tileKey = new Vector2Int(tileChange.X, tileChange.Y); | ||
int index = tiles[tileKey].unityLOD; | ||
var tile = tiles[tileKey]; | ||
int index = tile.unityLOD; | ||
string url = Datasets[index].path; | ||
if (Datasets[index].path.StartsWith("https://") || Datasets[index].path.StartsWith("file://")) | ||
{ | ||
|
@@ -116,46 +107,52 @@ private IEnumerator DownloadBinaryMesh(TileChange tileChange, System.Action<Tile | |
webRequest.SetRequestHeader("Accept-Encoding", "br"); | ||
#endif | ||
|
||
tiles[tileKey].runningWebRequest = webRequest; | ||
tile.runningWebRequest = webRequest; | ||
yield return webRequest.SendWebRequest(); | ||
|
||
if (!tiles.ContainsKey(tileKey)) yield break; | ||
|
||
tiles[tileKey].runningWebRequest = null; | ||
tile.runningWebRequest = null; | ||
|
||
if (webRequest.result != UnityWebRequest.Result.Success) | ||
{ | ||
RemoveGameObjectFromTile(tileKey); | ||
callback(tileChange); | ||
callback?.Invoke(tileChange); | ||
yield break; | ||
} | ||
else | ||
{ | ||
byte[] results = webRequest.downloadHandler.data; | ||
|
||
yield return new WaitUntil(() => pauseLoading == false); | ||
GameObject newGameobject = CreateNewGameObject(url, results, tileChange); | ||
if (newGameobject != null) | ||
{ | ||
#if SUBOBJECT | ||
if (hasMetaData) | ||
{ | ||
yield return StartCoroutine(LoadMetaData(newGameobject, url)); | ||
} | ||
#endif | ||
byte[] results = webRequest.downloadHandler.data; | ||
|
||
tiles[tileKey].gameObject = newGameobject; | ||
yield return new WaitUntil(() => pauseLoading == false); | ||
GameObject newGameobject = CreateNewGameObject(url, results, tileChange); | ||
|
||
if (!newGameobject) | ||
{ | ||
callback?.Invoke(tileChange); | ||
yield break; | ||
} | ||
|
||
callback(tileChange); | ||
|
||
} | ||
else | ||
if (tiles.TryGetValue(tileKey, out tile)) | ||
{ | ||
if (tile.gameObject) RemoveGameObject(tile.gameObject); | ||
|
||
tile.gameObject = newGameobject; | ||
|
||
#if SUBOBJECT | ||
if (hasMetaData) | ||
{ | ||
callback(tileChange); | ||
yield return StartCoroutine(LoadMetaData(newGameobject, url)); | ||
} | ||
#endif | ||
} | ||
else | ||
{ | ||
// Tile was destroyed in the mean time.. destroy this game object too then. | ||
RemoveGameObject(newGameobject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fix #2 where we destroy the game object again when the tile cannot be found anymore; we can assume the Tile was removed while the Coroutine was running |
||
} | ||
} | ||
|
||
|
||
callback?.Invoke(tileChange); | ||
} | ||
|
||
#if SUBOBJECT | ||
private IEnumerator LoadMetaData(GameObject gameObject, string geometryUrl) | ||
|
@@ -185,6 +182,9 @@ private IEnumerator LoadMetaData(GameObject gameObject, string geometryUrl) | |
|
||
private void ReadMetaDataFile(byte[] results, GameObject gameobject) | ||
{ | ||
// The gameobject could be destroyed in the mean time | ||
if (!gameobject) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor fix #3, because of the other changes we now need to take into consideration the GameObject may be 'destroying' in the same frame; using the ! operator will also check the lifecycle of a gameobject and return true if it technically still exists but is in the process of destroying |
||
|
||
ObjectMapping objectMapping = gameobject.AddComponent<ObjectMapping>(); | ||
objectMapping.items = new List<ObjectMappingItem>(); | ||
using (var stream = new MemoryStream(results)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fix #1 where a prior gameobject will be cleaned up, for example when changing the LOD