【UGUI】背包案例(六)
程序员文章站
2022-05-30 17:42:35
...
实现道具信息的介绍
背包中一个较为重要的功能了,介绍板,用来展示道具的信息
- 先做这样一个Ui面板,然后在它下面创建一个Text
- 在Inspector面板将他们Text组件和Image组件上的Raycast Target,全部勾选false掉
- 将这个Information面板放到Canvas的最下层,这样就不会被别的UI挡住
- 在我们的Prpo类里面添加代码
继承者两个接口IPointerEnterHandler, IPointerExitHandler
- 实现接口后,就可以添加逻辑代码了
/// <summary>
/// 当鼠标移到上面
/// </summary>
/// <param name="eventData"></param>
public void OnPointerEnter(PointerEventData eventData)
{
//如果装备没有被拖拽
if (!isDrag)
{
PrpoManager.instance.informationPanel.SetActive(true);
//面板位置等于鼠标位置
PrpoManager.instance.informationPanel.transform.position = Input.mousePosition;
int t_id = int.Parse(GetComponent<Image>().sprite.name);
string t_name = PrpoManager.instance.prpos[t_id].p_name;
int t_price = PrpoManager.instance.prpos[t_id].p_price;
string t_property = PrpoManager.instance.prpos[t_id].p_property;
string t_point = "null";
switch (PrpoManager.instance.prpos[t_id].p_wearPositionType)
{
case WearPositionType.DEFAULT:
t_point = "null";
break;
case WearPositionType.HEAD:
t_point = "头";
break;
case WearPositionType.FACE:
t_point = "脸";
break;
case WearPositionType.HAND:
t_point = "手";
break;
case WearPositionType.WAIST:
t_point = "腰";
break;
case WearPositionType.LEG:
t_point = "腿";
break;
case WearPositionType.FOOT:
t_point = "脚";
break;
default:
break;
}
PrpoManager.instance.informationPanel.GetComponentInChildren<Text>().text = "名字:" + t_name + "\n价格:" + t_price + "\n属性:" + t_property + "\n穿戴位置:" + t_point;
}
}
/// <summary>
/// 当鼠标离开
/// </summary>
/// <param name="eventData"></param>
public void OnPointerExit(PointerEventData eventData)
{
PrpoManager.instance.informationPanel.SetActive(false);
}
- 这里面读取的信息,在我们之前写好的PrpoManager里面
PrpoManager类的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 道具管理类,文件信息存储取出
/// </summary>
public class PrpoManager : MonoBehaviour
{
/// <summary>
/// 单例模式
/// </summary>
public static PrpoManager instance;
#region//私有的信息
/// <summary>
/// 接收Resources文件里的图片
/// </summary>
private List<Sprite> sprites;
/// <summary>
/// 将图片与ID对应储存
/// </summary>
private Dictionary<int, Sprite> keyValueSprites;
/// <summary>
/// 将信息文件与ID对应
/// </summary>
private Dictionary<int, PrpoInformation> keyValuePrpoInformations;
#endregion
/// <summary>
/// 储存所有实例化的道具对象
/// </summary>
public Dictionary<int,Prpos> prpos;
/// <summary>
/// 序列化的文件中,存放我们设定好的属性
/// </summary>
public List<PrpoInformation> prpoInformation;
/// <summary>
/// 背包Canvas在面板拖拽进来
/// </summary>
public Canvas m_canvas;
/// <summary>
/// 信息面板
/// </summary>
public GameObject informationPanel;
private void Awake()
{
instance = this;
}
void Start()
{
sprites = new List<Sprite>();
sprites.AddRange(Resources.LoadAll<Sprite>("UI"));
keyValueSprites = new Dictionary<int, Sprite>();
foreach (var item in sprites)
{
keyValueSprites.Add(int.Parse(item.name), item);
}
keyValuePrpoInformations = new Dictionary<int, PrpoInformation>();
foreach (var item in prpoInformation)
{
PrpoInformation PrpoInfor = new PrpoInformation();
PrpoInfor = item;
keyValuePrpoInformations.Add(item.id, PrpoInfor);
}
CreatPrpo();
}
// Update is called once per frame
void Update()
{
}
private void CreatPrpo()
{
prpos = new Dictionary<int, Prpos>();
foreach (var item in keyValueSprites)
{
int p_id = keyValuePrpoInformations[item.Key].id;
int p_price = keyValuePrpoInformations[item.Key].price;
string p_name = keyValuePrpoInformations[item.Key].name;
string p_property = keyValuePrpoInformations[item.Key].property;
PrpoType p_prpoType = keyValuePrpoInformations[item.Key].prpoType;
WearPositionType p_wearPositionType = keyValuePrpoInformations[item.Key].wearPositionType;
Prpos prpo = new Prpos(p_id, p_name, p_price, p_property, item.Value, p_prpoType, p_wearPositionType);
prpos.Add(p_id,prpo);
}
foreach (var item in prpos)
{
//Debug.Log(item.Value.p_id+ item.Value.p_name);
}
}
[System.Serializable]
public class PrpoInformation
{
public int id;
public string name;
public int price;
public string property;
public PrpoType prpoType;
public WearPositionType wearPositionType;
}
}
目前为止,已经实现了拖拽、整理、购买、出售、丢弃、合并和信息展示,只剩下使用道具和装备道具了
推荐阅读
-
javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful
-
让您创意不断的六个超实用方法 附上真实案例
-
通过实例学习Flash AS3.0——案例六
-
六大案例看内容营销六字箴言
-
JS案例六_1:添加城市
-
案例六_2:省市级联动
-
大数据之Zookeeper(六)-Zookeeper的API应用&监听服务器节点动态上下线案例
-
XENIRO 讯琥科技应用案例(六)| 边缘计算和区块链推动智慧农业升级
-
Metal(六) 案例之视频文件的渲染
-
javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful