找回密码
 立即注册
查看: 586|回复: 0

unity—卡牌翻转效果(2)

[复制链接]
发表于 2021-3-28 15:10 | 显示全部楼层 |阅读模式
上一篇的卡片翻转效果在应用中有一些问题,即当点击非卡牌区域,卡牌依旧会翻转。

这一篇的效果就是只有点击卡牌区域时才能翻转卡牌,与上一篇的制作方式不太相同。

将图片导入unity,这个图片是正面和反面在一起的,中间有一些透明像素,便于后面切割。

更改设置,如图2、3

设置完后,点击右侧的sprite editor按钮。

点击左上方slice(切割)-->slice,再点击左右两边的图片在4处重新命名,之后点击右上角Apply,再关闭窗口

在资源面板上,把图片的正面拖到左侧的面板上。

点击图片,给图片添加box collider,勾选is trigger

新建C#代码,内容如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class card : MonoBehaviour

{

        private SpriteRenderer rend;

        [SerializeField]

        private Sprite faceSprite,backSprite;

        private bool coroutineAllowed,facedUp;

  // Start is called before the first frame update

  void Start()

  {

                rend=GetComponent<SpriteRenderer>();

                rend.sprite=faceSprite;

                coroutineAllowed=true;

                facedUp=true;

  }

        private void OnMouseDown(){

                if(coroutineAllowed){

                        StartCoroutine(RotateCard());

                }

        }

        private IEnumerator RotateCard(){

                coroutineAllowed=false;

                if(!facedUp){

                        for(float i=0f;i<=180f;i+=10f){

                                transform.rotation=Quaternion.Euler(0f,i,0f);

                                if(i==90f){

                                        rend.sprite=faceSprite;

                                }

                                yield return new WaitForSeconds(0.01f);

                        }

                }

                else if (facedUp){

                        for (float i=180f;i>=0f;i-=10f){

                                transform.rotation=Quaternion.Euler(0f,i,0f);

                                if(i==90f){

                                        rend.sprite=backSprite;

                                }

                                yield return new WaitForSeconds(0.01f);

                        }

                }

                coroutineAllowed=true;

                facedUp=!facedUp;

        }

  // Update is called once per frame

  void Update()

  {

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-23 13:23 , Processed in 0.125020 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表