Main Menu with loading screen
This commit is contained in:
29
Assets/Scripts/GameOverUI.cs
Normal file
29
Assets/Scripts/GameOverUI.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameOverUI : MonoBehaviour
|
||||
{
|
||||
public GameObject panel;
|
||||
public static GameOverUI instance;
|
||||
|
||||
void Awake(){
|
||||
instance=this;
|
||||
}
|
||||
public static void Activate(){
|
||||
instance.activate();
|
||||
}
|
||||
|
||||
void activate(){
|
||||
panel.SetActive(true);
|
||||
}
|
||||
|
||||
|
||||
public void OnMenu(){
|
||||
LoadingScreen.LoadLevel("MainMenu");
|
||||
}
|
||||
|
||||
public void Restart(){
|
||||
LoadingScreen.LoadLevel("Game");
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GameOverUI.cs.meta
Normal file
11
Assets/Scripts/GameOverUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1234d81ad3fcbef95b5dfbbdc74e43ad
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
Assets/Scripts/LoadingScreen.cs
Normal file
104
Assets/Scripts/LoadingScreen.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
public class LoadingScreen : MonoBehaviour
|
||||
{
|
||||
public static LoadingScreen instance {get; private set;}
|
||||
private IProgressBar progress;
|
||||
[SerializeField]private Text loadingProgressTxt;
|
||||
[SerializeField]private Transform progressBar;
|
||||
|
||||
void Awake(){
|
||||
if(instance != null){Destroy(gameObject);return;}
|
||||
instance =this;
|
||||
canvasGroup = GetComponent<CanvasGroup>();
|
||||
|
||||
Application.targetFrameRate = 60;
|
||||
|
||||
progress = progressBar.GetComponent<IProgressBar>();
|
||||
if(progress==null){Debug.LogError("NO IProgressBar interface found on " + progressBar.name);}
|
||||
}
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
public static void LoadLevel(string levelName){
|
||||
if(instance==null){Debug.LogError("No loading screen found, Raw load"); SceneManager.LoadScene(levelName); return;}
|
||||
|
||||
instance.loadLevel(levelName);
|
||||
}
|
||||
|
||||
public void loadLevel(string levelName){
|
||||
if (loading) { return; }
|
||||
loading = true;
|
||||
StartCoroutine(_loadlLevel(levelName));
|
||||
}
|
||||
|
||||
public static bool loading {get; private set;}
|
||||
private CanvasGroup canvasGroup;
|
||||
IEnumerator _loadlLevel(string levelName)
|
||||
{
|
||||
// AudioManager.instnace.SetMusic(-1);
|
||||
loading = true;
|
||||
canvasGroup.alpha = 0;
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
|
||||
SetProgress(0);
|
||||
while (canvasGroup.alpha < 1f)
|
||||
{
|
||||
canvasGroup.alpha += 0.03f;
|
||||
yield return new WaitForFixedUpdate();
|
||||
}
|
||||
|
||||
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(levelName);
|
||||
|
||||
while (!asyncOperation.isDone)
|
||||
{
|
||||
SetProgress(asyncOperation.progress);
|
||||
yield return null;
|
||||
}
|
||||
Debug.Log("Loaded scene " + levelName);
|
||||
// yield return new WaitForSecondsRealtime(2f);
|
||||
while(progress.Progress < 1){
|
||||
SetProgress( progress.Progress+0.01f);
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
}
|
||||
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
|
||||
while (canvasGroup.alpha > 0)
|
||||
{
|
||||
canvasGroup.alpha -= 0.03f;
|
||||
yield return new WaitForSecondsRealtime(0.015f);
|
||||
|
||||
}
|
||||
Debug.Log("Loading scene vanishing");
|
||||
|
||||
loading = false;
|
||||
}
|
||||
|
||||
void SetProgress(float value)
|
||||
{
|
||||
// progre.fillAmount = value;
|
||||
progress.SetProgress(value);
|
||||
|
||||
if(loadingProgressTxt!=null)loadingProgressTxt.text = (int)(value * 100) + "%";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public interface IProgressBar{
|
||||
|
||||
public void SetProgress(float progress){
|
||||
}
|
||||
|
||||
public float Progress {get;}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/LoadingScreen.cs.meta
Normal file
11
Assets/Scripts/LoadingScreen.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9065cbd09509745c3941e19533f30779
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Assets/Scripts/MainMenu.cs
Normal file
20
Assets/Scripts/MainMenu.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MainMenu : MonoBehaviour
|
||||
{
|
||||
public void OnPlay(){
|
||||
LoadingScreen.LoadLevel("Game");
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/MainMenu.cs.meta
Normal file
11
Assets/Scripts/MainMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f140959789d9f94bb8d434e95a5e6b0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
15
Assets/Scripts/MultiTool.cs
Normal file
15
Assets/Scripts/MultiTool.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MultiTool : MonoBehaviour
|
||||
{
|
||||
public Vector3 movingSpeed;
|
||||
public Vector3 rotation;
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
transform.Translate(movingSpeed);
|
||||
transform.Rotate(rotation);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/MultiTool.cs.meta
Normal file
11
Assets/Scripts/MultiTool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e618c0fe7ab97e5daa46b93c6d48041
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -12,8 +12,8 @@ public class PlayerController : MonoBehaviour
|
||||
}
|
||||
|
||||
public static PlayerController instance;
|
||||
public static Transform t => instance.transform;
|
||||
public static Vector3 position=> instance.transform.position;
|
||||
public static Transform t => instance.transform ?? Camera.main.transform;
|
||||
public static Vector3 position=> (instance == null) ? Camera.main.transform.position : instance.transform.position;
|
||||
public float movingSpeed = 1;
|
||||
public float speedIncremental = 0.01f;
|
||||
public float verticalSpeed = 0.5f;
|
||||
@@ -22,6 +22,8 @@ public class PlayerController : MonoBehaviour
|
||||
|
||||
public Text txtScore;
|
||||
|
||||
public GameObject ExplosionFX;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
@@ -72,6 +74,10 @@ public class PlayerController : MonoBehaviour
|
||||
|
||||
|
||||
public void GameOver(){
|
||||
Application.LoadLevel(0);
|
||||
// Application.LoadLevel(0);
|
||||
GameOverUI.Activate();
|
||||
|
||||
Instantiate(ExplosionFX, transform.position, Quaternion.identity);
|
||||
Destroy(this);
|
||||
}
|
||||
}
|
||||
|
||||
20
Assets/Scripts/SizeProgressBar.cs
Normal file
20
Assets/Scripts/SizeProgressBar.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SizeProgressBar : MonoBehaviour, IProgressBar{
|
||||
public float Progress { get{return progress;}}
|
||||
private float progress =0;
|
||||
|
||||
public RectTransform target;
|
||||
|
||||
public Vector3 start,end;
|
||||
|
||||
|
||||
void OnValidate(){
|
||||
if(target==null){target=GetComponent<RectTransform>();}
|
||||
if(start==end){start = end = target.sizeDelta;}
|
||||
}
|
||||
public void SetProgress(float val){
|
||||
progress = val;
|
||||
target.sizeDelta = Vector3.Lerp(start,end, val);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/SizeProgressBar.cs.meta
Normal file
11
Assets/Scripts/SizeProgressBar.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ccf28dc9fd5f0df8f88b93ac1c898f35
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
111
Assets/Scripts/TweenHelper.cs
Normal file
111
Assets/Scripts/TweenHelper.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class TweenHelper : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
public float delay = 1;
|
||||
public LeanTweenType position_type, scale_type;
|
||||
public Vector3 startPos;
|
||||
public float position_time=1;
|
||||
public float scale_time=1;
|
||||
public Vector3 startScale;
|
||||
|
||||
Vector3 defaultPos, defaultScale;
|
||||
|
||||
[Header("Monitor data")]
|
||||
public Vector3 curPosition;
|
||||
public Vector3 curScale;
|
||||
|
||||
void Awake(){
|
||||
defaultPos = transform.position;
|
||||
defaultScale=transform.localScale;
|
||||
|
||||
|
||||
if(position_type != LeanTweenType.notUsed){ transform.position = startPos;}
|
||||
transform.localScale = startScale;
|
||||
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
Intro();
|
||||
}
|
||||
|
||||
public void Intro(){
|
||||
|
||||
TweenManager.Add(this);
|
||||
StartCoroutine(start());
|
||||
}
|
||||
|
||||
public void Outro(){
|
||||
if(position_type != LeanTweenType.notUsed){
|
||||
LeanTween.move(gameObject, startPos, position_time).setEase(position_type);
|
||||
}
|
||||
|
||||
if(scale_type != LeanTweenType.notUsed){
|
||||
LeanTween.scale(gameObject, startScale, scale_time).setEase(scale_type);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator start(){
|
||||
yield return new WaitForSeconds(delay);
|
||||
|
||||
LeanTween.move(gameObject, defaultPos, position_time).setEase(position_type);
|
||||
LeanTween.scale(gameObject, defaultScale, scale_time).setEase(scale_type);
|
||||
}
|
||||
|
||||
|
||||
void OnDrawGizmos(){
|
||||
if(position_type == LeanTweenType.notUsed){return;}
|
||||
|
||||
curPosition = transform.position;
|
||||
curScale = transform.localScale;
|
||||
Gizmos.DrawLine(startPos, curPosition);
|
||||
}
|
||||
bool init = false;
|
||||
void OnValidation(){
|
||||
if(!init){
|
||||
init=true;
|
||||
}else{
|
||||
return;
|
||||
}
|
||||
startPos = transform.position;
|
||||
startScale = transform.localScale;
|
||||
}
|
||||
|
||||
|
||||
public void OutroAll(){
|
||||
TweenManager.OutroAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class TweenManager{
|
||||
public static List<TweenHelper> Tweens{get; private set;}
|
||||
|
||||
public static void Add(TweenHelper tween){
|
||||
if(Tweens == null){Tweens = new List<TweenHelper>();}
|
||||
if(Tweens.Contains(tween)){return;}
|
||||
Tweens.Add(tween);
|
||||
}
|
||||
|
||||
public static void Outro(TweenHelper tween){
|
||||
tween.Outro();
|
||||
Tweens.Remove(tween);
|
||||
|
||||
}
|
||||
|
||||
public static void OutroAll(){
|
||||
foreach(TweenHelper tween in Tweens){
|
||||
if(tween == null){continue;}
|
||||
tween.Outro();
|
||||
}
|
||||
|
||||
for(int i=Tweens.Count-1; i >= 0; i--){
|
||||
Tweens.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/TweenHelper.cs.meta
Normal file
11
Assets/Scripts/TweenHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17bed29c9d0e93ad883a347fc1e43254
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user