日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

南梦宫 拼图笔记 1.流畅的拖拽操作

發布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 南梦宫 拼图笔记 1.流畅的拖拽操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  實現:流暢的拖拽GameObject

?

主要注意:MonoBehaviour下的默認接口:

void OnMouseDown()

void OnMouseUp()

void Updata

還有一點:計算射線交點時候,代碼里用的ray和plane, 而不是這個掛接的GameObject。如果使用ray和掛接的GameObject可能會出現:鼠標滑動過快,導致鼠標移除GameObject,不再計算位置。

?

using System.Collections; using System.Collections.Generic; using UnityEngine;/// <summary> /// 單個Piece的控制器 /// </summary> public class PieceControl : MonoBehaviour {/// <summary>/// 正確的位置 成功后不再移動/// </summary>private Vector3 mFinishedPos = Vector3.zero;/// <summary>/// 初始隨機的錯誤位置/// </summary>private Vector3 mStartPos = Vector3.zero;private Camera mGameCamera = null;private GameObject mCameraObject = null;private PieceState mNowState = PieceState.None;private Vector3 mOffsetVec3 = Vector3.zero;/// <summary>/// 已經選中了這個對象/// </summary>private bool mIsClickObject = false;/// <summary>/// 鼠標是否按下/// </summary>private bool mIsMouseDown = false;private Vector3 oldInput = Vector3.zero;// Use this for initializationvoid Start(){mCameraObject = GameObject.FindGameObjectWithTag("MainCamera");mGameCamera = mCameraObject.GetComponent<Camera>();}void Update(){if (mIsMouseDown){DragingRefreshPos();}}// 按下鼠標按鍵時 void OnMouseDown(){this.mIsMouseDown = true;Debug.LogError("Down:"+this.name);GetBeginDownOffset();}// 松開鼠標按鍵時void OnMouseUp(){Debug.LogError("Up" + this.name);this.mIsMouseDown = false;}/// <summary>/// 獲取剛拖動object時鼠標交點和object中心的偏移/// </summary>private void GetBeginDownOffset(){Vector3 rejectPos = Vector3.zero;if (GetClickProjectPos(ref rejectPos, Input.mousePosition)){mOffsetVec3 = transform.position - rejectPos;mIsClickObject = true;}}/// <summary>/// 根據GameObject和mouse的相對位置/// 每幀刷新拖動的位置/// </summary>private void DragingRefreshPos(){if (mIsClickObject){Vector3 rejectPos = Vector3.zero;if (GetClickProjectPos(ref rejectPos, Input.mousePosition)){transform.position = mOffsetVec3 + rejectPos;}}}/// <summary>/// 獲取交點坐標/// 當鼠標射線和大plane相交/// </summary>/// <returns></returns>private bool GetClickProjectPos(ref Vector3 click3dPos, Vector3 inputPos){Ray ray = mGameCamera.ScreenPointToRay(inputPos);// Debug.LogError("GetClickProjectPos oldInput:" + oldInput + " inputpos:" + inputPos);oldInput = inputPos;Plane plane = new Plane(Vector3.forward, new Vector3(0.0f, this.transform.position.y, 0.0f));float rayDepth;if(plane.Raycast(ray,out rayDepth)){click3dPos = ray.origin + ray.direction * rayDepth;return true;}return false;}#region 碎片的狀態private enum PieceState{None = -1,Idle = 0,Draging = 2,Finished = 3,Restart = 4,Snapping = 5//吸附過程中 }#endregion}

?

轉載于:https://www.cnblogs.com/sun-shadow/p/9206173.html

總結

以上是生活随笔為你收集整理的南梦宫 拼图笔记 1.流畅的拖拽操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。