usingUnityengine;usingSystem.Collections; Public classUdragmanager:monobehaviour { PublicTransform Target; PrivateVector3 offset; PrivateBounds Bounds; Public Static floatImageWidth; Public Static floatImageHeight; Public Static floatScreenWidth; Public Static floatScreenHeight; Public Static floatM_noffect; voidStart () {initconfig (); } voidinitconfig () {imagewidth=2490; ImageHeight=1536; ScreenWidth=2048; ScreenHeight=1536;//imagewidth = this.transform.localscale.x;//imageheight = this.transform.localscale.y;//screenwidth = screen.width;//screenheight = screen.height;M_noffect= ImageHeight-ScreenHeight; } voidOnpress (BOOLpressed) { if(target = =NULL)return; if(pressed) {bounds=nguimath.calculaterelativewidgetbounds (Target.transform); Vector3 position=UICamera.currentCamera.WorldToScreenPoint (target.position); Offset=NewVector3 (Input.mouseposition.x-(position.x-bounds.size.x/2), INPUT.MOUSEPOSITION.Y-(POSITION.Y-BOUNDS.SIZE.Y/2), 0f); } } voidOndrag (Vector2 delta) {Vector3 currentpoint=NewVector3 (Input.mouseposition.x-offset.x, INPUT.MOUSEPOSITION.Y-Offset.y, 0f); Currentpoint.x+ = bounds.size.x/2; Currentpoint.y+ = bounds.size.x/2; Currentpoint.z=2; Target.position=UICamera.currentCamera.ScreenToWorldPoint (currentpoint); } voidUpdate () {if( This. transform.localposition.x > (-screenwidth)) { This. transform.localposition =NewVector3 (-screenwidth, This. TRANSFORM.LOCALPOSITION.Y, This. transform.localposition.z); } if( This. transform.localposition.x < (-imagewidth)) { This. transform.localposition =NewVector3 (-imagewidth, This. TRANSFORM.LOCALPOSITION.Y, This. transform.localposition.z); } if( This. transform.localposition.y >0) { This. transform.localposition =NewVector3 ( This. transform.localposition.x,0, This. transform.localposition.z); } if( This. TRANSFORM.LOCALPOSITION.Y < (-m_noffect)) { This. transform.localposition =NewVector3 ( This. Transform.localposition.x,-m_noffect, This. transform.localposition.z); } }}
NGUI Touch Drag and constrain the drag area