unity 之 背包系统
此方法只是用于学习和实验所以细节不必要求
一、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 之 背包系统的更多相关文章
- Siki_Unity_3-3_背包系统
Unity 3-3 背包系统(基于UGUI) 任务1&2&3:演示.介绍.类图分析 背包面板.箱子面板.锻造合成面板.装备佩戴面板.商店面板等 面板的显示和隐藏.保存和加载.拾起物品. ...
- unity游戏设计之背包系统
这次任务是模仿上图的样子,制作一个类似的背包系统. 上面的链接为:http://www.tasharen.com/ngui/exampleX.html 我们的目标是: 1.实现背包系统的UI界面 2. ...
- Unity 背包系统的完整实现(基于MVC框架思想)
前言: 项目源码上传GitHub:Unity-knapsack 背包系统: 背包系统是游戏中非常重要的元素,几乎每一款游戏都有背包系统,我们使用背包系统可以完成装备栏的数据管理,商店物体的数据管理等等 ...
- 【Unity】基于MVC模式的背包系统 UGUI实现
前言 本文基于MVC模式,用UGUI初步实现了背包系统. Control层包括了点击和拖拽两种逻辑. 博文首发:http://blog.csdn.net/duzixi 下载地址:https://git ...
- [Unity3D]Unity资料大全免费分享
都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按 ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...
- NGUI简单背包系统的实现
一.利用txt文件存储游戏物品信息 首先在asset下创建一个txt文件,这里我们命名为objectsInfoList.txt,并将其拖放到unity Project视图中. 其中txt中我们先存放一 ...
- 【UGUI】 (三)------- 背包系统(上)之简易单页背包系统及检索功能的实现
背包系统,无论是游戏还是应用,都是常常见到的功能,其作用及重要性不用我多说,玩过游戏的朋友都应该明白. 在Unity中实现一个简易的背包系统其实并不是太过复杂的事.本文要实现的是一个带检索功能的背包系 ...
- 一些值得学习的Unity教程 (很实用的包括源码)
***********************项目源码******************************** 1. 降临 2. 沉睡缤纷乐 3. 千炮捕鱼 4. Photon官方FSP示例 ...
- Unity从实践中学习(1)
首先在实际中unity的开发之中快捷键应该是相当重要的一个部分,这里先引用csdn的一个博客,https://blog.csdn.net/qq_34552886/article/details/697 ...
随机推荐
- Delphi ADOConnection连接
unit Unit_DM; interface usesSysUtils, Classes, DB, ADODB,inifiles,windows,forms,controls; typeTDM = ...
- springboot:redis反序列化发生类型转换错误
明明是同一个类,在反序列时报类型转换错误,真实奇怪.经查找资料,说是引入了devtools的缘故. 注释掉以下内容: <dependency> <groupId>org.spr ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- mysql执行计划详解,
一.语法 explain SQL语句 例如: explain ; 二.explain输出解释 +----+-------------+-------+-------+----------------- ...
- python json模块(15)
JSON 的全称是 JavaScript Object Notation,即 JavaScript 对象符号,它是一种轻量级.跨平台.跨语言的数据交换格式,其设计意图是把所有事情都用设计的字符串来表示 ...
- 运行报错:'_TestResult' object has no attribute 'outputBuffer'
一.运行main函数,未生成测试报告,报错:'_TestResult' object has no attribute 'outputBuffer' 解决方式: 1.在HTMLTestReportCN ...
- PHP字符串替换
$pid = str_replace(',',',',$pid); $pid = str_replace(' ','',$pid); $pid = str_replace(array(',', ' ' ...
- npm包的语义版本控制(Semantic Versioning of Packages)
本文删改自Node.js 8 the Right Way Part I Chapter 3 npm 使用语义版本控制(SemVer)来寻找包的最佳可用兼容版本. 以安装测试框架mocha为例 $ ...
- Mysql中的读锁,写锁,乐观锁及事务隔离级别和并发问题
mysql读锁,写锁,乐观锁 读锁,也叫共享锁(shared lock) SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE 写锁,也叫排他 ...
- docker自动化脚本
使用脚本从git上拉取项目并运行, 有些不足的地方 编写脚本 run.sh 如果用到redis和myslq,要先启动redis和mysql #!/bin/bash # author:qiao # 更新 ...