السحب والإفلات في محرك يونيتي Unity 2D Drag and Drop
كيفية عمل لغز السحب والإفلات بسهولة في يونيتي unity مع واجهة المستخدم ؛ تحقق من المكان الصحيح للعنصر أو gameobject.
قم بإنشاء العناصر والفتحات باستخدام UI:
- أول شيء يجب فعله هو إنشاء Canva جديدة ، وضبط معاييرها.
يجب عليك أن تتوفر على عنصر EventSystem لأننا سنحتاجه لتطوير لعبتنا
- قم بانشاء صورة جديدة ( UI > Image ) و ستكون خاصة بعنصر اللعبة الدي سنقوم بتحريكه ,
- أضف Component " Canva Groupe" .
- أضف " Canva Group " to the Item gameobject.
- أنشئ سكريب جديد C# script ,
- سمه " ItemScript " ,
- اربط السكريبت بالعنصر Game Object.
- افتح السكريبت وانسخ ما يلي:
Item Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ItemScript : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
private RectTransform rectTrans;
public Canvas myCanvas;
private CanvasGroup canvaGroup;
private void Start()
{
rectTrans = GetComponent<RectTransform>();
canvaGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("BeginDrag");
canvaGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
//Debug.Log("OnDrag");
rectTrans.anchoredPosition += eventData.delta / myCanvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("EndDrag");
canvaGroup.blocksRaycasts = true;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Click");
}
}
اضغط Save .
ارجع الى يونيتي
قم بإنشاء صورة جديدة لمكان Slot, وهو المكان الذي سيتم فيه إسقاط العنصر
اجعل اسم الصورة " Slot " .
أنشئ سكريبت جديد
اجعل اسمه " SlotScript " ,
اربطه بعنصر صورة Slot .
افتح السكريبت وضع به الكود التالي
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SlotScript : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log("ItemDroped");
if(eventData.pointerDrag != null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
}
}
}
نظام السحب والافلات Drag & Drop:
للكشف عند النقر فوق العنصر :
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Click");
}
لاكتشاف متى تبدأ بالسحب:
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("BeginDrag");
canvaGroup.blocksRaycasts = false;
FindObjectOfType<SceneManagerScript>().SoundDrag();
}
لاكتشاف عندما تقوم بسحب العنصر:
public void OnDrag(PointerEventData eventData)
{
//Debug.Log("OnDrag");
rectTrans.anchoredPosition += eventData.delta / myCanvas.scaleFactor;
}
للكشف عند الانتهاء من السحب:
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("EndDrag");
canvaGroup.blocksRaycasts = true;
FindObjectOfType<SceneManagerScript>().SoundDrop();
}
في سكريبت الفتحة Slot
لاكتشاف ما إذا كان العنصر الخاص بك في مكان الفتحة. إذا كان هذا صحيحًا ، عند الانتهاء من السحب ، سيتم وضع العنصر تلقائيًا في منتصف الفتحة.
public void OnDrop(PointerEventData eventData)
{
Debug.Log("ItemDroped");
if(eventData.pointerDrag != null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
}
}
يمكنك أيضًا إضافة صوت عند اكتشاف المكان الصحيح لكل عنصر ,
unity 2d drag and drop script
drag and drop unity 2d
how to make a drag and drop puzzle game in unity
drag and drop unity 3d
unity 2d drag object with touch
Unity drag and drop
unity onclick gameobject 2d
unity drag an object