Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
Entity.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EntityFX : MonoBehaviour
{
private SpriteRenderer sr;//定义SR组件来保持要用的组件
[Header("Flash FX")]
[SerializeField] private Material hitMat;//要改成的材料
[SerializeField] private float flashDuration;//闪光的时间
private Material originalMat;//原来的材料
[Header("Aliment colors")]
[SerializeField] private Color[] chillColor;
[SerializeField] private Color[] igniteColor;
[SerializeField] private Color[] shockColor;
[Header("Aliment particles")]
[SerializeField] private ParticleSystem igniteFX;
[SerializeField] private ParticleSystem chillFX;
[SerializeField] private ParticleSystem shockFX;
private void Start()
{
sr = GetComponentInChildren<SpriteRenderer>();//从子组件中拿到SR组件
originalMat = sr.material;//拿到原来的材料
}
public void MakeTransprent(bool isClear)
{
if (isClear)
sr.color = Color.clear;
else
sr.color = Color.white;
}
private IEnumerator FlashFX()//被打后该触发的函数
{
sr.material = hitMat;
//修复在元素效果期间击中,颜色变红的情况
Color currentColor = sr.color;
sr.color = Color.white;
yield return new WaitForSeconds(flashDuration);
sr.color = currentColor;
sr.material = originalMat;
} //IEnumertor本质就是将一个函数分块执行,只有满足某些条件才能执行下一段代码,此函数有StartCoroutine调用
//https://www.zhihu.com/tardis/bd/art/504607545?source_id=1001
private void RedColorBlink()//使角色闪烁的函数
{
if (sr.color != Color.white)
{
sr.color = Color.white;
}
else
{
sr.color = Color.red;
}
}
private void CancelColorChange()//使角色停止闪烁的函数
{
CancelInvoke();//取消该 MonoBehaviour 上的所有 Invoke 调用。
//https://docs.unity3d.com/cn/current/ScriptReference/MonoBehaviour.CancelInvoke.html
sr.color = Color.white;
igniteFX.Stop();
chillFX.Stop();
shockFX.Stop();
}
public void ShockFxFor(float _second)
{
shockFX.Play();
InvokeRepeating("ShockColorFx", 0, .3f);
Invoke("CancelColorChange", _second);
}
public void ChillFxFor(float _second)
{
chillFX.Play();
InvokeRepeating("ChillColor", 0, .3f);
Invoke("CancelColorChange", _second);
}
public void IgniteFxFor(float _second)
{
igniteFX.Play();
InvokeRepeating("IgniteColorFX", 0, .3f);
Invoke("CancelColorChange", _second);
}
private void IgniteColorFX()
{
if (sr.color != igniteColor[0])
sr.color = igniteColor[0];
else
sr.color = igniteColor[1];
}
private void ShockColorFx()
{
if (sr.color != shockColor[0])
sr.color = shockColor[0];
else
sr.color = shockColor[1];
}
private void ChillColor()
{
if (sr.color != chillColor[0])
sr.color = chillColor[0];
else
sr.color = chillColor[1];
}
}