【Unity2D入门教程氵篇】简单制作一个弹珠游戏之制作场景④(设置不可破坏砖块,发布游戏设置)

学习目标:

 如果你看过我上一个文章就会发现图片好像有灰色的不可破坏的砖块,顺便教大伙怎么发布自己已经设置好的游戏


学习内容:

先把砖块的颜色改好

代码也不用改动直接拖上去,只需要给它一个新的标签

Block的脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Block : MonoBehaviour
{
    [SerializeField] AudioClip breakClip; //播放声音
    [SerializeField] GameObject impactVFX;
    Level level;

    [SerializeField] int hitsCount; //统计被击中几次

    [SerializeField] Sprite[]hitSprites; //分别是第一次被击中的Sprite,第二次,第三次
    private void Start()
    {
        CountBrealableBlocks();
    }

    private void CountBrealableBlocks()
    {
        level = FindObjectOfType<Level>();
        if (tag == "Breakable")
        {
            level.CountBreakBlocks();
        }
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ball") && tag == "Breakable")
        {
            HandleHit();
        }
    }

    private void HandleHit()
    {
        hitsCount++;
        int maxHits = hitSprites.Length + 1;
        if (hitsCount >= maxHits)
        {
            DestoryBlock(); //如果被击中的次数大于它最大的承受击中次数就直接破坏
            //Debug.Log(other.gameObject.name);
        }
        else
        {
            ShowNextHitSPrite(); //显示
        }
    }

    private void ShowNextHitSPrite() //显示下一张图片也就是击碎的Sprite
    {
        int spriteIndex = hitsCount - 1;
        if (hitSprites[spriteIndex] != null)
        {
            GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
        }
        else
        {
            Debug.LogError("Array out of Index");
        }
    }
    private void DestoryBlock()
    {
        FindObjectOfType<GameStatus>().AddToScore();
        AudioSource.PlayClipAtPoint(breakClip, Camera.main.transform.position);
        Destroy(gameObject);
        level.BlockDestory();
        TriggerSparklesVFX();
    }
    private void TriggerSparklesVFX()
    {
        GameObject sparkles = Instantiate(impactVFX, transform.position.normalized, transform.rotation);
        Destroy(sparkles,1f);
    }
}

游戏发布:

点一下Player Settings

然后Build And Run直接保存好目录即可

猜你喜欢

转载自blog.csdn.net/dangoxiba/article/details/124039308