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 ...
随机推荐
- 将AD域漫游用户配置文件放在samba服务器中
书接上回https://www.cnblogs.com/jackadam/p/11448497.html 我们已经将linux服务器设置为域成员,启动samba服务后,已经实现了使用域账号验证,自动创 ...
- [LeetCode] 52. N-Queens II N皇后问题 II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- docker安装+docker-compose
1.docker安装 yum install -y yum-utils device-mapper-persistent-data yum-config-manager --add-repo http ...
- LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...
- web动静分离
1 动态资源和静态资源 动态资源:多次访问页面,原代码会发生改变,比如jsp 静态资源:多次访问页面,原代码不发生改变,比如html,css 2 动静分离 将动态资源(jsp)放在tomcat服务器中 ...
- Apache Kafka Producer For Beginners
在我们上一篇Kafka教程中,我们讨论了Kafka Cluster.今天,我们将通过示例讨论Kafka Producer.此外,我们将看到KafkaProducer API和Producer API. ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- 深度学习-LSTM与GRU
http://www.sohu.com/a/259957763_610300此篇文章绕开了数学公式,对LSTM与GRU采用图文并茂的方式进行说明,尤其是里面的动图,让人一目了然.https://zyb ...
- Go语言(基本数据类型)
Go语言中有丰富的数据类型,除了基本的整型.浮点型.布尔型.字符串外,还有数组.切片.结构体.函数.map.通道(channel)等.Go 语言的基本类型和其他语言大同小异. 基本数据类型 整型 整型 ...
- Linux进程的五个段
目录 数据段 代码段 BSS段 堆(heap) 栈 数据段 用来存放可执行文件中已初始化的全局变量,换句话说就是存放程序静态分配的变量和全局变量: 代码段 代码段是用来存放可执行文件的操作指令,也就是 ...