您当前的位置: 首页 >  dangoxiba 游戏

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

dangoxiba 发布时间:2022-04-08 13:40:18 ,浏览量:4

学习目标:

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

学习内容:

先把砖块的颜色改好

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

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();
        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().sprite = hitSprites[spriteIndex];
        }
        else
        {
            Debug.LogError("Array out of Index");
        }
    }
    private void DestoryBlock()
    {
        FindObjectOfType().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直接保存好目录即可

关注
打赏
查看更多评论

dangoxiba

暂无认证

  • 4浏览

    0关注

    55博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录