今天继续来完成卡组编辑界面的搭建。首先,玩家肯定想要知道一张卡的具体信息,所以先做个显示具体信息的。基本思路是:鼠标移到某张卡牌上->在右侧显示这张卡牌的信息。所以把脚本挂在卡牌预制体上。
先新建一个空对象,在下面加入图片(显示卡图),文本(显示攻击,防御,法抗,血量,费用,技能等等等等)
大概就是这样,嗯。然后为了实现鼠标检测,实现了一个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;
}
好!接下来看一下成果
完美!
下一篇我们来实现把卡牌添加到卡组并保存,顺便实现一个卡牌检索功能。