此方法只是用于学习和实验所以细节不必要求

一、Ui设置。

画布配置如下:

布局:

说明:

画布里面首先建立一个panel命名为weapon1,在其内部再建立4个panel用于装备的卡槽,装备以image来显示,并且添加自定义hp脚本(hp脚本主要配置物品的名称、稀有度、说明)。message主要用来显示武器信息。image配置如下:

weapon1配置此处使用两种方法来检测鼠标事件(1.直接添加event trigger组建绑定函数  2.在dragManager代码中监听):

二、代码。

dragManager

//继承接口实现鼠标进入和移除事件
public class dragManager : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
//定义偏移量,用来记录鼠标和装备图片的偏移
public Vector3 offet;
//提示武器信息即message
public GameObject g;
//是否拖放成功
private bool massger; public Action a;
public Action b;
//拖拽武器
public GameObject game;
// Start is called before the first frame update
void Start()
{ } // Update is called once per frame
void Update()
{
if (a != null)
a();
// print(game.activeSelf);
}
//开始拖拽确定物体
public void beginDrag()
{
for (int i = ; i < transform.childCount; i++)
{
RectTransform rect =(RectTransform) transform.GetChild(i);
if (rect.rect.Contains(Input.mousePosition - rect.position) && rect.GetChild().gameObject.activeSelf)
{
offet = Input.mousePosition - rect.position;
g = rect.GetChild().gameObject;
return;
}
}
}
//释放
public void endDrag()
{
massger = false;
for (int i = ; i < transform.childCount; i++)
{
RectTransform rect = (RectTransform)transform.GetChild(i);
if (rect.rect.Contains(Input.mousePosition - rect.position) && !rect.GetChild().gameObject.activeSelf && g!=null)
{
rect.GetChild().GetComponent<Image>().sprite=g.GetComponent<Image>().sprite;
g.GetComponent<UnityEngine.UI.Image>().sprite = null;
g.gameObject.SetActive(false);
rect.GetChild().gameObject.SetActive(true);
massger = true;
g.GetComponent<RectTransform>().localPosition = Vector3.zero;
g = null;offet = Vector3.zero;
return;
}
}
if (!massger && g!=null)
{
g.GetComponent<RectTransform>().localPosition = Vector3.zero;
g = null; offet = Vector3.zero;
}
}
//拖拽中
public void draging()
{
if (g != null)
{
g.GetComponent<RectTransform>().position = Input.mousePosition - offet;
}
}
//鼠标进入
public void OnPointerEnter(PointerEventData eventData)
{
a += NewMethod;
} private void NewMethod()
{
for (int i = ; i < transform.childCount; i++)
{
RectTransform rect = (RectTransform)transform.GetChild(i);
if (rect.rect.Contains(Input.mousePosition - rect.position) && rect.GetChild().gameObject.activeSelf)
{
Text t = game.transform.GetChild().GetComponent<Text>();
hp h= rect.GetChild().GetComponent<hp>();
string s= "<color=" + h.color + ">"+ h.name +"</color>\n"+h.description;
t.text =s.ToString(); break;
}
}
game.transform.position = new Vector3(Input.mousePosition.x + game.GetComponent<RectTransform>().rect.width / ,
Input.mousePosition.y - game.GetComponent<RectTransform>().rect.height / , );
if(!game.activeSelf)game.SetActive(true);
} //鼠标移出
public void OnPointerExit(PointerEventData eventData)
{
b += NewMethod2;
b();
}
private void NewMethod2()
{
game.SetActive(false);
b = null;
a = null;
}
}

三、运行结果有点丑,但只是实验用。

四、解决个别问题。屏幕放大图片信息一直闪烁。

将text的Raycast Target取消勾选。

unity 之 背包系统的更多相关文章

  1. Siki_Unity_3-3_背包系统

    Unity 3-3 背包系统(基于UGUI) 任务1&2&3:演示.介绍.类图分析 背包面板.箱子面板.锻造合成面板.装备佩戴面板.商店面板等 面板的显示和隐藏.保存和加载.拾起物品. ...

  2. unity游戏设计之背包系统

    这次任务是模仿上图的样子,制作一个类似的背包系统. 上面的链接为:http://www.tasharen.com/ngui/exampleX.html 我们的目标是: 1.实现背包系统的UI界面 2. ...

  3. Unity 背包系统的完整实现(基于MVC框架思想)

    前言: 项目源码上传GitHub:Unity-knapsack 背包系统: 背包系统是游戏中非常重要的元素,几乎每一款游戏都有背包系统,我们使用背包系统可以完成装备栏的数据管理,商店物体的数据管理等等 ...

  4. 【Unity】基于MVC模式的背包系统 UGUI实现

    前言 本文基于MVC模式,用UGUI初步实现了背包系统. Control层包括了点击和拖拽两种逻辑. 博文首发:http://blog.csdn.net/duzixi 下载地址:https://git ...

  5. [Unity3D]Unity资料大全免费分享

     都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按  ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...

  6. NGUI简单背包系统的实现

    一.利用txt文件存储游戏物品信息 首先在asset下创建一个txt文件,这里我们命名为objectsInfoList.txt,并将其拖放到unity Project视图中. 其中txt中我们先存放一 ...

  7. 【UGUI】 (三)------- 背包系统(上)之简易单页背包系统及检索功能的实现

    背包系统,无论是游戏还是应用,都是常常见到的功能,其作用及重要性不用我多说,玩过游戏的朋友都应该明白. 在Unity中实现一个简易的背包系统其实并不是太过复杂的事.本文要实现的是一个带检索功能的背包系 ...

  8. 一些值得学习的Unity教程 (很实用的包括源码)

    ***********************项目源码******************************** 1. 降临 2. 沉睡缤纷乐 3. 千炮捕鱼 4. Photon官方FSP示例 ...

  9. Unity从实践中学习(1)

    首先在实际中unity的开发之中快捷键应该是相当重要的一个部分,这里先引用csdn的一个博客,https://blog.csdn.net/qq_34552886/article/details/697 ...

随机推荐

  1. node端代理浏览器路由 解决浏览器跨域问题

    var _ = require('lodash'); var request = require("request"); /* @LM 2017-02-16 node端代理浏览器路 ...

  2. [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. zabbix解决中文乱码

    解决中文乱码 yum install -y wqy-microhei-fonts #解决方法 中文乱码 \cp /usr/share/fonts/wqy-microhei/wqy-microhei.t ...

  4. 【视频开发】【CUDA开发】FFMPEG硬件加速-nvidia方案

    1.目标 <1>显卡性能参数: <2>方案可行性: 2.平台信息 2.1.查看当前显卡信息 命令:  lspci |grep VGA  信息:  01:00.0 VGA com ...

  5. ATSC/DVB/ISDB三大标准比较

    一.引言  众所周知,模拟电视有NTSC.PAL和SECAM三种标准.目前,数字电视也陷入这种局面,美国.欧洲和日本各自形成三种不同的数字电视标准.美国的标准是ATSC(Advanced Televi ...

  6. 【python小记】访问mysql数据库

    题记: 最近因为工作需要,学习了python,瞬间对这个轻松快捷的语给吸引了,以前只知道js脚本是写网页的,没有想到python这个脚本语言的应用范围可以这么广泛,现在做一些简单或稍微复杂的操作,基本 ...

  7. 第一二章、C++简介和基本语法

    C++简介 C++包含了三种编程模式,分别是面向过程.面向对象和泛型编程. 程序执行过程 C++编程格式 C++每个程序都会有main()函数作为入口,基本编程包括预处理/头文件.函数.输入输出语句等 ...

  8. 通过names.index()方法找到第2个eva值 ,并将其改成EVA

    names= ['alex','rain','peiqi','eva','mac','jack','eva','kangkang','jain']first_index=names.index('ev ...

  9. AttributeError: 'sys.flags' object has no attribute 'utf8_mode'

    AttributeError: 'sys.flags' object has no attribute 'utf8_mode' pycharm工程的py版本是3.6,结果即使使用py3.7编译后的py ...

  10. DFS集训

    2019-07-29 09:01:06 A PARTY A company has n employees numbered from 1 to n. Each employee either has ...