「Unity/Csharp/オブジェクトをドラッグ」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→別の方法その1) |
|||
(同じ利用者による、間の6版が非表示) | |||
行1: | 行1: | ||
+ | ==unity2020の場合== | ||
+ | |||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | using UnityEngine.EventSystems; | ||
+ | |||
+ | public class BlockScript : MonoBehaviour, IDragHandler, IDropHandler | ||
+ | { | ||
+ | public void OnDrag(PointerEventData eventData) | ||
+ | { | ||
+ | Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint(eventData.position); | ||
+ | Vector3 mousePointInScreen = new Vector3(Input.mousePosition.x, | ||
+ | Input.mousePosition.y, | ||
+ | objectPointInScreen.z); | ||
+ | Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint(mousePointInScreen); | ||
+ | mousePointInWorld.z = this.transform.position.z; | ||
+ | this.transform.position = mousePointInWorld; | ||
+ | } | ||
+ | public void OnDrop(PointerEventData eventData) | ||
+ | { | ||
+ | //Destroy(gameObject); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | 上記は、以下の項目と違って、Colliderは不要。 | ||
+ | |||
+ | ==unity2019?== | ||
+ | unity2020だと以下は動作しなかったので・・ | ||
+ | |||
画像オブジェクトにColliderをつけ以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる | 画像オブジェクトにColliderをつけ以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる | ||
using System.Collections; | using System.Collections; | ||
行10: | 行39: | ||
void OnMouseDrag() | void OnMouseDrag() | ||
{ | { | ||
− | Vector3 objectPointInScreen | + | Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint(this.transform.position); |
− | + | Vector3 mousePointInScreen = new Vector3(Input.mousePosition.x, | |
− | Vector3 mousePointInScreen | + | |
− | + | ||
Input.mousePosition.y, | Input.mousePosition.y, | ||
objectPointInScreen.z); | objectPointInScreen.z); | ||
行22: | 行49: | ||
} | } | ||
− | ==参考== | + | ===参考=== |
http://neareal.com/1230/ | http://neareal.com/1230/ | ||
+ | |||
+ | ==別の方法その1== | ||
+ | Rididbody2DとCollider2D追加 | ||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | using UnityEngine.EventSystems; | ||
+ | |||
+ | public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler | ||
+ | { | ||
+ | private Vector2 prevPos; | ||
+ | |||
+ | public void OnBeginDrag(PointerEventData eventData) | ||
+ | { | ||
+ | prevPos = transform.position; | ||
+ | } | ||
+ | |||
+ | public void OnDrag(PointerEventData eventData) | ||
+ | { | ||
+ | transform.position = eventData.position; | ||
+ | } | ||
+ | |||
+ | public void OnEndDrag(PointerEventData eventData) | ||
+ | { | ||
+ | transform.position = eventData.position - prevPos; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | 参考:http://matorierunormal.sblo.jp/article/187233424.html |
2024年7月12日 (金) 13:10時点における最新版
unity2020の場合
using UnityEngine; using UnityEngine.EventSystems; public class BlockScript : MonoBehaviour, IDragHandler, IDropHandler { public void OnDrag(PointerEventData eventData) { Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint(eventData.position); Vector3 mousePointInScreen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, objectPointInScreen.z); Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint(mousePointInScreen); mousePointInWorld.z = this.transform.position.z; this.transform.position = mousePointInWorld; } public void OnDrop(PointerEventData eventData) { //Destroy(gameObject); } }
上記は、以下の項目と違って、Colliderは不要。
unity2019?
unity2020だと以下は動作しなかったので・・
画像オブジェクトにColliderをつけ以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BlockScript : MonoBehaviour { void Start () { } void Update () { } void OnMouseDrag() { Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint(this.transform.position); Vector3 mousePointInScreen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, objectPointInScreen.z); Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint(mousePointInScreen); mousePointInWorld.z = this.transform.position.z; this.transform.position = mousePointInWorld; } }
参考
別の方法その1
Rididbody2DとCollider2D追加
using UnityEngine; using UnityEngine.EventSystems; public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler { private Vector2 prevPos; public void OnBeginDrag(PointerEventData eventData) { prevPos = transform.position; } public void OnDrag(PointerEventData eventData) { transform.position = eventData.position; } public void OnEndDrag(PointerEventData eventData) { transform.position = eventData.position - prevPos; } }