码迷,mamicode.com
首页 > 编程语言 > 详细

【Unity】基于MVC模式的背包系统 UGUI实现

时间:2015-07-03 23:34:40      阅读:1010      评论:0      收藏:0      [点我收藏+]

标签:unity   mvc   背包系统   ugui   

本文基于MVC模式,用UGUI初步实现了背包系统。

包含点击和拖拽两种逻辑,先献上源代码,工程和分析稍后补充。


Model 层

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 脚本功能:MVC模式——Model层,定义物品结构,保存物品数据
/// 添加对象:Bag 背包(Canvas下的空对象)
/// 版权声明:Copyright (c) 2015 duzixi.com  All Rights Reserved
/// 创建日期:2015.5.8
/// 知识要点:
/// 1. MVC
/// 2. 自定义类
/// 3. 类的嵌套
/// </summary>
public class ItemModel : MonoBehaviour {

    // 物品类的定义
    public class Item
    {
        public string name; // 物品名称
        public Sprite img;  // 物品图片

        // 构造器
        public Item(string name, Sprite img) {
            this.name = name;
            this.img = img;
        }
    }

    public static List<Item> items; // 保存物品对象的集合

    // 物品图片数组
    public int size = 16;
    Sprite[] sprites;

    void Awake() // 数据初始化
    {
        items = new List<Item>(); // 初始化List<Item>
        sprites = new Sprite[size];

        // 根据行列值初始化物品列表
        for (int i = 0; i < BagView.row; i++) {
            for (int j = 0; j < BagView.col; j++) {
                items.Add(new Item("", null));
            }
        }

        // 【注意】实际开发中以下部分应由数据库代替
		for (int i = 0; i < size; i++) {
            string name = i < 9 ? "0" + (i + 1) : "" + (i + 1);
            sprites[i] = Resources.Load(name, typeof(Sprite)) as Sprite;
			items[i] = new Item(" ", sprites[i]);
		}	
    }
}



View 层


using UnityEngine;
using UnityEngine.UI;
using System.Collections;
/// <summary>
/// 脚本功能:MVC模式 —— View视图,控制背包的显示方式
/// 添加对象:Bag 背包 (Canvas下的空对象)
/// 版权声明:Copyright (c) 2015 duzixi.com  All Rights Reserved
/// 创建时间:2015.05.08
/// 修改记录:2015.05.18 添加编号
/// 修改记录:2015.07.03 封装显示物品格子方法
/// 知识要点:
/// 1. MVC
/// 2. UGUI 
/// </summary>
public class BagView : MonoBehaviour {
    // 背包规格
    public static int row = 4;  // 行
    public static int col = 5;  // 列

    // 背包格子
    public GameObject grid;
    float width;  // 格子宽度
    float height; // 格子高度

    // 根据格子预设体获取宽和高
    void Awake() {
        width = grid.GetComponent<RectTransform>().rect.width;
        height = grid.GetComponent<RectTransform>().rect.height;
    }

	// 初始状态:平铺格子,创建背包
	void Start () {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                // 计算ID值(物品列表下标)
                int id = j + i * col;

                // 实例化格子预设,按宽高布局
                GameObject itemGrid = Instantiate(grid, transform.position + new Vector3(j * width, -i * height, 0), Quaternion.identity) as GameObject;
                // 将实例化的格子对象设置为背包的子对象
                itemGrid.transform.SetParent(transform);

				// 调用自定义方法:显示某个id的格子内容
				ShowItem(itemGrid.transform, id);

                // 给格子 PickUpDrop 组件编号,拾取放下时用
                itemGrid.GetComponent<PickUpDrop>().gridID = id;
            }
        }
	}

    // 重新刷新背包显示(物品位置发生变化时)
    public void ShowItems() {
        for (int i = 0; i < row * col; i++) {
            Transform itemGrid = transform.GetChild(i);
			ShowItem(itemGrid, i);
        }
    }

    // 显示物品格子
	private void ShowItem(Transform itemGrid, int id) {
        // 显示物品名称
        Text txtUGUI = itemGrid.GetComponentInChildren<Text>();
        txtUGUI.text = ItemModel.items[id].name;

		// 获取物品Icon的Image组件
		Image imageUGUI = itemGrid.GetChild(0).GetComponent<Image>();
		
		// 如果有物品,就显示图片
		if (ItemModel.items[id].img != null) {
			imageUGUI.color = Color.white;
		} else { // 否则不显示
			imageUGUI.color = Color.clear;
		}
        imageUGUI.sprite = ItemModel.items[id].img;
	}
}

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
/// <summary>
/// 脚本功能:让拾取的背包物品随鼠标移动
/// 添加对象:PickedItem 拾取的物品
/// 版权声明:Copyright (c) 2015 duzixi.com  All Rights Reserved
/// 创建日期:2015.05.18
/// 修改记录:2015.07.03 添加射线忽略
/// 知识要点:
/// 1. UGUI RectTransform、锚点、中心点
/// 2. 忽略射线接口 ICanvasRaycastFilter
/// </summary>

public class MoveWithMouse : MonoBehaviour, ICanvasRaycastFilter {
    RectTransform rect; // 获取UGUI定位组件

	Image icon;  // 显示当前拾取物品的图标

    void Awake() {
        rect = GetComponent<RectTransform>();
        // 【注意】图标对象是第0个子对象
		icon = transform.GetChild(0).GetComponent<Image>(); 
    }

	void Update () {
        // 用鼠标位置给图标图片定位
        rect.anchoredPosition3D = Input.mousePosition;
        // 根据是否有图片确定透明度
		if (PickUpDrop.pickedItem.img != null) {
			icon.color = Color.white;
		} else {
			icon.color = Color.clear;
		}
		icon.sprite = PickUpDrop.pickedItem.img;
	}

    // 忽略鼠标图标上的射线
	public bool IsRaycastLocationValid (Vector2 sp, Camera eventCamera) {
		return false;
	}
}



Control 层

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
/// <summary>
/// 脚本功能:MVC模式 —— Control控制,背包内物品摆放
/// 添加对象:Item 物品格子预设体 
/// 版权声明:Copyright (c) 2015 duzixi.com  All Rights Reserved
/// 创建日期:2015.05.18 duzixi.com
/// 修改记录:2015.07.03 添加射线忽略
/// 知识要点:
/// 1. UGUI、MVC设计模式
/// </summary>
public class PickUpDrop : MonoBehaviour, IDropHandler {
    public int gridID;

    public static ItemModel.Item pickedItem; // 当前拾取的物品
	
	void Start () {
		// 初始化当前拾取物品为空
        pickedItem = new ItemModel.Item("", null); 
	}

    // 背包核心逻辑:交换
    public static void SwapItem(int gridID)
    {
        // 交换背包中的物品和拾取物品
        ItemModel.Item temp = pickedItem;
        pickedItem = ItemModel.items[gridID];
        ItemModel.items[gridID] = temp;

        // 刷新背包显示
        GameObject.Find("Bag").GetComponent<BagView>().ShowItems();
    }

	// 当物品按钮被点下时(点击触发模式)
    public void Drop() {
		SwapItem(gridID);
    }

    // 当物品放在格子中时(拖拽触发模式)
	public void OnDrop (PointerEventData eventData) {
		SwapItem(gridID);
	}

}


using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
/// <summary>
/// 脚本功能:MVC模式 —— Control控制,操作逻辑,拖拽事件处理
/// 添加对象:Bag 背包 (Canvas下的空对象)
/// 版权声明:Copyright (c) 2015 duzixi.com  All Rights Reserved
/// 创建时间:2015.07.03
/// 知识要点:
/// 1. UnityEngine.EventSystem
/// 2. IBeginDragHandlder, IDragHandler, IEndDragHander
/// </summary>
public class DragEvent : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler {

	int gridID = 0;  // 格子编号

	void Start() {
		gridID = GetComponentInParent<PickUpDrop>().gridID; // 获取格子编号
	}

    // 开始拖拽
	public void OnBeginDrag (PointerEventData eventData) {
        // 调用交换方法
		PickUpDrop.SwapItem(gridID);
	}

    // 拖拽中
	public void OnDrag (PointerEventData eventData) {
		// 【注意】即使没有任何代码处理也要实现该接口,否则拖拽不成功
	}
    
    // 结束拖拽
	public void OnEndDrag (PointerEventData eventData) {
        // 调用交换方法
		PickUpDrop.SwapItem(gridID);
	}
	
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

【Unity】基于MVC模式的背包系统 UGUI实现

标签:unity   mvc   背包系统   ugui   

原文地址:http://blog.csdn.net/duzixi/article/details/46745399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!