Unity游戏背包系统的实现
一、项目概述
1. 功能描述
该部分主要实现了游戏中玩家在个人背包和游戏角色之间切换装备,能够从背包中将装备装到游戏角色上也能够将游戏角色的装备卸下放入背包。
卸下装备放入背包
将背包中装备赋给游戏角色
2. 实现思路
本功能无需3D效果,只需要在UI上进行涉及即可,因此主要涉及知识为Unity UI组件的使用以及C#基础编程。
主要文件结构如下:
背包、装备栏物品切换的实现 :在背包和装备栏上每个存放物品的格子设置一个空对象,并给他们添加Image组件,通过挂载编辑好的脚本可以实现Image上Sprite的改变,从而实现每个物品格子显示空内容还是某个装备。
例如这是背包中第一个装备格子的属性:
装备栏格子同上(脚本不同)。
装备在状态栏和背包之外的移动 :
为了实现这个效果,可以效仿前面的思路,设置一个空对象,有Image组件,当装备从状态栏/背包中脱离但还没放置到背包/状态栏上时,该空对象的Image则显示刚刚脱离装备格子的装备,其他时间该空对象不显示。且该对象随着鼠标移动。
二、项目实现
1. 背包装备格子的处理:
由于给装备格子添加了Button组件,只需要在挂载的脚本中实现按钮点击事件的处理即可。
该事件处理主要考虑两个因素:1.被点击的装备格子是空格子还是有装备的 2.此时是否还有装备从状态栏/背包中卸下但还是放置到某个格子里
using System.Collections;
using System.Collections.Generic;
using MyGameManager;
using UnityEngine;
using UnityEngine.UI;
public class bag : MonoBehaviour
{
private MyGameSceneManager gsm;
private Image bag_image;
public int cli_type;
public Sprite attack;
public Sprite deffence;
public Sprite move;
public Sprite UIMask;
void Awake()
{
gsm = MyGameSceneManager.GetInstance();
bag_image = GetComponent<Image>();
}
public void On_bag_Button()
{
Debug.Log("in bag clicked .....");
Debug.Log(cli_type);
Debug.Log(bag_image.sprite);
int clickType = gsm.GetClicked().GetClickType();
if (bag_image.sprite != UIMask && clickType == 0)
{
Debug.Log(cli_type);
bag_image.sprite = UIMask;
gsm.GetClicked().SetClickType(cli_type);
cli_type = 0;
}
else if (bag_image.sprite == UIMask)
{
if (clickType == 1)
bag_image.sprite = attack;
else if (clickType == 2)
bag_image.sprite = deffence;
else if (clickType == 3)
bag_image.sprite = move;
cli_type = clickType;
gsm.GetClicked().SetClickType(0);
}
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
2. 状态栏装备格子的处理
考虑与背包格子相同的因素即可。
另外,状态栏的三个格子对应不同类型的装备,每个格子只能装备对应类别的装备。
using System.Collections;
using System.Collections.Generic;
using MyGameManager;
using UnityEngine;
using UnityEngine.UI;
public class left : MonoBehaviour
{
private MyGameSceneManager gsm;
private Image left_img;
public int click_type;
public Sprite zhuangbei;
public Sprite UIMask;
void Awake()
{
gsm = MyGameSceneManager.GetInstance();
left_img = GetComponent<Image>();
}
public void On_left_Button()
{
Debug.Log("in left clicked .....");
int clickType = gsm.GetClicked().GetClickType();
if (left_img.sprite == zhuangbei && clickType == 0)
{
Debug.Log("equip: " + click_type);
left_img.sprite = UIMask;
gsm.GetClicked().SetClickType(click_type);
}
else if (left_img.sprite == UIMask)
{
if (clickType == click_type)
{
left_img.sprite = zhuangbei;
gsm.GetClicked().SetClickType(0);
}
}
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
3.卸下的装备随鼠标移动
装备随鼠标移动需要在空对象挂载的脚本中使用 Update()
函数。不断判断当前是否有装备从格子中卸下,根据状态修改空对象的Iamge组件。
using System.Collections;
using System.Collections.Generic;
using MyGameManager;
using UnityEngine;
using UnityEngine.UI;
public class clickedImage : MonoBehaviour
{
private MyGameSceneManager gsm;
private Image click_image;
private int click_type = 0;
public Sprite basep;
public Sprite attack;
public Sprite jinghua;
public Sprite deffence;
public Sprite move;
public Color None;
public Color NotNone;
public Camera cam;
void Awake()
{
gsm = MyGameSceneManager.GetInstance();
gsm.SetMouse(this);
click_image = GetComponent<Image>();
}
public int GetClickType()
{
return click_type;
}
public void SetClickType(int cli_type)
{
click_type = cli_type;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log("strat click_type: ");
Debug.Log(click_type);
Debug.Log(transform.position.x);
Debug.Log(transform.position.y);
Debug.Log(transform.position.z);
if (click_type == 0)
{
click_image.sprite = basep;
// click_image.color = None;
}
else
{
// click_image.color = NotNone;
if (click_type == 1)
click_image.sprite = attack;
else if (click_type == 2)
click_image.sprite = deffence;
else if (click_type == 3)
click_image.sprite = move;
Vector3 screenP =Camera.main.WorldToScreenPoint(transform.position);
Vector3 mp = Input.mousePosition;
mp.z=screenP.z;
// Vector3 mmp = cam.ScreenToWorldPoint(mp);
// transform.position = new Vector3(mp.x - 450, mp.y - 200, 0);
transform.position = Camera.main.ScreenToWorldPoint(mp);
// transform.position = mp+new Vector3(-370,-140,0);
Debug.Log("mp: " + mp);
// Debug.Log("mmp: " + mmp);
Debug.Log("position: "+transform.position);
Debug.Log(Camera.main.ScreenToWorldPoint(mp));
}
}
}
4. GameManager类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActionManager : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
namespace MyGameManager
{
public class MyGameSceneManager : System.Object
{
private static MyGameSceneManager _game_scene_manager;
private static clickedImage _Clicked;
public static MyGameSceneManager GetInstance()
{
if (_game_scene_manager == null)
{
_game_scene_manager = new MyGameSceneManager();
}
return _game_scene_manager;
}
public void SetMouse(clickedImage _clicked)
{
if (_Clicked == null)
{
_Clicked = _clicked;
}
}
public clickedImage GetClicked()
{
return _Clicked;
}
}
}
三、效果展示
相关连接:
Github:/termHomework文章来源:https://www.toymoban.com/news/detail-742125.html
bilibili 视频展示文章来源地址https://www.toymoban.com/news/detail-742125.html
到了这里,关于Unity 3D期末大作业--背包系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!