Files
2024-11-20 15:21:28 +01:00

149 lines
5.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace Gaia
{
public enum LoadMode { Disabled, EditorSelected, EditorAlways, RuntimeAlways }
[ExecuteInEditMode]
public class TerrainLoader : MonoBehaviour
{
public BoundsDouble m_loadingBounds = new BoundsDouble();
[SerializeField]
[HideInInspector]
private LoadMode m_loadMode = LoadMode.EditorSelected;
public LoadMode LoadMode {
get {
return m_loadMode;
}
set {
m_loadMode = value;
UpdateTerrains();
}
}
[HideInInspector]
public bool m_isSelected = false;
[HideInInspector]
public bool m_beingDragged = false;
public float m_minRefreshDistance = 100f;
public float m_maxRefreshDistance = 2000f;
public float m_minRefreshMS = 100f;
public float m_maxRefreshMS = 5000f;
public bool m_followTransform = true;
private BoundsDouble m_shiftedBounds = new BoundsDouble();
private GaiaSessionManager m_sessionManager;
private GaiaSessionManager SessionManager
{
get
{
if (m_sessionManager == null)
{
m_sessionManager = GaiaSessionManager.GetSessionManager(false);
}
return m_sessionManager;
}
}
private void OnDrawGizmos()
{
#if UNITY_EDITOR
if (TerrainLoaderManager.Instance.ShowLocalTerrain && ((m_loadMode == LoadMode.EditorSelected && m_isSelected) || m_loadMode == LoadMode.EditorAlways || (m_loadMode == LoadMode.RuntimeAlways && (Application.isPlaying || m_isSelected))))
{
if (m_loadMode == LoadMode.RuntimeAlways && !Application.isPlaying)
{
Gizmos.color = Color.black;
}
else
{
Gizmos.color = Color.magenta;
}
//m_loadingBounds.center = transform.position;
if (m_followTransform)
{
m_loadingBounds.center = transform.position;
}
Gizmos.DrawWireCube(m_loadingBounds.center, m_loadingBounds.size);
}
#endif
}
private void Update()
{
if (m_loadMode == LoadMode.RuntimeAlways)
{
if (m_followTransform)
{
m_loadingBounds.center = transform.position;
}
UpdateTerrains();
}
}
public void Start()
{
#if UNITY_EDITOR
if (EditorApplication.isPlaying)
{
//Display Error when Loaders are not activated in the Loader Manager
if (m_loadMode == LoadMode.RuntimeAlways && !TerrainLoaderManager.Instance.TerrainSceneStorage.m_terrainLoadingEnabled)
{
Debug.LogError("Terrain Loaders are currently disabled under Gaia Runtime > Terrain Loader. The Terrain Loader on the object '" + transform.name + "' will not be able to load any terrains.");
}
}
#endif
}
public void Awake()
{
#if UNITY_EDITOR
if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
{
//Auto-Deselect non runtime loaders on entering playmode
//otherwise the tool might interfere with runtime loading
if(m_loadMode!=LoadMode.RuntimeAlways && m_isSelected)
{
Selection.objects = new Object[1] { GaiaUtils.GetGaiaGameObject()};
}
}
UpdateTerrains();
#endif
}
public void UnloadTerrains()
{
if (this!=null && gameObject != null)
{
foreach (TerrainScene terrainScene in TerrainLoaderManager.TerrainScenes)
{
if (terrainScene.HasReference(gameObject))
{
terrainScene.RemoveReference(gameObject);
}
}
}
}
public void UpdateTerrains()
{
if (m_loadMode == LoadMode.Disabled || (m_loadMode == LoadMode.EditorSelected && !m_isSelected) || (m_loadMode==LoadMode.RuntimeAlways && !Application.isPlaying))
{
UnloadTerrains();
return;
}
//shave off a tiny little bit of the loading size. - when a tool such as a spawner has a range that lies directly
//on the edge of the terrain border, this leads to up to 9 additional terrains being loaded that are not required.
//This results in slower spawning speeds, spawn issues, etc.
m_shiftedBounds.extents = m_loadingBounds.extents - new Vector3Double(0.001, 0.001, 0.001);
m_shiftedBounds.center = m_loadingBounds.center + TerrainLoaderManager.Instance.GetOrigin();
TerrainLoaderManager.Instance.UpdateTerrainLoadState(m_shiftedBounds, gameObject, m_minRefreshDistance, m_maxRefreshDistance, m_minRefreshMS, m_maxRefreshMS);
}
}
}