Unity游戲背包系統(tǒng)的實(shí)現(xiàn)
一、項(xiàng)目概述
1. 功能描述
該部分主要實(shí)現(xiàn)了游戲中玩家在個(gè)人背包和游戲角色之間切換裝備,能夠從背包中將裝備裝到游戲角色上也能夠?qū)⒂螒蚪巧难b備卸下放入背包。
卸下裝備放入背包
將背包中裝備賦給游戲角色
2. 實(shí)現(xiàn)思路
本功能無需3D效果,只需要在UI上進(jìn)行涉及即可,因此主要涉及知識為Unity UI組件的使用以及C#基礎(chǔ)編程。
主要文件結(jié)構(gòu)如下:
背包、裝備欄物品切換的實(shí)現(xiàn) :在背包和裝備欄上每個(gè)存放物品的格子設(shè)置一個(gè)空對象,并給他們添加Image組件,通過掛載編輯好的腳本可以實(shí)現(xiàn)Image上Sprite的改變,從而實(shí)現(xiàn)每個(gè)物品格子顯示空內(nèi)容還是某個(gè)裝備。
例如這是背包中第一個(gè)裝備格子的屬性:
裝備欄格子同上(腳本不同)。
裝備在狀態(tài)欄和背包之外的移動(dòng) :
為了實(shí)現(xiàn)這個(gè)效果,可以效仿前面的思路,設(shè)置一個(gè)空對象,有Image組件,當(dāng)裝備從狀態(tài)欄/背包中脫離但還沒放置到背包/狀態(tài)欄上時(shí),該空對象的Image則顯示剛剛脫離裝備格子的裝備,其他時(shí)間該空對象不顯示。且該對象隨著鼠標(biāo)移動(dòng)。
二、項(xiàng)目實(shí)現(xiàn)
1. 背包裝備格子的處理:
由于給裝備格子添加了Button組件,只需要在掛載的腳本中實(shí)現(xiàn)按鈕點(diǎn)擊事件的處理即可。
該事件處理主要考慮兩個(gè)因素:1.被點(diǎn)擊的裝備格子是空格子還是有裝備的 2.此時(shí)是否還有裝備從狀態(tài)欄/背包中卸下但還是放置到某個(gè)格子里
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. 狀態(tài)欄裝備格子的處理
考慮與背包格子相同的因素即可。
另外,狀態(tài)欄的三個(gè)格子對應(yīng)不同類型的裝備,每個(gè)格子只能裝備對應(yīng)類別的裝備。
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.卸下的裝備隨鼠標(biāo)移動(dòng)
裝備隨鼠標(biāo)移動(dòng)需要在空對象掛載的腳本中使用 Update()
函數(shù)。不斷判斷當(dāng)前是否有裝備從格子中卸下,根據(jù)狀態(tài)修改空對象的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;
}
}
}
三、效果展示
相關(guān)連接:
Github:/termHomework文章來源:http://www.zghlxwxcb.cn/news/detail-742125.html
bilibili 視頻展示文章來源地址http://www.zghlxwxcb.cn/news/detail-742125.html
到了這里,關(guān)于Unity 3D期末大作業(yè)--背包系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!