Skip to content
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

Add Scene Async Loading #71

Merged
merged 5 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Assets/Prefabs/BattleField Builder.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ MonoBehaviour:
_fieldLength: 30
_fieldWidth: 30
_tileSize: 2
_wallDensity: 0.1
_wallDensity: 0.0404
_bushesDensity: 0.03
_maxBushSize: 3
_potsCount: 9
_heroesCount: 14
_heroesCount: 20
_spawnDistance: 8
6 changes: 3 additions & 3 deletions Assets/Prefabs/Environment/Ground.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Transform:
m_GameObject: {fileID: 3321912436738672957}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_LocalScale: {x: 11.809999, y: 11.809999, z: 11.809999}
m_ConstrainProportionsScale: 1
m_Children:
- {fileID: 4241119107875755153}
m_Father: {fileID: 0}
Expand Down Expand Up @@ -90,7 +90,7 @@ Transform:
m_GameObject: {fileID: 6908426290981875276}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 20, y: 1, z: 20}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1992661901449262017}
Expand Down
17 changes: 17 additions & 0 deletions Assets/Prefabs/Environment/Wall.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 4227798970065305816}
- component: {fileID: 3622335821436744679}
- component: {fileID: 5044149668879186811}
m_Layer: 0
m_Name: Wall
m_TagString: Untagged
Expand Down Expand Up @@ -62,6 +63,22 @@ MonoBehaviour:
m_VoxelSize: 0.16666667
m_BuildHeightMesh: 0
m_NavMeshData: {fileID: 0}
--- !u!114 &5044149668879186811
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4459002749115364136}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1e3fdca004f2d45fe8abbed571a8abd5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_OverrideArea: 1
m_Area: 1
m_IgnoreFromBuild: 0
m_AffectedAgents: ffffffff
--- !u!1 &4903476772157447637
GameObject:
m_ObjectHideFlags: 0
Expand Down
4 changes: 2 additions & 2 deletions Assets/Prefabs/Heroes/Characters/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 27626dbb1b1e46aebdd4bbab6ae8858d, type: 3}
m_Name:
m_EditorClassIdentifier:
_maxHealthPoints: 250
_healthPoints: 250
_maxHealthPoints: 400
_healthPoints: 400
_onDeath: {fileID: 11400000, guid: 68d23cd08d27b8e44b38090202c53f2f, type: 2}
_onHealthChanged: {fileID: 11400000, guid: eddf2d5640e89e646a041d096b9c5341, type: 2}
--- !u!114 &4340838794852804689
Expand Down
4 changes: 0 additions & 4 deletions Assets/Scenes/BattleField.unity
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,6 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8942223173646349031, guid: 00ef75323699d3b4b8c135a5653bacf2, type: 3}
propertyPath: _heroesCount
value: 30
objectReference: {fileID: 0}
- target: {fileID: 8942223173646349032, guid: 00ef75323699d3b4b8c135a5653bacf2, type: 3}
propertyPath: m_Name
value: BattleFieldBuilder
Expand Down
137 changes: 137 additions & 0 deletions Assets/Scenes/CharacterChoice.unity
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ MonoBehaviour:
_shiftDistance: 5
_shiftSpeed: 0.7
_onCharacterNameChanged: {fileID: 11400000, guid: 63d411f892bc38c46b1a256f96df4c24, type: 2}
_loadingScreen: {fileID: 2096120882}
--- !u!1 &240939033
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1380,6 +1381,7 @@ RectTransform:
- {fileID: 551379757}
- {fileID: 1347588887}
- {fileID: 923484636}
- {fileID: 2096120883}
m_Father: {fileID: 0}
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -3861,6 +3863,141 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2036183229}
m_CullTransparentMesh: 1
--- !u!1 &2096120882
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2096120883}
- component: {fileID: 2096120885}
- component: {fileID: 2096120884}
m_Layer: 5
m_Name: Loading
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &2096120883
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2096120882}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 584174524}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -613.5, y: -418.3}
m_SizeDelta: {x: 485.08, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &2096120884
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2096120882}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: Map is generating...
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 29.8
m_fontSizeBase: 29.8
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 1
m_HorizontalAlignment: 2
m_VerticalAlignment: 256
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!222 &2096120885
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2096120882}
m_CullTransparentMesh: 1
--- !u!1 &2130229080
GameObject:
m_ObjectHideFlags: 0
Expand Down
15 changes: 12 additions & 3 deletions Assets/Scripts/Battlefield/BattleFieldBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using Environment;
using Misc;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Serialization;
using Random = System.Random;

namespace Battlefield
Expand Down Expand Up @@ -37,6 +37,7 @@ public class BattleFieldBuilder : MonoBehaviour
private readonly Random _rnd = new Random();
private float xCorrection;
private float zCorrection;

private void OnValidate()
{
this.CheckIfNull(_wall, _border, _grass, _ground);
Expand All @@ -49,6 +50,12 @@ private void Start()
zCorrection = -_fieldWidth * _tileSize;
BuildTerrain();
BakeNavMesh();

Invoke(nameof(Spawn), 0.1f);
}

private void Spawn()
{
PlaceBots();
ObjectsPool.Instance.SetPlayerSpawnPosition(GetPlayerSpotCoordinates());
ObjectsPool.Instance.SetHeroes(Heroes);
Expand Down Expand Up @@ -95,7 +102,7 @@ private void BuildTerrain()
Instantiate(_border,
new Vector3(xCorrection + i * _tileSize, 0, zCorrection + j * _tileSize),
Quaternion.identity);
_navMeshSurfaces.Add(createdObject.GetComponent<NavMeshSurface>());
// _navMeshSurfaces.Add(createdObject.GetComponent<NavMeshSurface>());
break;
case 3:
Instantiate(_grass,
Expand Down Expand Up @@ -153,7 +160,9 @@ private void BakeNavMesh()
{
foreach (var navMeshSurface in _navMeshSurfaces)
{
navMeshSurface.BuildNavMesh();
navMeshSurface.collectObjects = CollectObjects.All;
navMeshSurface.useGeometry = NavMeshCollectGeometry.RenderMeshes;
navMeshSurface.BuildNavMeshAsync();
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions Assets/Scripts/Battlefield/NavMeshSurfaceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEngine;
using UnityEngine.AI;

namespace Battlefield
{
public static class NavMeshSurfaceExtensions
{
public static AsyncOperation BuildNavMeshAsync(this NavMeshSurface surface)
{
surface.RemoveData();
surface.navMeshData = new NavMeshData(surface.agentTypeID)
{
name = surface.gameObject.name,
position = surface.transform.position,
rotation = surface.transform.rotation
};

if (surface.isActiveAndEnabled)
surface.AddData();

return surface.UpdateNavMesh(surface.navMeshData);
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Battlefield/NavMeshSurfaceExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Assets/Scripts/CharacterSelection/CharacterSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace CharacterSelection
{
Expand All @@ -15,6 +16,9 @@ public class CharacterSelector : MonoBehaviour
[SerializeField] private float _shiftDistance = 2f;
[SerializeField] private float _shiftSpeed = 2f;
[SerializeField] private GameEvent _onCharacterNameChanged;

[Header("Scene Loading")]
[SerializeField] private GameObject _loadingScreen;

private int _selectedCharacterIndex = 0;
private bool _isMoving = false;
Expand All @@ -28,7 +32,21 @@ private void Start()
public void SelectCharacter()
{
SceneManager.sceneLoaded += OnSceneLoaded;
_loadingScreen.SetActive(true);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
// StartCoroutine(LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1));
}

private IEnumerator LoadSceneAsync(int sceneIndex)
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneIndex);

_loadingScreen.SetActive(true);

while (!asyncOperation.isDone)
{
yield return null;
}
}

private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Hud/HealthBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ private void Update()
{
if (_isHud is false)
{
if (_camera == null) _camera = Camera.main;

_objectToRotate.transform.rotation = Quaternion.LookRotation(transform.position - _camera.transform.position);
}
}
Expand Down