我遵循了YouTube上的一个教程(在这个link上可以找到),展示了如何在我的游戏中拖放物品。有没有人知道我如何调整代码,让游戏自动结束或改变场景时,所有的瓷砖已经放置在可用的位置?我还想知道是否有任何方法可以限制用户将磁贴放到已经有磁贴的插槽上?这是我已经拥有的所有源代码:
插槽源代码
public class Slot : MonoBehaviour, IDropHandler {
public GameObject item {
get {
if(transform.childCount>0){
return transform.GetChild (0).gameObject;
}
return null;
}
}
#region IDropHandler implementation
public void OnDrop (PointerEventData eventData)
{
if(!item){
DragHandeler.itemBeingDragged.transform.SetParent (transform);
ExecuteEvents.ExecuteHierarchy<IHasChanged>(gameObject,null,(x,y) => x.HasChanged ());
}
}
DragHandler源代码:
public void OnBeginDrag (PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
startParent = transform.parent;
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
transform.position = eventData.position;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag (PointerEventData eventData)
{
itemBeingDragged = null;
GetComponent<CanvasGroup>().blocksRaycasts = true;
if(transform.parent == startParent){
transform.position = startPosition;
}
}
发布于 2019-06-21 00:08:27
SceneManager.LoadScene(scenename);
如果你还需要帮助你把它放在项目中的逻辑位置-请添加你的代码和一些关于你正在做什么的更多解释,而不是链接到22分钟的一些教程。
更新:
public void OnDrop (PointerEventData eventData)
{
if(!item)
{
DragHandeler.itemBeingDragged.transform.SetParent (transform);
ExecuteEvents.ExecuteHierarchy<IHasChanged>(gameObject,null,(x,y) => x.HasChanged ());
// This code will check if all slots are full and load new scene if they are
// If you already have an array/list of all your slots, you can replace this "FindObjectsOfType" with it
Slot[] allSlots = FindObjectsOfType<Slot>();
bool areAllBackpackSlotsFull = true;
foreach (Slot slot in allSlots)
{
if (slot.isBackpack == true && slot.item == null)
{
areAllBackpackSlotsFull = false;
break;
}
}
if (areAllBackpackSlotsFull)
{
string nextSceneName = "YourScene"; // Place the name of scene you want to load next
UnityEngine.SceneManagement.SceneManager.LoadScene(nextSceneName);
}
}
}
但是我发现它似乎已经在你的代码中实现了。
更新2:
关于背包和库存的新问题的更新。你可以在Slot类中添加一些bool变量来区分backpack和inventory:
public class Slot : MonoBehaviour, IDropHandler
{
public bool isBackpack = false;
...
更新3:
从库存中拖出一块瓷砖并放到“背包”后,有没有办法锁定它?
这显然与最初的帖子和问题无关,但好吧,我也会尽力帮助你的……
我认为这样的东西可以实现您想要的(我已经更改了您的DragHandler代码):
public void OnBeginDrag (PointerEventData eventData)
{
Slot currentSlot = gameObject.GetComponentInParent<Slot>();
if (currentSlot.isBackpack == false)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
startParent = transform.parent;
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
else
{
itemBeingDragged = null;
}
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
if (itemBeingDragged != null)
transform.position = eventData.position;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag (PointerEventData eventData)
{
if (itemBeingDragged != null)
{
itemBeingDragged = null;
GetComponent<CanvasGroup>().blocksRaycasts = true;
if(transform.parent == startParent)
transform.position = startPosition;
}
}
希望能有所帮助。
P.S.我已经帮你回答了5个不同的问题。因为这不是使用这个网站的正确方式,在评论中提出新的无关问题,并不断更新问题和答案,这是不方便的。
https://stackoverflow.com/questions/56689643
复制相似问题