DragObject

方法:在被拖拽的UI上添加BoxCollider和UIDragObject。

​UIDragObject

1)Target:被拖动的物体​

2)Movement:移动灵敏度。

3)KeepVisible:让被拖动的物体永远在一个矩形内保持可见

基于UIDragDropItem脚本实现简单的控件拖拽

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyDragDropItem : UIDragDropItem {

    Transform startTransform;

    //物体拖动中会执行
    protected override void OnDragDropMove (Vector2 delta)
    {
        base.OnDragDropMove (delta);
    }

    //开始拖动物体执行
    protected override void OnDragDropStart ()
    {
        base.OnDragDropStart ();

        startTransform = this.transform.parent;
        this.GetComponent<UISprite> ().depth = 3;
    }

    //检测拖拽物体之后放置在哪一个其它物体之上
    protected override void OnDragDropRelease (GameObject surface)
    {
        base.OnDragDropRelease (surface);

        Debug.Log (surface);

        if (surface.tag == "Cell") {
            this.transform.parent = surface.transform;
            this.transform.localPosition = new Vector3(-4,-4,0); 
        } else if (surface.tag == "Item") {
            this.transform.parent = surface.transform.parent;
            surface.transform.parent = startTransform;
            this.transform.localPosition = new Vector3(-4,-4,0);
            surface.transform.localPosition = new Vector3(-4,-4,0);
        }

        this.GetComponent<UISprite> ().depth = 2;
    }
}

results matching ""

    No results matching ""