指定のオブジェクトをドラッグ&ドロップするコードを
書きたいです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class DragObject : MonoBehaviour { 6 7 private Vector3 screenPoint; 8 private Vector3 offset; 9 10 void OnMouseDown() 11 { 12 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 13 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 14 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 15 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 16 } 17 18 void OnMouseDrag() 19 { 20 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 21 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 22 transform.position = currentPosition; 23 } 24} 25
このコードを3Dで試したところ、Drag&Dropできました。
2Dでできるようになる方法を教えていただきたいです。
環境
unity 5.6.2f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/02 08:41