Unity实战案例 2D小游戏HappyGlass(游戏管理类脚本)
本案例素材和教程都来自Siki学院,十分感谢教程中的老师
本文仅作学习笔记分享交流,不作任何商业用途
基础效果
游戏管理脚本
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public static GameManager gameManager;
public LineRenderer lineRenderer;
//控制鼠标抬起时不会画线
private bool isDown;
//优化相同点
List<Vector2> points = new List<Vector2>();
//铅笔对象
public GameObject Pencil;
//水龙头对象
public GameObject Tap;
//滑动条
public Slider slider;
//游戏内的星星
public Image[] starts;
//设置不可检测的层级
public LayerMask wall;
//设置胜利条件,和获取赢得界面的星星
public int winWater;
public GameObject[] winStars;
//胜利界面
public GameObject winPanel;
//通过的星星数量
public int starsCount;
//失败时控制杯子的表情变化
void Start()
{
gameManager = this;
isDown = true;
lineRenderer.positionCount = 0;//初始化点的个数
slider.value = 1;
starsCount = 3;
}
void Update()
{
DrawLine();
UpMouse();
}
private void DrawLine()
{
if (slider.value == 0)
{
Pencil.SetActive(false);
}
if (Input.GetMouseButton(0) && isDown == true && slider.value > 0)
{
//屏幕坐标转世界坐标
Vector2 MousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//铅笔跟随鼠标
Pencil.transform.position = MousePosition;
Pencil.SetActive(true);
//画线并优化点的位置
if (!points.Contains(MousePosition))
{
//开启铅笔晃动脚本
Pencil.GetComponent<Pencil>().enabled = true;
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, MousePosition);//画出点
points.Add(MousePosition);
}
//为画出来的线添加碰撞体
if (points.Count > 1)
{
//设置滑动条和星星减少的逻辑
slider.value -= (MousePosition - points[lineRenderer.positionCount - 2]).magnitude / 20;
if (slider.value < 0.75 && starts[2].enabled)
{
starts[2].enabled = false;
starsCount--;
}
if (slider.value < 0.5 && starts[1].enabled)
{
starts[1].enabled = false;
starsCount--;
}
if (slider.value < 0.25 && starts[0].enabled)
{
starts[0].enabled = false;
starsCount--;
}
//获取两点位置
Vector2 p1 = points[lineRenderer.positionCount - 2];
Vector2 p2 = points[lineRenderer.positionCount - 1];
//添加碰撞器
GameObject gameObject = new GameObject("Collider");
gameObject.transform.parent = lineRenderer.transform;
gameObject.transform.localPosition = (p1 + p2) / 2;
gameObject.AddComponent<BoxCollider2D>();
//设置碰撞器的位置
gameObject.GetComponent<BoxCollider2D>().size = new Vector2((p1 - p2).magnitude, lineRenderer.endWidth);
gameObject.transform.right = (p1 - p2).normalized;
}
}
}
private void UpMouse()
{
//如果抬起鼠标
if (Input.GetMouseButtonUp(0))
{
//铅笔隐藏
Pencil.SetActive(false);
Pencil.GetComponent<Pencil>().enabled = false;
//水龙头触发
Tap.GetComponent<AboutWater>().enabled = true;
isDown = false;
if (!lineRenderer.gameObject.GetComponent<Rigidbody2D>())
lineRenderer.gameObject.AddComponent<Rigidbody2D>();
StartCoroutine(Reset());
}
}
//失败重置场景
public IEnumerator Reset()
{
yield return new WaitForSeconds(10f);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
//获胜激活面板
public IEnumerator Win()
{
//激活胜利panel
winPanel.SetActive(true);
for (int i = 0; i < starsCount; i++)
{
//激活星星数量
yield return new WaitForSeconds(0.5f);
winStars[i].SetActive(true);
}
}
}