Last active
July 21, 2019 06:54
-
-
Save ruccho/3e99a13f906fa3a096058b1a9435d330 to your computer and use it in GitHub Desktop.
Unity uGUI要素をドラッグ可能にするスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.EventSystems; | |
[RequireComponent(typeof(EventTrigger))] | |
public class DraggableGraphicWithEventTrigger : MonoBehaviour | |
{ | |
//ドラッグ開始地点とこのオブジェクトの原点との差分ベクトル | |
private Vector2 mouseOffset; | |
void Start() | |
{ | |
InitEventTrigger(); | |
} | |
//ドラッグが開始されたとき1回呼ばれる | |
public void OnBeginDrag() | |
{ | |
//このGameObjectのpositionとマウスカーソル位置の差分を保存 | |
mouseOffset = transform.position - Input.mousePosition; | |
} | |
//ドラッグ中に毎フレーム呼ばれる | |
public void OnDrag() | |
{ | |
//保存した差分ベクトルをマウスカーソルに加算して移動位置を算出 | |
transform.position = (Vector2)Input.mousePosition + mouseOffset; | |
} | |
private void InitEventTrigger() | |
{ | |
//EventTriggerにOnDragBeginとOnDragのイベントを追加 | |
EventTrigger evRef = GetComponent<EventTrigger>(); | |
//OnDragBegin | |
EventTrigger.Entry entry_begindrag = new EventTrigger.Entry(); | |
entry_begindrag.eventID = EventTriggerType.BeginDrag; | |
EventTrigger.TriggerEvent event_begindrag = new EventTrigger.TriggerEvent(); | |
event_begindrag.AddListener((eventData) => OnBeginDrag()); | |
entry_begindrag.callback = event_begindrag; | |
evRef.triggers.Add(entry_begindrag); | |
//OnDrag | |
EventTrigger.Entry entry_drag = new EventTrigger.Entry(); | |
entry_drag.eventID = EventTriggerType.Drag; | |
EventTrigger.TriggerEvent event_drag = new EventTrigger.TriggerEvent(); | |
event_drag.AddListener((eventData) => OnDrag()); | |
entry_drag.callback = event_drag; | |
evRef.triggers.Add(entry_drag); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
[RequireComponent(typeof(Graphic))] | |
public class DraggableGraphicWithRay : MonoBehaviour | |
{ | |
//ドラッグ開始地点とこのオブジェクトの原点との差分ベクトル | |
private Vector2 mouseOffset; | |
private bool isDragging; | |
void Start() | |
{ | |
} | |
void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
PointerEventData pointer = new PointerEventData(EventSystem.current); | |
pointer.position = Input.mousePosition; | |
List<RaycastResult> result = new List<RaycastResult>(); | |
EventSystem.current.RaycastAll(pointer, result); | |
if (result[0].gameObject == gameObject) | |
{ | |
//このGameObjectのpositionとマウスカーソル位置の差分を保存 | |
mouseOffset = transform.position - Input.mousePosition; | |
isDragging = true; | |
} | |
} | |
if (!Input.GetMouseButton(0)) isDragging = false; | |
if (isDragging) | |
{ | |
transform.position = (Vector2)Input.mousePosition + mouseOffset; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
はじめまして!
この度利用させて頂きました!
ありがとうございました!