Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
LostCurrencyController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LostCurrencyController : MonoBehaviour
{
public int currency;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.GetComponent<Player>() != null)
{
PlayerManager.instance.currency += currency;
Destroy(this.gameObject);
}
}
}
GameManager.cs
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;//关于场景的操作
public class GameManager : MonoBehaviour, ISaveManager
{
public static GameManager instance;
private Transform player;
[SerializeField] private Checkpoint[] checkpoints;
[SerializeField] private string closestCheckpointId;
[Header("Lost currency")]
[SerializeField] private GameObject lostCurrencyPerfab;
public int lostCurrencyAmount;
[SerializeField] private float lostCurrencyX;
[SerializeField] private float lostCurrencyY;
private void Awake()
{
if (instance != null)
{
Destroy(instance.gameObject);
}
else
instance = this;
checkpoints = FindObjectsOfType<Checkpoint>();
player = PlayerManager.instance.player.transform;
}
private void Start()
{
checkpoints = FindObjectsOfType<Checkpoint>();
}
public void RestratScene()//场景重开函数
{
SaveManager.instance.SaveGame();
Scene scene = SceneManager.GetActiveScene();//获得初始场景
SceneManager.LoadScene(scene.name);//获取的场景必须通过字符串载入
}
public void LoadData(GameData _data)
{
foreach (KeyValuePair<string, bool> pair in _data.checkpoints)
{
foreach (Checkpoint checkpoint in checkpoints)
{
if (checkpoint.id == pair.Key && pair.Value == true)
{
checkpoint.ActivateCheckpoint();
}
}
}
closestCheckpointId = _data.closestCheckpointId;
PlacePlayerAtClosestCheckpoint();
LoadLostCurrency(_data);
}
private void LoadLostCurrency(GameData _data)//产生可以捡到的钱尸体函数
{
lostCurrencyAmount = _data.lostCurrencyAmount;
lostCurrencyX = _data.lostCurrencyX;
lostCurrencyY = _data.lostCurrencyY;
if(lostCurrencyAmount > 0)
{
GameObject newLostCurrency = Instantiate(lostCurrencyPerfab, new Vector3(lostCurrencyX, lostCurrencyY), Quaternion.identity);
newLostCurrency.GetComponent<LostCurrencyController>().cur