自制Unity小游戏TankHero-2D(3)开始玩起来

时间:2023-03-08 21:19:58

自制Unity小游戏TankHero-2D(3)开始玩起来

我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm)这个游戏制作的。仅为学习Unity之用。图片大部分是自己画的,少数是从网上搜来的。您可以到我的github页面(https://github.com/bitzhuwei/TankHero-2D)上得到工程源码。

自制Unity小游戏TankHero-2D(3)开始玩起来

本篇主要记录金币、按钮、坦克工厂、小地图等小部件,让整个场景初步成为一个可玩的游戏。

在本篇在制作过程中,修改了前两篇的很多东西,算是对Unity更加熟悉了。

金币

玩家击毁一个敌方坦克,会敌方坦克所在位置会出现1个金币。金币可以用来升级玩家坦克的速度、武器等,也可以用来恢复生命。

自制Unity小游戏TankHero-2D(3)开始玩起来

自制Unity小游戏TankHero-2D(3)开始玩起来

金币有3个脚本。

Show Up控制金币的出现是从透明到全不透明的。

 public class ShowUp : MonoBehaviour {

     public float showUpSpeed = ;
private SpriteRenderer spriteRenderer; void Awake()
{
this.spriteRenderer = this.GetComponent<SpriteRenderer>();
var color = this.spriteRenderer.color;
this.spriteRenderer.color = new Color(color.r, color.g, color.b, );
} // Update is called once per frame
void Update () {
if (this.spriteRenderer == null) { return; } this.spriteRenderer.color = Color.Lerp(this.spriteRenderer.color, Color.white, this.showUpSpeed * Time.deltaTime); //Debug.Log(string.Format("A: {0}", this.spriteRenderer.color.a));
if (Mathf.Abs(Color.white.a - this.spriteRenderer.color.a) <= 0.02f)
{
this.spriteRenderer.color = Color.white;
this.spriteRenderer = null;
}
}

ShowUp.cs

Coin Info保存金币的价值。

注意:脚本中要保留一个Start或一个Update函数,否则在Inspector面板就不会显示脚本组件前面的勾选框了。

自制Unity小游戏TankHero-2D(3)开始玩起来

自制Unity小游戏TankHero-2D(3)开始玩起来

 public class CoinInfo : MonoBehaviour {

     public int value;

     void Start()
{
} }

Picked Coin让金币碰到玩家坦克时销毁自己。

 public class PickedCoin : MonoBehaviour {

     private bool picked;

     void Awake()
{
this.picked = false;
} void Start () { } void OnTriggerEnter2D(Collider2D other)
{
if (other.tag != Tags.hero) { return; } if (!this.picked)
{
this.picked = true;
MonoBehaviour.Destroy(this.gameObject);
}
}
}

游戏暂停和继续

用一个按钮来控制游戏的暂停和继续。

选择UI-Button即可添加一个按钮。

自制Unity小游戏TankHero-2D(3)开始玩起来

在按钮的Button组件中,添加一个btnPause.cs脚本,添加一个On Click(),选择这个btnPause.cs脚本组件,选择对应的事件函数即可。(下图是错的,应该把btnPause.cs组件赋给On Click项。

自制Unity小游戏TankHero-2D(3)开始玩起来

那么事件函数怎么写呢?

游戏暂停的原理很简单,只需 Time.timeScale = ; ,那么今后所有的 Time.deltaTime 都将是0。因此所有乘以 Time.deltaTime 的地方都不会再有进展。

     private float originalTimeScale;
private UnityEngine.UI.Text buttonText; void Awake()
{
this.originalTimeScale = Time.timeScale;
this.buttonText = this.GetComponentInChildren<UnityEngine.UI.Text>();
} public void btnPause_Click()
{
if (Time.timeScale > )
{
Time.timeScale = ;
buttonText.text = "Continue";
}
else
{
Time.timeScale = this.originalTimeScale;
buttonText.text = "Pause";
}
}

btnPause

在激烈的游戏过程中,把鼠标挪到屏幕某处点击按钮是很费劲的。所以,添加一个按下Space键就可以暂停或继续游戏的功能很有必要。只需给刚刚的btnPause.cs脚本添加如下代码。

自制Unity小游戏TankHero-2D(3)开始玩起来

     void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
btnPause_Click();
}
}

坦克工厂

现在可以在界面上方三个点产生敌方坦克。后续我将此处改造为关卡控制器。此处暂时没什么可说的。

小地图

整个游戏地图有的大,一屏显示不完,所以给个能显示全地图的小地图是很好的。

制作小地图的原理是再添加一个摄像机smallMap,确保其Depth大于主摄像机。这样小地图就会显示在主场景上层。调整smallMap的Viewport属性,使其只在界面的某个角落显示。这里我让小地图显示在场景左下角,其长宽均为场景的五分之一即0.2。

注意,Viewport的X、Y、Width、Height属性都是0~1的,表示的是百分比。

自制Unity小游戏TankHero-2D(3)开始玩起来

注意,上图左上方红色围起来的白色框,其长宽比=下方Game视图的长宽比,后面我就是根据这个调整小地图的长宽的。

完成后,在Game视图里是这样的,小地图并不是正方形。这不好。

自制Unity小游戏TankHero-2D(3)开始玩起来

不过这个问题我用脚本解决了,实际上是调整了摄像机的Viewport的长宽属性,使之调整到相同的长度。

自制Unity小游戏TankHero-2D(3)开始玩起来

脚本如下。思路是,当场景的width大于height时,要缩小小地图的width;当场景的width小于height时,要缩小小地图的height。

 public class AdjustViewPort : MonoBehaviour {

     Camera cameraComponent;
private float screenWidth;
private float screenHeight;
private Rect originalCameraRect; void Awake()
{
this.cameraComponent = this.GetComponent<Camera>();
this.originalCameraRect = this.cameraComponent.rect;
} void Update () {
var width = Screen.width;
var height = Screen.height;
if (width == this.screenWidth && height == this.screenHeight) { return; } this.screenWidth = width;
this.screenHeight = height; if (width > height)
{
var rect = this.cameraComponent.rect;
rect.width = this.originalCameraRect.width * ((float)height / (float)width);
this.cameraComponent.rect = rect;
}
else
{
var rect = this.cameraComponent.rect;
rect.height = this.originalCameraRect.height * ((float)width / (float)height);
this.cameraComponent.rect = rect;
}
}
}

AdjustViewport.cs

只显示小地图的话,可能会跟场景混淆,所以给小地图加个红色的边框,就区分得明显了。我搜了很多加边框的方法,发现都太繁琐,还要依赖各种包、库。还是直接画一个Texture简单。

为方便起见,就在刚刚的AdjustViewPort脚本中同时绘制边框好了。

自制Unity小游戏TankHero-2D(3)开始玩起来

所需的边框纹理就是一个内部透明四周为红色的PNG图片。

自制Unity小游戏TankHero-2D(3)开始玩起来

(下面的脚本忽略了调整长宽相关的部分。)

 public class AdjustViewPort : MonoBehaviour {

     Camera cameraComponent;
public Texture borderTexture; void Awake()
{
this.cameraComponent = this.GetComponent<Camera>();
} void OnGUI()
{
var rect = this.cameraComponent.rect;
float left = ;
float top = Screen.height - Screen.height * rect.height;
float width = Screen.width * rect.width;
float height = Screen.height * rect.height; GUI.DrawTexture(new Rect(left, top, width, height), this.borderTexture, ScaleMode.StretchToFill);
}
}

总结

本篇添加了一些虽小但用起来很方便的小部件。现在这个TankHero就算是可以玩了。下面我将设计实现关卡,让这个游戏具有多个关卡,并且可配置。

您可以到我的github页面(https://github.com/bitzhuwei/TankHero-2D)上得到工程源码。

请多多指教~