Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
Blackhole_Hotkey_Controller.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class Blackhole_Hotkey_Controller : MonoBehaviour
{
private SpriteRenderer sr;
private KeyCode myHotKey;
private TextMeshProUGUI myText;
private Transform myEnemy;
private Blackhole_Skill_Controller blackHole;
public void SetupHotKey(KeyCode _myNewHotKey, Transform _myEnemy, Blackhole_Skill_Controller _myBlackHole)
{
sr = GetComponent<SpriteRenderer>();
myText = GetComponentInChildren<TextMeshProUGUI>();
myEnemy = _myEnemy;
blackHole = _myBlackHole;
myHotKey = _myNewHotKey;
myText.text = myHotKey.ToString();
}
public void Update()
{
if(Input.GetKeyDown(myHotKey))
{
blackHole.AddEnemyToList(myEnemy);
myText.color = Color.clear;
sr.color = Color.clear;
}
}
}
Blackhole_Skill_Controller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blackhole_Skill_Controller : MonoBehaviour
{
[SerializeField] private GameObject hotKeyPrefab;
[SerializeField] private List<KeyCode> KeyCodeList;
public float maxSize;//最大尺寸
public float growSpeed;//变大速度
public bool canGrow;//是否可以变大
[SerializeField]private List<Transform> targets = new List<Transform>();
private void Update()
{
if(canGrow)
{
//这是控制物体大小的参数
transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(maxSize, maxSize), growSpeed * Time.deltaTime);
//类似MoveToward,不过是放大到多少大小 https://docs.unity3d.com/cn/current/ScriptReference/Vector2.Lerp.html
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.GetComponent<Enemy>()!=null)
{
collision.GetComponent<Enemy>().FreezeTime(true);
CreateHotKey(collision);
}
}
private void CreateHotKey(Collider2D collision)
{
if(KeyCodeList.Count == 0)
{
return;
}
GameObject newHotKey = Instantiate(hotKeyPrefab, collision.transform.position + new Vector3(0, 2), Quaternion.identity);
KeyCode choosenKey = KeyCodeList[Random.Range(0, KeyCodeList.Count)];
KeyCodeList.Remove(choosenKey);
Blackhole_Hotkey_Controller newHotKeyScript = newHotKey.GetComponent<Blackhole_Hotkey_Controller>();
newHotKeyScript.SetupHotKey(choosenKey, collision.transform, this);
}
public void AddEnemyToList(Transform _myEnemy)
{
targets.Add(_myEnemy);
}
}