当前位置: 首页 > news >正文

Unity3D仿星露谷物语开发19之库存栏丢弃及交互道具

1、目标

从库存栏中把道具拖到游戏场景中,库存栏中道具数相应做减法或者删除道具。同时在库存栏中可以交换两个道具的位置。

2、UIInventorySlot设置Raycast属性

在UIInventorySlot中,我们只希望最外层的UIInventorySlot响应Raycast,他下面的2个子对象不需要相应Raycast,所以需要取消InventoryHighlight和Text的Raycast Target设置。

     

3、创建可拖动对象

(1)优化Tags.cs

为了可以方便获取Items对象,新增一个Tag名为ItemsParentTransform,并且将Items标记为ItemsParentTransform。

同时在Assets -> Scripts -> Misc的Tags.cs代码中,新增:

public const string ItemsParentTransform = "ItemsParentTransform";

此时Tags.cs的完整代码是:

using UnityEngine;public static class Tags
{public const string BoundsConfiner = "BoundsConfiner";public const string ItemsParentTransform = "ItemsParentTransform";
}

(2)优化Play.cs

修改Assets -> Scripts -> Player -> Player.cs文件。

当我们用鼠标拖动道具到场景中时,我们不希望角色跟着鼠标移动。

改动1:将已有逻辑移到判断PlayerInputIsDisabled下,当输入不被禁用时才移动。

private void Update()
{#region  Player Inputif (!PlayerInputIsDisabled){ResetAnimationTrigger();PlayerMovementInput();PlayerWalkInput();// Send event to any listeners for player movement inputEventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,isPickingRight, isPickingLeft, isPickingUp, isPickingDown,isSwingToolRight, isSwingToolLeft, isSwingToolUp, isSwingToolDown,false, false, false, false);}#endregion Player Input
}

改动2:创建禁用/启动输入的函数

 public void DisablePlayerInpupt(){PlayerInputIsDisabled = true;}public void EnablePlayerInput(){PlayerInputIsDisabled = false;}

改动3:对外提供的函数,禁用输入,并且设置用户为静止状态。

public void DisablePlayerInputAndResetMovement()
{DisablePlayerInpupt();ResetMovement();// Send event to any listeners for player movement inputEventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,isPickingRight, isPickingLeft, isPickingUp, isPickingDown,isSwingToolRight, isSwingToolLeft, isSwingToolUp, isSwingToolDown,false, false, false, false);
}private void ResetMovement()
{// Reset movementxInput = 0f;yInput = 0f;isRunning = false;isWalking = false;isIdle = true;
}

Player.cs完整的代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Player : SingletonMonobehaviour<Player>
{private float xInput;private float yInput;private bool isWalking;private bool isRunning;private bool isIdle;private bool isCarrying = false;private ToolEffect toolEffect = ToolEffect.none;private bool isUsingToolRight;private bool isUsingToolLeft;private bool isUsingToolUp;private bool isUsingToolDown;private bool isLiftingToolRight;private bool isLiftingToolLeft;private bool isLiftingToolUp;private bool isLiftingToolDown;private bool isPickingRight;private bool isPickingLeft;private bool isPickingUp;private bool isPickingDown;private bool isSwingToolRight;private bool isSwingToolLeft;private bool isSwingToolUp;private bool isSwingToolDown;private Camera mainCamera;private Rigidbody2D rigidbody2D;private Direction playerDirection;private float movementSpeed;private bool _playerInputIsDisabled = false;public bool PlayerInputIsDisabled { get => _playerInputIsDisabled; set => _playerInputIsDisabled = value; }protected override void Awake(){base.Awake();rigidbody2D = GetComponent<Rigidbody2D>();mainCamera = Camera.main;}private void Update(){#region  Player Inputif (!PlayerInputIsDisabled){ResetAnimationTrigger();PlayerMovementInput();PlayerWalkInput();// Send event to any listeners for player movement inputEventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,isPickingRight, isPickingLeft, isPickingUp, isPickingDown,isSwingToolRight, isSwingToolLeft, isSwingToolUp, isSwingToolDown,false, false, false, false);}#endregion Player Input}private void FixedUpdate(){PlayerMovement();}private void PlayerMovement(){Vector2 move = new Vector2(xInput * movementSpeed * Time.deltaTime, yInput * movementSpeed * Time.deltaTime);rigidbody2D.MovePosition(rigidbody2D.position + move);}private void ResetAnimationTrigger(){toolEffect = ToolEffect.none;isUsingToolRight = false;isUsingToolLeft = false;isUsingToolUp = false;isUsingToolDown = false;isLiftingToolRight = false;isLiftingToolLeft = false;isLiftingToolUp = false;isLiftingToolDown = false;isPickingRight = false;isPickingLeft = false;isPickingUp = false;isPickingDown = false;isSwingToolRight = false;isSwingToolLeft = false;isSwingToolUp = false;isSwingToolDown = false;}private void PlayerMovementInput(){xInput = Input.GetAxisRaw("Horizontal");yInput = Input.GetAxisRaw("Vertical");// 斜着移动if (xInput != 0 && yInput != 0) {xInput = xInput * 0.71f;yInput = yInput * 0.71f;}// 在移动if (xInput != 0 || yInput != 0) {isRunning = true;isWalking = false;isIdle = false;movementSpeed = Settings.runningSpeed;// Capture player direction for save gameif (xInput < 0){playerDirection = Direction.left;}else if (xInput > 0){playerDirection = Direction.right;}else if (yInput < 0){playerDirection = Direction.down;}else{playerDirection = Direction.up;}}else if(xInput == 0 && yInput == 0){isRunning = false;isWalking = false;isIdle = true;}}// 按住Shift键移动为walkprivate void PlayerWalkInput(){if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)){isRunning = false;isWalking = true;isIdle = false;movementSpeed = Settings.walkingSpeed;}else{isRunning = true;isWalking = false;isIdle = false;movementSpeed = Settings.runningSpeed;}}public Vector3 GetPlayerViewportPosition(){// Vector3 viewport position for player (0,0) viewport bottom left, (1,1) viewport top rightreturn mainCamera.WorldToViewportPoint(gameObject.transform.position);}public void DisablePlayerInputAndResetMovement(){DisablePlayerInpupt();ResetMovement();// Send event to any listeners for player movement inputEventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,isPickingRight, isPickingLeft, isPickingUp, isPickingDown,isSwingToolRight, isSwingToolLeft, isSwingToolUp, isSwingToolDown,false, false, false, false);}private void ResetMovement(){// Reset movementxInput = 0f;yInput = 0f;isRunning = false;isWalking = false;isIdle = true;}public void DisablePlayerInpupt(){PlayerInputIsDisabled = true;}public void EnablePlayerInput(){PlayerInputIsDisabled = false;}
}

(3)修改UIInventoryBar.cs

创建对可拖动物体的引用。

public GameObject inventoryBarDraggedItem;

当我们从UIInventorySlot中取出道具时会填充该字段。

此时UIInventoryBar.cs的完整代码为:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIInventoryBar : MonoBehaviour
{[SerializeField] private Sprite blank16x16sprite = null; // 插槽默认图案[SerializeField] private UIInventorySlot[] inventorySlot = null; // 所有插槽集合public GameObject inventoryBarDraggedItem;private RectTransform rectTransform;private bool _isInventoryBarPositionBottom = true;public bool IsInventoryBarPositionBottom { get { return _isInventoryBarPositionBottom;} set { _isInventoryBarPositionBottom = value; } }private void Awake(){rectTransform = GetComponent<RectTransform>();}private void OnDisable(){EventHandler.InventoryUpdatedEvent -= InventoryUpdated;}private void InventoryUpdated(InventoryLocation inventoryLocation, List<InventoryItem> inventoryList){if (inventoryLocation == InventoryLocation.player){ClearInventorySlots();if (inventorySlot.Length > 0 && inventoryList.Count > 0){for (int i = 0; i < inventorySlot.Length; i++){if (i < inventoryList.Count){int itemCode = inventoryList[i].itemCode;ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(itemCode);if (itemDetails != null){// add images and details to inventory item slotinventorySlot[i].inventorySlotImage.sprite = itemDetails.itemSprite;inventorySlot[i].textMeshProUGUI.text = inventoryList[i].itemQuantity.ToString();inventorySlot[i].itemDetails = itemDetails;inventorySlot[i].itemQuantity = inventoryList[i].itemQuantity;}}else{break;}}}}}private void ClearInventorySlots(){if(inventorySlot.Length > 0){// loop through inventory slots and update with blank spritefor(int i = 0; i < inventorySlot.Length; i++){inventorySlot[i].inventorySlotImage.sprite = blank16x16sprite;inventorySlot[i].textMeshProUGUI.text = "";inventorySlot[i].itemDetails = null;inventorySlot[i].itemQuantity = 0;}}}private void OnEnable(){EventHandler.InventoryUpdatedEvent += InventoryUpdated;}private void Update(){// Switch inventory bar position depending on player positionSwitchInventoryBarPosition();}private void SwitchInventoryBarPosition(){Vector3 playerViewportPosition = Player.Instance.GetPlayerViewportPosition();if (playerViewportPosition.y > 0.3f && IsInventoryBarPositionBottom == false){rectTransform.pivot = new Vector2(0.5f, 0f);rectTransform.anchorMin = new Vector2(0.5f, 0f);rectTransform.anchorMax = new Vector2(0.5f, 0f);rectTransform.anchoredPosition = new Vector2(0f, 2.5f);IsInventoryBarPositionBottom = true;}else if (playerViewportPosition.y <= 0.3f && IsInventoryBarPositionBottom == true) {rectTransform.pivot = new Vector2(0.5f, 1f);rectTransform.anchorMin = new Vector2(0.5f, 1f);rectTransform.anchorMax = new Vector2(0.5f, 1f);rectTransform.anchoredPosition = new Vector2(0f, -2.5f);IsInventoryBarPositionBottom = false;}}
}

(4)创建Dragged Item预制体

在Hierarchy -> PersistentScene -> UI -> MainGameUICanvas -> UICanvasGroup -> UIInventoryBar下创建InventoryDraggedItem空物体。设置其属性如下:

设置Sort Order=2是为了:当在UIInventoryBar上拖动时,能够更高优先级展示,从而不被Slot上的物体覆盖。

在InventoryDraggedItem下再创建Item空对象,其属性设置如下:

然后将InventoryDraggedItem拖到Assets -> Prefabs -> UI下

然后再删除Hierarchy中的InventoryDraggedItem对象。

点击InventoryDraggedItem预制体,设置其Render Mode为Screen Space - Overlay,勾选Pixel Perfect。

(5)设置inventoryBarDraggedItem属性

点击Hierarchy中的UIInventoryBar对象,将InventoryDraggedItem预制体拖入inventoryBarDraggedItem属性。

4、优化UIInventorySlot脚本

using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;public class UIInventorySlot : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{private Camera mainCamera;private Transform parentItem; // 场景中的物体父类private GameObject draggedItem; // 被拖动的物体public Image inventorySlotHighlight;public Image inventorySlotImage;public TextMeshProUGUI textMeshProUGUI;[SerializeField] private UIInventoryBar inventoryBar = null;[SerializeField] private GameObject itemPrefab = null;[HideInInspector] public ItemDetails itemDetails;[HideInInspector] public int itemQuantity;private void Start(){mainCamera = Camera.main;parentItem = GameObject.FindGameObjectWithTag(Tags.ItemsParentTransform).transform;}public void OnBeginDrag(PointerEventData eventData){if(itemDetails != null) {// Disable keyboard inputPlayer.Instance.DisablePlayerInputAndResetMovement();// Instatiate gameobject as dragged itemdraggedItem = Instantiate(inventoryBar.inventoryBarDraggedItem, inventoryBar.transform);// Get image for dragged itemImage draggedItemImage = draggedItem.GetComponentInChildren<Image>();draggedItemImage.sprite = inventorySlotImage.sprite;}}public void OnDrag(PointerEventData eventData){// move game object as dragged itemif(!draggedItem != null){draggedItem.transform.position = Input.mousePosition;}}public void OnEndDrag(PointerEventData eventData){// Destroy game object as dragged itemif (draggedItem != null) {Destroy(draggedItem);// if drag ends over inventory bar, get item drag is over and swap thenif (eventData.pointerCurrentRaycast.gameObject != null && eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>() != null) {}else{// else attemp to drop the item if it can be droppedif (itemDetails.canBeDropped){DropSelectedItemAtMousePosition();}}// Enable player inputPlayer.Instance.EnablePlayerInput();}}/// <summary>/// Drops the item(if selected) at the current mouse position. called by the DropItem event/// </summary>private void DropSelectedItemAtMousePosition(){if(itemDetails != null){Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCamera.transform.position.z));// Create item from prefab at mouse positionGameObject itemGameObject = Instantiate(itemPrefab, worldPosition, Quaternion.identity, parentItem);Item item = itemGameObject.GetComponent<Item>();item.ItemCode = itemDetails.itemCode;// Remove item from player's inventoryInventoryManager.Instance.RemoveItem(InventoryLocation.player, item.ItemCode);}}
}

1)针对draggedItemImage.sprite = inventorySlotImage.sprite;的代码:

当点击/拖动某个具体Slot时,就会执行该Slot对应的UIInventorySlot脚本,也可以获取到对应的Slot Image信息。

2)针对item.ItemCode = itemDetails.itemCode;

itemDetails属性为UIInventorySlot的public属性。

当添加一个物体时,会触发CallInventoryUpdatedEvent事件,从而更新每个Slot的itemDetails值。

5、优化InventoryManager脚本

增加RemoveItem和RemoveItemAtPosition两个函数。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class InventoryManager : SingletonMonobehaviour<InventoryManager>
{private Dictionary<int, ItemDetails> itemDetailsDictionary;public List<InventoryItem>[] inventoryLists; // 每个位置的库存清单// 每个位置的库存数。 The index of the array is the inventory list(from the// InventoryLocation enum), and the value is the capacity of that inventory list[HideInInspector] public int[] inventoryListCapacityIntArray; [SerializeField] private SO_ItemList itemList = null;protected override void Awake(){base.Awake();// Create Inventory listsCreateInventoryLists();// Create item details dictionaryCreateItemDetailsDictionary();}private void CreateInventoryLists(){inventoryLists = new List<InventoryItem>[(int)InventoryLocation.count];for (int i = 0; i < (int)InventoryLocation.count; i++){inventoryLists[i] = new List<InventoryItem>();}// initialize inventory list capacity arrayinventoryListCapacityIntArray = new int[(int)InventoryLocation.count];// initialize player inventory list capacityinventoryListCapacityIntArray[(int)InventoryLocation.player] = Settings.playerInitialInventoryCapacity;}/// <summary>/// Populates the itemDetailsDictionary from the scriptable object items list/// </summary>private void CreateItemDetailsDictionary(){itemDetailsDictionary = new Dictionary<int, ItemDetails>();foreach (ItemDetails itemDetails in itemList.itemDetails) {itemDetailsDictionary.Add(itemDetails.itemCode, itemDetails);}}/// <summary>/// Add an item to the inventory list for the inventoryLocation and then destroy the gameObjectToDelete/// 角色拾取到物品后,物品需要消失掉/// </summary>/// <param name="inventoryLocation"></param>/// <param name="item"></param>/// <param name="gameObjectToDelete"></param>public void AddItem(InventoryLocation inventoryLocation, Item item, GameObject gameObjectToDelete){AddItem(inventoryLocation, item);Destroy(gameObjectToDelete);}/// <summary>/// Add an item to the inventory list for the inventoryLocation/// </summary>/// <param name="inventoryLocation"></param>/// <param name="item"></param>public void AddItem(InventoryLocation inventoryLocation, Item item){int itemCode = item.ItemCode;List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];// Check if inventory already contains the itemint itemPosition = FindItemInInventory(inventoryLocation, itemCode);if(itemPosition != -1){AddItemPosition(inventoryList, itemCode, itemPosition);}else{AddItemPosition(inventoryList, itemCode);}// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);  }/// <summary>/// Add item to position in the inventory/// </summary>/// <param name="inventoryList"></param>/// <param name="itemCode"></param>/// <param name="position"></param>/// <exception cref="NotImplementedException"></exception>private void AddItemPosition(List<InventoryItem> inventoryList, int itemCode, int position){InventoryItem inventoryItem = new InventoryItem();int quantity = inventoryList[position].itemQuantity + 1;inventoryItem.itemQuantity = quantity;inventoryItem.itemCode = itemCode;inventoryList[position] = inventoryItem;Debug.ClearDeveloperConsole();//DebugPrintInventoryList(inventoryList);}/// <summary>/// Remove an item from the inventory, and create a game object at the position it was dropped/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>public void RemoveItem(InventoryLocation inventoryLocation, int itemCode){List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];// Check if inventory already contains the itemint itemPosition = FindItemInInventory(inventoryLocation, itemCode);if(itemPosition != -1){RemoveItemAtPosition(inventoryList, itemCode, itemPosition);}// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);}private void RemoveItemAtPosition(List<InventoryItem> inventoryList, int itemCode, int position){InventoryItem inventoryItem = new InventoryItem();int quantity = inventoryList[position].itemQuantity - 1;if(quantity > 0){inventoryItem.itemQuantity = quantity;inventoryItem.itemCode = itemCode;inventoryList[position] = inventoryItem;}else{inventoryList.RemoveAt(position);   }}private void DebugPrintInventoryList(List<InventoryItem> inventoryList){foreach(InventoryItem inventoryItem in inventoryList){Debug.Log("Item Description:" + InventoryManager.Instance.GetItemDetails(inventoryItem.itemCode).itemDescription + "    Item Quantity:" + inventoryItem.itemQuantity);}Debug.Log("*******************************************************************************");}/// <summary>/// Add item to the end of the inventory /// </summary>/// <param name="inventoryList"></param>/// <param name="itemCode"></param>/// <exception cref="NotImplementedException"></exception>private void AddItemPosition(List<InventoryItem> inventoryList, int itemCode){InventoryItem inventoryItem = new InventoryItem(); inventoryItem.itemCode = itemCode;inventoryItem.itemQuantity = 1;inventoryList.Add(inventoryItem);//DebugPrintInventoryList(inventoryList);}/// <summary>/// Find if an itemCode is already in the inventory. Returns the item position/// in the inventory list, or -1 if the item is not in the inventory/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>/// <returns></returns>/// <exception cref="NotImplementedException"></exception>private int FindItemInInventory(InventoryLocation inventoryLocation, int itemCode){List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];for (int i = 0; i < inventoryList.Count; i++){if(inventoryList[i].itemCode == itemCode){return i;}}return -1;}/// <summary>/// Returns the itemDetails (from the SO_ItemList) for the itemCode, or null if the item doesn't exist/// </summary>/// <param name="itemCode"></param>/// <returns></returns>public ItemDetails GetItemDetails(int itemCode) {ItemDetails itemDetails;if(itemDetailsDictionary.TryGetValue(itemCode, out itemDetails)){return itemDetails;}else{return null;}}
}

6、填充InventorySlot面板属性

这两个属性没法从prefab中直接选择对象进行设置,所以需要在InventorySlot实例化之后的对象们中一一进行设置。

先同时选中UIInventorySlot0~11,然后设置属性如下:

此时运行程序,即可实现从库存栏中拖拽物品到游戏场景中。

7、交换库存栏中商品的位置

(1)修改UIInventorySlot脚本

首先,添加slotNumber用于标记插槽的序列号。

[SerializeField] private int slotNumber = 0; // 插槽的序列号

然后当点击结束处为slot对象时,交换两个slot的位置。

此时UIInventorySlot的完整代码如下:

using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;public class UIInventorySlot : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{private Camera mainCamera;private Transform parentItem; // 场景中的物体父类private GameObject draggedItem; // 被拖动的物体public Image inventorySlotHighlight;public Image inventorySlotImage;public TextMeshProUGUI textMeshProUGUI;[SerializeField] private UIInventoryBar inventoryBar = null;[SerializeField] private GameObject itemPrefab = null;[SerializeField] private int slotNumber = 0; // 插槽的序列号[HideInInspector] public ItemDetails itemDetails;[HideInInspector] public int itemQuantity;private void Start(){mainCamera = Camera.main;parentItem = GameObject.FindGameObjectWithTag(Tags.ItemsParentTransform).transform;}public void OnBeginDrag(PointerEventData eventData){if(itemDetails != null) {// Disable keyboard inputPlayer.Instance.DisablePlayerInputAndResetMovement();// Instatiate gameobject as dragged itemdraggedItem = Instantiate(inventoryBar.inventoryBarDraggedItem, inventoryBar.transform);// Get image for dragged itemImage draggedItemImage = draggedItem.GetComponentInChildren<Image>();draggedItemImage.sprite = inventorySlotImage.sprite;}}public void OnDrag(PointerEventData eventData){// move game object as dragged itemif(!draggedItem != null){draggedItem.transform.position = Input.mousePosition;}}public void OnEndDrag(PointerEventData eventData){// Destroy game object as dragged itemif (draggedItem != null) {Destroy(draggedItem);// if drag ends over inventory bar, get item drag is over and swap thenif (eventData.pointerCurrentRaycast.gameObject != null && eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>() != null) {// get the slot number where the drag endedint toSlotNumber = eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>().slotNumber;// Swap inventory items in inventory listInventoryManager.Instance.SwapInventoryItems(InventoryLocation.player, slotNumber, toSlotNumber);}else{// else attemp to drop the item if it can be droppedif (itemDetails.canBeDropped){DropSelectedItemAtMousePosition();}}// Enable player inputPlayer.Instance.EnablePlayerInput();}}/// <summary>/// Drops the item(if selected) at the current mouse position. called by the DropItem event/// </summary>private void DropSelectedItemAtMousePosition(){if(itemDetails != null){Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCamera.transform.position.z));// Create item from prefab at mouse positionGameObject itemGameObject = Instantiate(itemPrefab, worldPosition, Quaternion.identity, parentItem);Item item = itemGameObject.GetComponent<Item>();item.ItemCode = itemDetails.itemCode;// Remove item from player's inventoryInventoryManager.Instance.RemoveItem(InventoryLocation.player, item.ItemCode);}}
}

(2)修改InventoryManager脚本

新增如下代码:

public void SwapInventoryItems(InventoryLocation inventoryLocation, int fromItem, int toItem)
{// if fromItem index and toItemIndex are within the bounds of the list, not the same, and greater than or equal to zeroif(fromItem < inventoryLists[(int)inventoryLocation].Count && toItem < inventoryLists[(int)inventoryLocation].Count&& fromItem != toItem && fromItem >= 0 && toItem >= 0){InventoryItem fromInventoryItem = inventoryLists[(int)inventoryLocation][fromItem];InventoryItem toInventoryItem = inventoryLists[(int)inventoryLocation][toItem];inventoryLists[(int)inventoryLocation][toItem] = fromInventoryItem;inventoryLists[(int)inventoryLocation][fromItem] = toInventoryItem;// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);}
}

此时InventoryManager.cs完整代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class InventoryManager : SingletonMonobehaviour<InventoryManager>
{private Dictionary<int, ItemDetails> itemDetailsDictionary;public List<InventoryItem>[] inventoryLists; // 每个位置的库存清单// 每个位置的库存数。 The index of the array is the inventory list(from the// InventoryLocation enum), and the value is the capacity of that inventory list[HideInInspector] public int[] inventoryListCapacityIntArray; [SerializeField] private SO_ItemList itemList = null;protected override void Awake(){base.Awake();// Create Inventory listsCreateInventoryLists();// Create item details dictionaryCreateItemDetailsDictionary();}private void CreateInventoryLists(){inventoryLists = new List<InventoryItem>[(int)InventoryLocation.count];for (int i = 0; i < (int)InventoryLocation.count; i++){inventoryLists[i] = new List<InventoryItem>();}// initialize inventory list capacity arrayinventoryListCapacityIntArray = new int[(int)InventoryLocation.count];// initialize player inventory list capacityinventoryListCapacityIntArray[(int)InventoryLocation.player] = Settings.playerInitialInventoryCapacity;}/// <summary>/// Populates the itemDetailsDictionary from the scriptable object items list/// </summary>private void CreateItemDetailsDictionary(){itemDetailsDictionary = new Dictionary<int, ItemDetails>();foreach (ItemDetails itemDetails in itemList.itemDetails) {itemDetailsDictionary.Add(itemDetails.itemCode, itemDetails);}}/// <summary>/// Add an item to the inventory list for the inventoryLocation and then destroy the gameObjectToDelete/// 角色拾取到物品后,物品需要消失掉/// </summary>/// <param name="inventoryLocation"></param>/// <param name="item"></param>/// <param name="gameObjectToDelete"></param>public void AddItem(InventoryLocation inventoryLocation, Item item, GameObject gameObjectToDelete){AddItem(inventoryLocation, item);Destroy(gameObjectToDelete);}/// <summary>/// Add an item to the inventory list for the inventoryLocation/// </summary>/// <param name="inventoryLocation"></param>/// <param name="item"></param>public void AddItem(InventoryLocation inventoryLocation, Item item){int itemCode = item.ItemCode;List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];// Check if inventory already contains the itemint itemPosition = FindItemInInventory(inventoryLocation, itemCode);if(itemPosition != -1){AddItemPosition(inventoryList, itemCode, itemPosition);}else{AddItemPosition(inventoryList, itemCode);}// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);  }/// <summary>/// Add item to position in the inventory/// </summary>/// <param name="inventoryList"></param>/// <param name="itemCode"></param>/// <param name="position"></param>/// <exception cref="NotImplementedException"></exception>private void AddItemPosition(List<InventoryItem> inventoryList, int itemCode, int position){InventoryItem inventoryItem = new InventoryItem();int quantity = inventoryList[position].itemQuantity + 1;inventoryItem.itemQuantity = quantity;inventoryItem.itemCode = itemCode;inventoryList[position] = inventoryItem;Debug.ClearDeveloperConsole();//DebugPrintInventoryList(inventoryList);}/// <summary>/// Remove an item from the inventory, and create a game object at the position it was dropped/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>public void RemoveItem(InventoryLocation inventoryLocation, int itemCode){List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];// Check if inventory already contains the itemint itemPosition = FindItemInInventory(inventoryLocation, itemCode);if(itemPosition != -1){RemoveItemAtPosition(inventoryList, itemCode, itemPosition);}// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);}private void RemoveItemAtPosition(List<InventoryItem> inventoryList, int itemCode, int position){InventoryItem inventoryItem = new InventoryItem();int quantity = inventoryList[position].itemQuantity - 1;if(quantity > 0){inventoryItem.itemQuantity = quantity;inventoryItem.itemCode = itemCode;inventoryList[position] = inventoryItem;}else{inventoryList.RemoveAt(position);   }}/// <summary>/// Swap item at fromItem index with item at toItem index in inventoryLocation inventory list/// </summary>/// <param name="inventoryLocation"></param>/// <param name="fromItem"></param>/// <param name="toItem"></param>public void SwapInventoryItems(InventoryLocation inventoryLocation, int fromItem, int toItem){// if fromItem index and toItemIndex are within the bounds of the list, not the same, and greater than or equal to zeroif(fromItem < inventoryLists[(int)inventoryLocation].Count && toItem < inventoryLists[(int)inventoryLocation].Count&& fromItem != toItem && fromItem >= 0 && toItem >= 0){InventoryItem fromInventoryItem = inventoryLists[(int)inventoryLocation][fromItem];InventoryItem toInventoryItem = inventoryLists[(int)inventoryLocation][toItem];inventoryLists[(int)inventoryLocation][toItem] = fromInventoryItem;inventoryLists[(int)inventoryLocation][fromItem] = toInventoryItem;// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);}}private void DebugPrintInventoryList(List<InventoryItem> inventoryList){foreach(InventoryItem inventoryItem in inventoryList){Debug.Log("Item Description:" + InventoryManager.Instance.GetItemDetails(inventoryItem.itemCode).itemDescription + "    Item Quantity:" + inventoryItem.itemQuantity);}Debug.Log("*******************************************************************************");}/// <summary>/// Add item to the end of the inventory /// </summary>/// <param name="inventoryList"></param>/// <param name="itemCode"></param>/// <exception cref="NotImplementedException"></exception>private void AddItemPosition(List<InventoryItem> inventoryList, int itemCode){InventoryItem inventoryItem = new InventoryItem(); inventoryItem.itemCode = itemCode;inventoryItem.itemQuantity = 1;inventoryList.Add(inventoryItem);//DebugPrintInventoryList(inventoryList);}/// <summary>/// Find if an itemCode is already in the inventory. Returns the item position/// in the inventory list, or -1 if the item is not in the inventory/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>/// <returns></returns>/// <exception cref="NotImplementedException"></exception>private int FindItemInInventory(InventoryLocation inventoryLocation, int itemCode){List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];for (int i = 0; i < inventoryList.Count; i++){if(inventoryList[i].itemCode == itemCode){return i;}}return -1;}/// <summary>/// Returns the itemDetails (from the SO_ItemList) for the itemCode, or null if the item doesn't exist/// </summary>/// <param name="itemCode"></param>/// <returns></returns>public ItemDetails GetItemDetails(int itemCode) {ItemDetails itemDetails;if(itemDetailsDictionary.TryGetValue(itemCode, out itemDetails)){return itemDetails;}else{return null;}}
}

(3)更新Inspector中的slotNumber值

依次修改UIInventorySlot0~11中的SlotNumber值。

相关文章:

Unity3D仿星露谷物语开发19之库存栏丢弃及交互道具

1、目标 从库存栏中把道具拖到游戏场景中&#xff0c;库存栏中道具数相应做减法或者删除道具。同时在库存栏中可以交换两个道具的位置。 2、UIInventorySlot设置Raycast属性 在UIInventorySlot中&#xff0c;我们只希望最外层的UIInventorySlot响应Raycast&#xff0c;他下面…...

SQL进阶实战技巧:如何利用 Oracle SQL计算线性回归置信区间?

目录 1 置信区间计算方法 步骤1:计算回归系数 步骤2:计算标准误差 步骤3:计算置信区间 2 数据准备 <...

计算机网络——网络层—IP数据报与分片

一、IP 数据报的格式 • 一个 IP 数据报由首部和数据两部分组成。 • 首部的前一部分是固定长度&#xff0c;共 20 字节&#xff0c;是所有 IP 数据报必须具有的。 • 在首部的固定部分的后面是一些可选字段&#xff0c;其长度是可变的。 IP 数据报首部的固定部分中的各字段 版…...

高山旅游景区有效降低成本,无人机山下到山上物资吊运技术详解

在高山旅游景区&#xff0c;传统的物资运输方式往往面临人力成本高昂、效率低下等问题&#xff0c;而无人机技术的引入为这一难题提供了新的解决方案。以下是对无人机从山下到山上进行物资吊运技术的详细解析&#xff1a; 一、无人机物资吊运技术的优势 1. 降低人力成本&#…...

Linux 注册线程化的中断处理程序

1. 注册线程化中断处理函数 devmem_request_threaded_irq 是 Linux 内核中的一个函数&#xff0c;用于请求并注册一个线程化的中断处理程序。这个函数允许开发者注册一个中断处理函数&#xff0c;这个函数会在中断发生时被调用&#xff0c;从而实现相应的中断处理逻辑。它通过…...

【狂热算法篇】解锁数据潜能:探秘前沿 LIS 算法

嘿&#xff0c;各位编程爱好者们&#xff01;今天带来的 LIS 算法简直太赞啦 无论你是刚入门的小白&#xff0c;还是经验丰富的大神&#xff0c;都能从这里找到算法的奇妙之处哦&#xff01;这里不仅有清晰易懂的 C 代码实现&#xff0c;还有超详细的算法讲解&#xff0c;让你轻…...

Linux: 关于 mount 的一些细节

文章目录 1. 前言2. mount 的主要细节 1. 前言 限于作者能力水平&#xff0c;本文可能存在谬误&#xff0c;因此而给读者带来的损失&#xff0c;作者不做任何承诺。 2. mount 的主要细节 mount 从系统调用 sys_mount() 发起&#xff0c;如 mount -t tmpfs cgroup /sys/fs/cg…...

CSS3——3. 书写格式二

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title></head><body><!--css书写&#xff1a;--><!--1. 属性名:属性值--><!--2.属性值是对属性的相关描述--><!--3.属性名必须是…...

Java-JVM详解

Java-JVM ①JVM概述 ❶基本介绍 JVM&#xff1a;全称 Java Virtual Machine&#xff0c;一个虚拟计算机&#xff0c;Java 程序的运行环境&#xff08;Java二进制字节码的运行环境&#xff09; 特点&#xff1a; Java 虚拟机基于二进制字节码执行&#xff0c;由一套字节码指…...

docker搭建atlassian-confluence:7.2.0

文章目录 引言I 部署前准备数据库镜像准备自己构建镜像dockerhub第三方镜像II 安装启动容器基础配置(获取服务器ID)授权码获取集群选择设置数据库配置管理员账号引言 准备数据库、镜像启动容器获取服务器ID根据服务器ID等信息,基于atlassian-agent.jar 授权I 部署前准备 数…...

YOLOv8实战人员跌倒检测

本文采用YOLOv8作为核心算法框架&#xff0c;结合PyQt5构建用户界面&#xff0c;使用Python3进行开发。YOLOv8以其高效的实时检测能力&#xff0c;在多个目标检测任务中展现出卓越性能。本研究针对人员跌倒目标数据集进行训练和优化&#xff0c;该数据集包含丰富人员跌倒图像样…...

瑞芯微rk3568平台 openwrt系统适配ffmpeg硬件解码(rkmpp)

瑞芯微rk3568平台 openwrt系统适配ffmpeg硬件解码(rkmpp) RK3568及rkmpp介绍编译安装mpp获取源码交叉编译安装 libdrmlibdrm-2.4.89 make 方式编译(cannot find -lcairo, 不推荐)下载源码编译编译错误: multiple definition of `nouveau debug‘错误cannot find -lcairo:…...

自动驾驶控制与规划——Project 6: A* Route Planning

目录 零、任务介绍一、算法原理1.1 A* Algorithm1.2 启发函数 二、代码实现三、结果分析四、效果展示4.1 Dijkstra距离4.2 Manhatten距离4.3 欧几里德距离4.4 对角距离 五、后记 零、任务介绍 carla-ros-bridge/src/ros-bridge/carla_shenlan_projects/carla_shenlan_a_star_p…...

wordpress报错open_basedir restriction in effect

Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/wp-content/plugins/woocommerce/patterns/banner.php) is not within the allowed path(s): 关闭防跨站攻击...

VSCode Live Server 插件安装和使用

VSCode Live Server是一个由Ritwick Dey开发的Visual Studio Code扩展插件&#xff0c;它提供了一个带有实时重载功能的本地开发服务器。在VSCode中安装和使用Live Server插件进行实时预览和调试Web应用程序。这将大大提高前端开发效率&#xff0c;使网页设计和开发变得更为流畅…...

网络安全-XSS跨站脚本攻击(基础篇)

漏洞扫描的原理 1.跨站脚本攻击介绍 xss跨站脚本攻击&#xff1a; xSS 全称&#xff08;Cross site Scripting &#xff09;跨站脚本攻击&#xff0c;是最常见的Web应用程序安全漏洞之一&#xff0c;位于OWASP top 10 2013/2017年度分别为第三名和第七名&#xff0c;XSS是指攻…...

【C++笔记】红黑树(RBTree)深度剖析和AVL树的对比分析

【C笔记】红黑树(RBTree)深度剖析和AVL树的对比分析 &#x1f525;个人主页&#xff1a;大白的编程日记 &#x1f525;专栏&#xff1a;C笔记 文章目录 【C笔记】红黑树(RBTree)深度剖析和AVL树的对比分析前言一.红黑树的定义1.1 红黑树的概念1.2红黑树的规则1.3 红黑树对比A…...

Mysql 性能优化:索引条件下推(ICP)

MySQL 索引下推&#xff08;Index Condition Pushdown&#xff0c;ICP&#xff09;是一种查询优化技术&#xff0c;旨在提高使用索引的查询效率。它是在 MySQL 5.6 中引入的&#xff0c;通过将部分 WHERE 子句的过滤条件下推到索引扫描阶段来减少不必要的回表操作&#xff0c;从…...

docker如何进入交互模式

目录 使用 docker run -it 使用 docker exec -it 示例&#xff1a; 使用 docker attach 示例&#xff1a; 在写代码的时候对小白来说避免不了本地和docker环境执行结果不一样的情况 这个时候需要进入正在运行的容器进行调试或执行一些命令操作。这时可以使用 Docker 提供的…...

闲谭SpringBoot--ShardingSphere分库分表探究

文章目录 1. 背景2. 创建数据库3. 修改yml配置文件4. 分片算法类5. 测试6 小结 1. 背景 接上文&#xff0c;我们对日志表&#xff0c;进行了按月的分表&#xff0c;这样每个月几百万条数据量还是扛得住的。 但是如果数据再多呢&#xff0c;除了提高硬件性能&#xff0c;还有一…...

在Java中Semaphore的解释及主要用途

目录 定义 使用方法 主要用途 使用场景示例 定义 Semaphore&#xff08;信号量&#xff09;是Java并发编程中的一个同步工具类&#xff0c;用于控制对共享资源的访问。它通过维护一个计数器来管理多个线程对资源的并发访问数量。这个计数器表示当前可用的许可数&#xff0c…...

React Native 项目 Error: EMFILE: too many open files, watch

硬件&#xff1a;MacBook Pro (Retina, 13-inch, Mid 2014) OS版本&#xff1a;MacOS BigSur 11.7.10 (20G1427) 更新: 删除modules的方法会有反弹&#xff0c;最后还是手动安装了预编译版本的watchman。 React Native 项目运行npm run web&#xff0c;出现如下错误&#xff1a…...

四、VSCODE 使用GIT插件

VSCODE 使用GIT插件 一下载git插件与git Graph插件二、git插件使用三、文件提交到远程仓库四、git Graph插件 一下载git插件与git Graph插件 二、git插件使用 git插件一般VSCode自带了git&#xff0c;就是左边栏目的图标 在下载git软件后vscode的git插件会自动识别当前项目 …...

5 分布式ID

这里讲一个比较常用的分布式防重复的ID生成策略&#xff0c;雪花算法 一个用户体量比较大的分布式系统必然伴随着分表分库&#xff0c;分机房部署&#xff0c;单体的部署方式肯定是承载不了这么大的体量。 雪花算法的结构说明 如下图所示: 雪花算法组成 从上图我们可以看…...

flink的EventTime和Watermark

时间机制 Flink中的时间机制主要用在判断是否触发时间窗口window的计算。 在Flink中有三种时间概念&#xff1a;ProcessTime、IngestionTime、EventTime。 ProcessTime&#xff1a;是在数据抵达算子产生的时间&#xff08;Flink默认使用ProcessTime&#xff09; IngestionT…...

T-SQL语言的函数实现

T-SQL语言的函数实现 在数据库管理系统中&#xff0c;函数是一种非常重要的编程结构。SQL Server支持多种类型的函数&#xff0c;包括标量函数、表值函数和系统函数。本文将详细介绍T-SQL中函数的实现&#xff0c;结合实际应用场景&#xff0c;帮助读者深入理解函数的使用方法…...

SpringBoot 使用 Cache 集成 Redis做缓存保姆教程

1. 项目背景 Spring Cache是Spring框架提供的一个缓存抽象层&#xff0c;它简化了缓存的使用和管理。Spring Cache默认使用服务器内存&#xff0c;并无法控制缓存时长&#xff0c;查找缓存中的数据比较麻烦。 因此Spring Cache支持将缓存数据集成到各种缓存中间件中。本文已常…...

Delphi+SQL Server实现的(GUI)户籍管理系统

1.项目简介 本项目是一个户籍管理系统&#xff0c;用于记录住户身份信息&#xff0c;提供新户登记&#xff08;增加&#xff09;、户籍变更&#xff08;修改&#xff09;、户籍注销&#xff08;删除&#xff09;、户籍查询、曾用名查询、迁户记录查询以及创建备份、删除备份共8…...

Ungoogled Chromium127 编译指南 MacOS篇(七)- 安装依赖包

1. 引言 在获取了 Ungoogled Chromium 的源代码之后&#xff0c;我们需要安装所有必要的依赖包。这些依赖包对于成功编译 Chromium 至关重要。本文将指导您完成所有必需软件包的安装。 2. 依赖包安装 2.1 使用 Homebrew 安装基础依赖 # 安装 Ninja 构建系统 brew install n…...

开放词汇检测新晋SOTA:地瓜机器人开源DOSOD实时检测算法

在计算机视觉领域&#xff0c;目标检测是一项关键技术&#xff0c;旨在识别图像或视频中感兴趣物体的位置与类别。传统的闭集检测长期占据主导地位&#xff0c;但近年来&#xff0c;开放词汇检测&#xff08;Open-Vocabulary Object Detection-OVOD 或者 Open-Set Object Detec…...

json dump避免转义字符反斜杠

笔者在将json序列化到文件时&#xff0c;发现内容包含了反斜杠&#xff1a; [{"video": "MSRVTT-QA\/video_features\/7010.mp4","id": 170859},... ]解决办法是使用ensure_asciiFalse&#xff1a; json.dump(result_items, f, ensure_asciiFa…...

PL/SQL语言的正则表达式

PL/SQL语言的正则表达式详解 在现代软件开发中&#xff0c;数据处理和文本处理是至关重要的环节之一。尤其是在数据库操作中&#xff0c;使用正则表达式来处理字符串数据能大幅提高效率和灵活性。PL/SQL&#xff08;Procedural Language/SQL&#xff09;是Oracle数据库的过程性…...

C/C++中头文件time

在C/C中&#xff0c;<ctime>头文件提供了处理时间和日期的函数&#xff0c;这些函数允许你获取当前时间、计算时间差、格式化时间字符串等。以下是一些<ctime>头文件中常用函数的详细介绍和使用示例&#xff1a; time()&#xff1a;获取当前时间。 time_t currentT…...

【.NET】Kafka消息队列介绍,使用Confluent.Kafka集成Kafka消息队列

一、Kafka介绍 kafka是一种高吞吐量、分布式、可扩展的消息中间件系统&#xff0c;最初由LinkedIn公司开发。随着不断的发展&#xff0c;在最新的版本中它定义为分布式的流处理平台&#xff0c;现在在大数据应用中也是十分广泛。 它可以处理大量的实时数据流&#xff0c;被广…...

图像处理|膨胀操作

在图像处理领域&#xff0c;形态学操作是一种基于图像形状的操作&#xff0c;用于分析和处理图像中对象的几何结构。**膨胀操作&#xff08;Dilation&#xff09;**是形态学操作的一种&#xff0c;它能够扩展图像中白色区域&#xff08;前景&#xff09;或减少黑色区域&#xf…...

kali安装

2024年最新kali Linux安装教程&#xff08;超详细&#xff0c;图文并茂&#xff09;_kali安装-CSDN博客 【2024年最新版】Kali安装详细教程-CSDN博客 Kali Linux 安装过程 超详细&#xff08;图文并茂&#xff0c;通用版&#xff09;-腾讯云开发者社区-腾讯云...

【Python3】异步操作 redis

aioredis 在高版本已经不支持了&#xff0c; 不要用 代码示例 redis 连接池异步操作redis以及接口 import asyncio from sanic import Sanic from sanic.response import json import redis.asyncio as redis from redis.asyncio import ConnectionPool# 创建 Sanic 应用 app…...

C++ vtordisp的应用场景

文章目录 问题代码1. 基本概念回顾2. 应用场景虚继承与虚函数并存的类层次结构 3. 编译器相关考虑 问题代码 #include <iostream> using namespace std;class base { public:base() {}virtual void show() { cout << "base:: show"<<endl; } priv…...

花生好坏缺陷识别数据集,7262张图片,支持yolo,coco json,pasical voc xml格式的标注,识别准确率在95.7%

花生好坏缺陷识别数据集,7262张图片&#xff0c;支持yolo&#xff0c;coco json&#xff0c;pasical voc xml格式的标注&#xff0c;识别准确率在95.7% 数据集分割 训练组87&#xff05; 6353图片 有效集8% 606图片 测试集4% 303图片 预处理 自动定…...

递归构建树菜单节点

一、获取所有分类上下级信息 /*** 获取所有分类上下级信息*/ public R<List<ResearchTypeTreeVO>> getTypeTreeList(){//获取所有分类数据List<ResearchTypeVO> list ibResearchTypeService.getSuperList(null);List<ResearchTypeTreeVO> researchTy…...

物联网无线芯片模组方案,设备智能化交互升级,ESP32-C3控制应用

无线交互技术的核心在于实现设备之间的无缝连接和数据传输。在智能家居系统中&#xff0c;各种智能设备如智能灯泡、智能插座、智能门锁等&#xff0c;都通过无线网络相互连接&#xff0c;形成一个互联互通的生态。 用户可以通过语音助手、手机APP或其他智能终端&#xff0c;远…...

【Unity万人同屏插件】使用手册 保姆级教程 GPU动画 Jobs多线程渲染

【万人同屏插件】 基于Dots技术&#xff0c;高性能实现3D、2D Spine渲染、海量单位锁敌/碰撞检测。同时通过自定义BRG渲染器绕过对Entities包的依赖&#xff0c;也就是不用写ECS代码即可拥有Entities Graphics的高性能渲染&#xff0c;使用传统开发方式BRG接管Renderer组件。 …...

结合前端的响应式开发深入理解设备像素比

前端响应式开发中设备像素比是一个绕不开的概念&#xff0c;彻底理解这个概念&#xff0c;我们首先要梳理清楚屏幕设备的物理像素&#xff08;Physical Pixel&#xff09;、逻辑像素&#xff08;Logical Pixel&#xff09;以及css像素&#xff08;px&#xff09;之前的区别和联…...

QT c++ 按钮 样式 设置按下和松开的背景颜色

上一篇文章&#xff0c;需要自定义类&#xff0c;本文使用样式设置按下和松开的背景颜色。 1.头文件 #ifndef WIDGET_H #define WIDGET_H #include <QWidget> //#include "CustomButton.h" #include <QVBoxLayout> //#include <QLinearGradient>…...

解决Docker冲突问题

错误&#xff1a;docker-ce-cli conflicts with 2:docker-1.13.1-210.git7d71120.el7.centos.x86_64 错误&#xff1a;docker-ce conflicts with 2:docker-1.13.1-210.git7d71120.el7.centos.x86_64 您可以尝试添加 --skip-broken 选项来解决该问题 您可以尝试执行&#xff1a;…...

C++之闭散列哈希表

目录 unordered_set和unordered_map 哈希概念 哈希表基本结构 哈希冲突 线性探测​编辑 二次探测 前几期我们学习了红黑树和红黑树的模拟实现&#xff0c;最终使用红黑树封装了map和set。本期开始我们将学习下一个重要的知识点---哈希表&#xff0c;最终使用哈希表封装u…...

微信小程序map组件所有markers展示在视野范围内

注意&#xff1a;使用include-points属性不生效&#xff0c;要通过createMapContext实现 <template><view class"map-box"><map id"map" class"map" :markers"markers" :enable-traffic"true" :enable-poi&…...

Ubuntu平台虚拟机软件学习笔记

Ubuntu平台上常见虚拟机软件 VirtualBox [Download]KVM/QEMU 1. VirtualBox 1.1 查看安装版本 VBoxManage -V2. KVM/QEMU KVM: Kernel-based Virtual Machine QEMU: Quick EMUlator 通义千问&#xff1a; virt-manager 既不是QEMU也不是KVM&#xff0c;而是用于管理和创建…...

EasyExcel数据的导入导出

1.easyExcel简介 EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。 他能让你在不用考虑性能、内存的等因素的情况下&#xff0c;快速完成Excel的读、写等功能。 EasyExcel 的主要特点如下&#xff1a; 1、高性能&#xff1a;EasyExcel 采用了异…...

CSS Grid 布局全攻略:从基础到进阶

文章目录 一.Grid 是什么二.示例代码1. 基础使用 - 固定宽高2.百分百宽高3.重复设置-repeat4.单位-fr5.自适应6.间距定义其他 一.Grid 是什么 CSS 中 Grid 是一种强大的布局方式&#xff0c;它可以同时处理行和列 Grid 和Flex有一些类似&#xff0c;都是由父元素包裹子元素使用…...