unity卡牌游戏开发记录(3)

今天继续来完成卡组编辑界面的搭建。首先,玩家肯定想要知道一张卡的具体信息,所以先做个显示具体信息的。基本思路是:鼠标移到某张卡牌上->在右侧显示这张卡牌的信息。所以把脚本挂在卡牌预制体上。

先新建一个空对象,在下面加入图片(显示卡图),文本(显示攻击,防御,法抗,血量,费用,技能等等等等)

大概就是这样,嗯。然后为了实现鼠标检测,实现了一个IPointerEnterHandler接口。接口的原理我也不是很清楚,大概就是在monobehaviour后面加上接口名称,然后编辑器报错,提示实现接口,点了之后会增加一个函数,在里面写触发内容就行了。注意,里面的OnMouseOver也是检测鼠标,但是不能用于ui。

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

public class show_in_deckedit : MonoBehaviour, IPointerEnterHandler
{
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnMouseOver()
    {
        Debug.Log("1");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("1");
        //throw new System.NotImplementedException();
    }
}

接下来把脚本挂到卡组的card预制体上,运行游戏,在加载卡组出来后,把鼠标移到卡牌上,log界面显示1,成功。

实现了鼠标检测,接下来当然是要把数据放到上面。修改一下上次写的代码,让代码在读取数据的时候把其他的数据保存下来。为了方便管理,我新建了一个脚本saveCardInformation如下,挂在卡牌预制体上,里面只有卡牌相关的变量。这样子我就可以用其他的脚本把数据保存在这个脚本中。

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

public class saveCardInformation : MonoBehaviour
{

    public int id;
    public int cost;
    public string cardname;
    public double attack;
    public double defense;
    public float health;
    public float max_health;
    public double fakang;//法术抗性
    public string birth;//出身地
    public string force;//所属势力
    public string race;//种族
    public string career;//职业
    public string description;//技能描述
    public UnityEngine.UI.Image Character;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

接下来修改上次写的代码,首先是获取图片。unity读取图片的思路基本上就是根据id读取文件->获取byte信息->把byte信息转为texture2d文件->把texture2d文件转为sprite文件->赋值给image的sprite属性

private static Sprite GetImageByte(int id,string imagePath)
{
    //读取到文件
    string path = imagePath;
    FileStream files = new(path, FileMode.Open);
    //新建比特流对象
    byte[] imgByte = new byte[files.Length];
    //将文件写入对应比特流对象
    files.Read(imgByte, 0, imgByte.Length);
    //关闭文件
    files.Close();
    //返回比特流的值
    Texture2D texture = new Texture2D(1, 1);
    texture.LoadImage(imgByte);
    Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    return sprite;
}

这段代码返回的sprite可以直接赋值给image组件的sprite。我的做法是新建一个空物体,给它附上一个image组件来接收

GameObject cardimage = new("Image");
cardimage.AddComponent<Image>();
cardimage.GetComponent<Image>().sprite = GetImageByte(id,path_root);

之后我写了一个函数来保存从文件中读取到的其他信息。saveCardInformation是刚刚新建的存储脚本。这样子就可以把数据保存在实例化的卡牌下面。

void saveinformation(GameObject a,int _id, int _cost, string _name, double _attack, double _defense, float _health, float _fakang, string _birth, string _career, string _force, string _race, string _des, UnityEngine.UI.Image _ch)
{
    a.GetComponent<saveCardInformation>().id = _id;
    a.GetComponent<saveCardInformation>().cost = _cost;
    a.GetComponent <saveCardInformation>().cardname = _name; 
    a.GetComponent<saveCardInformation>().attack = _attack;
    a.GetComponent<saveCardInformation>().defense = _defense;
    a.GetComponent<saveCardInformation>().health = _health;
    a.GetComponent<saveCardInformation>().fakang = _fakang;
    a.GetComponent<saveCardInformation>().birth = _birth;
    a.GetComponent<saveCardInformation>().career = _career;
    a.GetComponent<saveCardInformation>().force = _force;
    a.GetComponent<saveCardInformation>().race = _race;
    a.GetComponent<saveCardInformation>().description = _des;
    a.GetComponent<saveCardInformation>().Character = _ch;
}

接下来回到我们刚刚写的检测鼠标的脚本,增加一个函数用来显示卡牌信息,放到OnPointerEnter函数中。

void showbigcard()
{
    //List<Card_information> card_informations_list;
    //card_informations_list = showcard.card_informations_list;
    //根据脚本挂载的物体来显示对应数据
    BigCard.transform.Find("attack").GetComponent<TextMeshProUGUI>().text = this.transform.Find("attack").GetComponent<TextMeshProUGUI>().text;
    BigCard.transform.Find("defense").GetComponent<TextMeshProUGUI>().text = this.transform.Find("defense").GetComponent<TextMeshProUGUI>().text;
    BigCard.transform.Find("fakang").GetComponent<TextMeshProUGUI>().text = this.transform.Find("fakang").GetComponent<TextMeshProUGUI>().text;
    BigCard.transform.Find("health").GetComponent<TextMeshProUGUI>().text = this.transform.Find("health").GetComponent<TextMeshProUGUI>().text;
    BigCard.transform.Find("career").GetComponent<TextMeshProUGUI>().text = this.GetComponent<saveCardInformation>().career;
    BigCard.transform.Find("birth").GetComponent<TextMeshProUGUI>().text = this.GetComponent<saveCardInformation>().birth;
    BigCard.transform.Find("race").GetComponent <TextMeshProUGUI>().text= this.GetComponent<saveCardInformation>().race;
    BigCard.transform.Find("force").GetComponent<TextMeshProUGUI>().text =this.GetComponent<saveCardInformation>().force;
    BigCard.transform.Find("CardImage").GetComponent<Image>().sprite = this.transform.Find("Character_image").GetComponent<Image>().sprite;
    BigCard.transform.Find("CardSkill").GetComponent<TextMeshProUGUI>().text = this.GetComponent<saveCardInformation>().description;
}

好!接下来看一下成果

完美!

下一篇我们来实现把卡牌添加到卡组并保存,顺便实现一个卡牌检索功能。

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity卡牌游戏开发通常需要以下几个步骤。首先,需要确定需求,包括需要一个存放Sprite的数组用于赋值,并修改代码以随机生成卡牌的封面。其次,新建一个空物体并创建一个名为SceneController的脚本。在这个脚本中,我们可以定义卡牌的预制体和一些参数,如最大卡牌数、每行最大卡牌数和相邻卡牌的间隔。接着,在createCards()方法中使用Instantiate函数实例化卡牌预制体,并按照设定的间隔位置将卡牌分布在场景中。最后,在Start()方法中调用createCards()方法来生成卡牌[2]。 在这个过程中,我们选择了使用SceneController动态生成一个数组来给不同的卡牌赋值的方法。这样做的好处是不同的卡牌只有图片一个属性不同,通过动态生成数组来给卡牌赋值可以避免一个个手动拖拽预制体到对应的位置上赋值的麻烦。这种方法简化了开发过程,提高了开发效率。 总结起来,Unity卡牌游戏开发可以通过确定需求、修改代码以随机生成卡牌封面、创建一个SceneController脚本来管理卡牌的生成和赋值。这样的开发流程能够简化开发过程并提高开发效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Unity实现简单卡牌游戏框架](https://blog.csdn.net/LuLou_/article/details/115191300)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值