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 ...
随机推荐
- 【翻译】Flink Table Api & SQL — 内置函数
本文翻译自官网:Built-In Functions https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/fu ...
- JavaScript和Jquery个人笔记
目录 前言 价格 * 数量 = 金额 js计算时间差值 判断敲回车或Shift+回车 js控制textarea换行 $(this)选择当前元素 前端调试禁止其他js js添加a标签href属性和文本 ...
- 一起学习log4cxx
目前成熟的日志系统有很多,比如log4cxx,log4cpp等,今天一起来学习log4cxx吧,之所以学习这个,首先,这个日志库比较成熟,一直由apach基金在维护,而log4cpp缺乏维护.再者,这 ...
- win10 解决Failed installing 'Tomcat8' service
今天将tomcat安装成服务执行service.bat install时遇到 Installing the service 'Tomcat8' ... Using CATALINA_HOME: &qu ...
- 使用ObjectARX 2012向导生成的自定义实体类无法捕捉的问题解决方式
使用ObjectARX 2012自定实体向导的时候,获取捕捉点的重写函数的向导中模板有点问题, C:\Program Files (x86)\Autodesk\ObjectARX 2019 Wizar ...
- leetcode 55 Jump Game 三种方法,回溯、动态规划、贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- STM32F030-UART1_DMA使用提示
STM32F030-UART1_DMA使用提示 前言: 今天把STM32F030C8T6的串口DMA学习了一下,为了加快各位研发人员的开发进度,避免浪费大量的时间在硬件平台上,写出个人代码调试的经验. ...
- jQuery框架"风云榜"案例
<title>电影风云榜</title> <style> /*清空默认样式*/ *{padding:0;margin:0;border:0;list-style:n ...
- Sql CLR创建一个简单的表值函数
1.创建面目: 2. 添加函数代码: using System; using System.Data.Sql; using Microsoft.SqlServer.Server; using Syst ...
- 系统开启UAC情形下开机自启动程序如何以管理员权限启动
系统开启UAC情形下开机自启动程序如何以管理员权限启动 题记:本文阐述的是在Windows系统开启UAC的情况下,开机自启动程序需要以管理员权限启动, 系统弹出UAC对话框,用户同意的情形下启动程序 ...