unity之KnapsackView
来源:互联网 发布:csgo显示网络数据命令 编辑:程序博客网 时间:2024/06/05 09:44
出售是先点击出售按钮,然后点击目标格子
简单布局
效果,当前为切换到背包2
测试背包类
using UnityEngine;public class Test : MonoBehaviour { private void Start() { var knapsack = transform.GetComponentInChildren<KnapsackView>(); knapsack.GetChangedValue("Gold").text = "123"; knapsack.onLeftClick += x => { Debug.Log("左击了格子 "+x.name); }; knapsack.onArrange += () => { Debug.Log("整理背包"); }; knapsack.onButtonClick += x => { Debug.Log("点击了 " + x); }; knapsack.onDoubleClick += x => { Debug.Log("双击了 " + x.name); }; knapsack.onRightClick += x => { Debug.Log("右击了 " + x.name); }; knapsack.onSell += x => { Debug.Log("出售"+x.name); }; }}
背包类
using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using System;[RequireComponent(typeof(ToggleGroup))]public class KnapsackView : MonoBehaviour{ [Serializable] public class Relation { public Toggle toggle; public Transform grid; } [Tooltip("格子数量")] public int latticeCount = 20; [Tooltip("格子预置体")] public GameObject latticePrefab; [Tooltip("格子父对象")] public GridLayoutGroup[] backpackPage; [Tooltip("所有Toggle对应一个背包页面")] public Relation[] showBackpackPage; [Tooltip("出售点击后的图标")] public Transform sellMoveIcon; [Tooltip("出售按钮")] public Button sellButton; [Tooltip("当该列表任意对象有鼠标按键消息时隐藏图标")] public GameObject[] sellIconHide; [Tooltip("整理按钮")] public Button arrangeButton; [Tooltip("关闭按钮")] public Button closeButton; [Tooltip("点击该列表对象时一样能把背包移动到最后")] public GameObject[] backpacks; /// <summary> /// 有可能这是个模态背包? /// 那么root层应该是不可移动的 /// 所以这是个拖动时具体移动的对象 /// </summary> [Tooltip("拖动时具体移动的对象")] public Transform dragObject; [Tooltip("可拖动对象列表")] public GameObject[] dragKnapsack; [Tooltip("需要修改数据的对象")] public Text[] changedValue; [Tooltip("所有非预定义的按钮")] public Button[] buttons; #region 事件 public Action onArrange; public Action<Transform> onSell; public Action<string> onButtonClick; public Action<Transform> onLeftClick; public Action<Transform> onRightClick; public Action<Transform> onDoubleClick; #endregion private Vector3 mouseOffset; private bool isSell = false; private Dictionary<string, Text> changedValueDic = new Dictionary<string, Text>(); private void Awake() { SetToggles(); SetDragEvent(); SetSellButton(); CreateLattice(); SetCloseButton(); SetButtonClick(); SetSellIconHide(); SetArrangeEvent(); SetChangedValueDic(); SetScrollRectEvent(); SellMoveIconAddCanvasGroup(); if (null != sellMoveIcon) HideSellMoveIcon(); EventListener.Get(gameObject).onLeftClick += () => { transform.SetAsLastSibling(); }; } private void Update() { if (isSell && null != sellMoveIcon && sellMoveIcon.gameObject.activeSelf) sellMoveIcon.position = Input.mousePosition; } /// <summary> /// 获得对应名字的Text /// </summary> /// <param name="name"></param> /// <returns></returns> public Text GetChangedValue(string name) { return changedValueDic[name]; } public void Show() { gameObject.SetActive(true); } public void Hide() { gameObject.SetActive(false); } private void CreateLattice() { if (null == sellButton) { CreateLattice(LeftClick1, RightClick1, LeftDoubleClick1); } else { if (null == sellMoveIcon) CreateLattice(LeftClick2, RightClick2, LeftDoubleClick2); else CreateLattice(LeftClick3, RightClick3, LeftDoubleClick3); } backpackPage = null; } private void CreateLattice(Action<GameObject> leftClick, Action<GameObject> rightClick, Action<GameObject> leftDoubleClick) { for (int i = 0; i < backpackPage.Length; i++) { var grid = backpackPage[i].transform; for (int j = 0; j < latticeCount; j++) { var go = Instantiate(latticePrefab); go.transform.SetParent(grid); EventListener.Get(go).onLeftClick += () => { leftClick(go); }; EventListener.Get(go).onRightClick += () => { rightClick(go); }; EventListener.Get(go).onLeftDoubleClick += () => { leftDoubleClick(go); }; } } } private void LeftClick1(GameObject go) { transform.SetAsLastSibling(); if (null != onLeftClick) onLeftClick(go.transform); } private void LeftClick2(GameObject go) { if (isSell) { isSell = false; if (null != onSell) onSell(go.transform); } else { LeftClick1(go); } } private void LeftClick3(GameObject go) { LeftClick2(go); HideSellMoveIcon(); } private void LeftDoubleClick1(GameObject go) { if (null != onDoubleClick) onDoubleClick(go.transform); } private void LeftDoubleClick2(GameObject go) { isSell = false; LeftDoubleClick1(go); } private void LeftDoubleClick3(GameObject go) { LeftDoubleClick2(go); HideSellMoveIcon(); } private void RightClick1(GameObject go) { transform.SetAsLastSibling(); if (null != onRightClick) onRightClick(go.transform); } private void RightClick2(GameObject go) { isSell = false; RightClick1(go); } private void RightClick3(GameObject go) { RightClick2(go); HideSellMoveIcon(); } private void SetToggles() { var toggleGroup = GetComponent<ToggleGroup>(); for (int i = 0; i < showBackpackPage.Length; i++) { var toggle = showBackpackPage[i].toggle; toggle.group = toggleGroup; var grid = showBackpackPage[i].grid; toggle.onValueChanged.AddListener(isOn => { transform.SetAsLastSibling(); grid.gameObject.SetActive(isOn); }); toggle.isOn = !toggle.isOn; } if(null != showBackpackPage && showBackpackPage.Length > 0) { showBackpackPage[0].toggle.isOn = true; showBackpackPage = null; } } private void SetChangedValueDic() { for (int i = 0; i < changedValue.Length; i++) { var text = changedValue[i]; if (null != text) changedValueDic.Add(changedValue[i].name, changedValue[i]); } changedValue = null; } private void SetButtonClick() { for (int i = 0; i < buttons.Length; i++) { var button = buttons[i]; button.onClick.AddListener(() => { transform.SetAsLastSibling(); if (null != onButtonClick) onButtonClick(button.name); }); } buttons = null; } private void ShowSellMoveIcon() { sellMoveIcon.gameObject.SetActive(true); } private void HideSellMoveIcon() { sellMoveIcon.gameObject.SetActive(false); } private void SetArrangeEvent() { if (null != arrangeButton) { arrangeButton.onClick.AddListener(() => { if (null != onArrange) onArrange(); transform.SetAsLastSibling(); }); arrangeButton = null; } } private void SellMoveIconAddCanvasGroup() { if(null!= sellMoveIcon) { var canvasGroup = sellMoveIcon.GetComponent<CanvasGroup>(); if (null == canvasGroup) { sellMoveIcon.gameObject.AddComponent<CanvasGroup>().blocksRaycasts = false; } } } private void SetCloseButton() { if (null != closeButton) { closeButton.onClick.AddListener(() => { Hide(); }); closeButton = null; } } private void SetSellButton() { if (null != sellButton) { if (null != sellMoveIcon) sellButton.onClick.AddListener(SellEvent1); else sellButton.onClick.AddListener(SellEvent2); } } private void SellEvent1() { ShowSellMoveIcon(); SellEvent2(); } private void SellEvent2() { isSell = true; transform.SetAsLastSibling(); } private void SetDragEvent() { if (null != dragKnapsack) { if(null != dragObject) SetDrag(dragObject); else SetDrag(transform); dragObject = null; dragKnapsack = null; } } private void SetDrag(Transform tran) { for (int i = 0; i < dragKnapsack.Length; i++) { if (null == dragKnapsack[i]) continue; EventListener.Get(dragKnapsack[i]).onInitializePotentialDrag += () => { transform.SetAsLastSibling(); mouseOffset = tran.position - Input.mousePosition; }; EventListener.Get(dragKnapsack[i]).onDrag += pos => { tran.position = Input.mousePosition + mouseOffset; }; } } private void SetScrollRectEvent() { for(int i = 0; i < backpacks.Length; i++) { EventListener.Get(backpacks[i].gameObject).onLeftClick += () => { transform.SetAsLastSibling(); }; EventListener.Get(backpacks[i].gameObject).onRightClick += () => { transform.SetAsLastSibling(); }; } backpacks = null; } private void SetSellIconHide() { if (null != sellMoveIcon) { for (int i = 0; i < sellIconHide.Length; i++) { EventListener.Get(sellIconHide[i]).onLeftClick += HideSellMoveIcon; EventListener.Get(sellIconHide[i]).onRightClick += HideSellMoveIcon; EventListener.Get(sellIconHide[i]).onLeftMiddleClick += HideSellMoveIcon; } sellIconHide = null; } }}
阅读全文
0 0
- unity之KnapsackView
- Unity之Unity基本操作
- unity之入门经验
- unity菜鸟之路
- unity之Animation组件
- Unity之局域网通信
- Unity之渲染问题
- Unity开发之Android
- unity学习之公路
- Unity学习之三
- unity之小结
- unity之数组
- unity之枚举
- unity之抽象类
- unity之接口
- unity之接口
- unity之数据库
- unity之集合
- LeetCode编程练习
- redis入门-String扩展命令
- spark-troubleshooting-常用问题解决
- 博客的开始
- LeetCode编程练习
- unity之KnapsackView
- virtualenv
- (一)linux系统移植过程和环境搭建
- sgu-264. Travel 稳定婚姻问题
- 树的算法_1,并查集
- 结合redis设计与实现的redis源码学习-1-内存分配(zmalloc)
- 给我放在中间
- LeetCode编程练习
- MQL5-真实型(双精度型,浮点型)