using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; namespace WorldStreamer2 { public class PlayerMover : MonoBehaviour { /// /// The streamers. /// [Tooltip("List of streamers objects that should affect loading screen. Drag and drop here all your streamer objects from scene hierarchy which should be used in loading screen.")] public Streamer[] streamers; /// /// The player transform. /// [Space(10)] [Tooltip("Drag and drop here, an object that system have to follow during streaming process.")] public Transform player; /// /// The player safe position during loading. /// [Tooltip("The player safe position during loading.")] public Transform safePosition; [Space(10)] public UnityEvent onDone; private GameObject temporaryObject; private float progress = 0; private bool waitForPlayer = false; private bool playerMoved = false; /// /// Awake this instance. /// void Awake() { if (streamers.Length > 0) { if (streamers[0].spawnedPlayer == false) { MovePlayer(); } else { waitForPlayer = true; } } } /// /// Update this instance, and sets current progress of streaming. /// void Update() { if (waitForPlayer) { if (player == null && !string.IsNullOrEmpty(streamers[0].playerTag)) { GameObject playerGo = GameObject.FindGameObjectWithTag(streamers[0].playerTag); if (playerGo != null) { player = playerGo.transform; MovePlayer(); waitForPlayer = false; } } } else { if (!playerMoved) { if (streamers.Length > 0) { bool initialized = true; progress = 0; foreach (var item in streamers) { progress += item.LoadingProgress / (float) streamers.Length; initialized = initialized && item.initialized; } if (initialized) { if (progress >= 1) { if (onDone != null) onDone.Invoke(); Done(); } } } else Debug.Log("No streamer Attached"); } } } public void Done() { player.position = temporaryObject.transform.position; player.rotation = temporaryObject.transform.rotation; foreach (var item in streamers) { item.player = player; } Destroy(temporaryObject); playerMoved = true; gameObject.SetActive(false); } /// /// Moves player for loading; /// public void MovePlayer() { temporaryObject = new GameObject("Temporary"); temporaryObject.transform.position = player.position; temporaryObject.transform.rotation = player.rotation; foreach (var item in streamers) { item.player = temporaryObject.transform; } Debug.Log(safePosition.position); player.position = safePosition.position; player.rotation = safePosition.rotation; gameObject.SetActive(true); playerMoved = false; } } }