Pc,android,iphone Camera complex operation on three platforms
Last Update:2015-09-10
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>Using unityengine;<br>Using unityengine.ui;<br>Using system.collections;</p></p><p><p></p></p><p><p>public class Testfocus:monobehaviour<br>{<br>Public Text tex;<br>Public Transform ktarget;<br>public float initdistance = 13.7f;<br>public float Distancemax = 30f;<br>public float desireddistance;<br>public float scrollspeed = 12f;<br>public float rotatespeed = 8f;<br>public float panspeed = 0.3f;</p></p><p><p>private float DPI = 0;<br>private float currentdistance = 0f;<br>private float MouseX = 0f;<br>private float Mousey = 0f;<br>Private Vector3 _tinitpos;<br>Private Vector3 velocity = vector3.zero;<br>private float Velocityz = 0f;<br>private float Delayz = 0.3f;<br>Private quaternion currentrotation;<br>Private quaternion desiredrotation;</p></p><p><p>Private float lastdistance; Achieve a smooth effect of distance and pull away, last camera stop point<br>Private float perdistance; Achieve a smoothing effect that pulls the distance closer and farther away, where each frame of the camera is to be moved</p></p><p><p>Private Vector2 endone; Used to record the zoom trend<br>Private Vector2 endtwo;</p></p><p><p>private bool Istouch = false; Whether it is a touch screen<br>private bool IsPc = false;<br>private bool Centerfinish = true;</p></p><p><p><br>Raycasthit hit;<br>BOOL _isfocus = false;<br>Transform mtrans;</p></p><p><p>void Awake ()<br>{<br>Mtrans = this.transform;</p></p><p><p>#if unity_android<br>Istouch = true;<br>Debug.Log ("this is Android device ^_^");<br>#endif</p></p><p><p>#if Unity_iphone<br>Istouch = true;<br>Debug.Log ("this is Apple device >_<");<br>#endif</p></p><p><p>#if Unity_standalone_win<br>IsPc = true;<br>Debug.Log ("i am a t_t running on a computer from windows");<br>#endif<br>}</p></p><p><p>Use this for initialization<br>void Start ()<br>{<br>DPI = 1080f/screen.width;<br>_tinitpos = ktarget.position;<br>Initdistance = Mathf.clamp (initdistance, 1.25f, distancemax);<br>Currentdistance = Desireddistance = initdistance;</p></p><p><p>}<br><br>Update is called once per frame<br>void Lateupdate ()<br>{<br>If (_isfocus)<br>{<br>Ktarget.position = Vector3.smoothdamp (ktarget.position, hit.transform.position, ref velocity, delayz-0.1f);<br>Desireddistance = initdistance;<br>}<br>}</p></p><p><p><br>void Update ()<br>{</p></p><p><p>Center Display in Keyboard control<br>If (input.getmousebuttonup (0) && isPc)<br>{<br>Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition);<br>If (physics.raycast (ray, out Hit))<br>{<br>If (hit.collider! = Null)<br>_isfocus = true;<br>Tex.text = "keyboard Control centered";<br>}<br>}</p></p><p><p>Center Display in touch screen<br>if (input.touchcount = = 1 && istouch && input.gettouch (0). Phase = = Touchphase.stationary)<br>{<br>Ray Ray = Camera.main.ScreenPointToRay (input.gettouch (0). position);<br>If (physics.raycast (ray, out Hit))<br>{<br>If (hit.collider! = Null)<br>_isfocus = true;<br>Tex.text = "touch Screen control center";<br>}<br>}<br>Touch screen Control<br>If (istouch)<br>{<br>If (input.touchcount > 0)<br>{<br>Tex.text = "touch screen";<br>if (input.touchcount = = 1 && Input.gettouch (0). phase = = Touchphase.moved)<br>{<br>_isfocus = false;<br>MouseX + = Input.gettouch (0). deltaposition.x * rotatespeed * DPI * 0.3f;<br>mousey-= Input.gettouch (0). deltaposition.y * rotatespeed * DPI * 0.3f;<br>Tex.text = "in touch screen rotation";<br>}</p></p><p><p>Touch Screen Zoom<br>if (input.touchcount = = 2)<br>{<br>_isfocus = false;</p></p><p><p> If (input.gettouch (0). phase = = touchphase.moved && Input.gettouch (1). Phase = = Touchphase.moved) <br> {<br> Vector2 Startone = Input.gettouch (0). position; <br> Vector2 starttwo = Input.gettouch (1). position; <br> If (isenlarge (startone, starttwo, endone, endtwo)) <br> {<br> tex.text = "touchscreen zoom"; <br> If (desireddistance < Distanc eMax) <br> Desireddistance + = 1f; <br>} <br> Else <br> {<br> tex.text = "touchscreen magnification"; <br> If (desireddistance > 5f) <br> desireddistance-= 1f; <br>} <br> Endone = startone; <br> Endtwo = starttwo; <br>} <br>} </p></p><p><p>Touch screen for Drag-and-drop<br>if (input.touchcount = = 3)<br>{<br>_isfocus = false;<br>If (input.gettouch (0). phase = = touchphase.moved && input.gettouch (1). phase = touchphase.moved<br>&& Input.gettouch (2). Phase = = Touchphase.moved)<br>{<br>Tex.text = "touch Screen drag";<br>Ktarget.rotation = mtrans.rotation;<br>Ktarget.translate (vector2.right *-input.gettouch (0). deltaposition.x *panspeed);<br>Ktarget.translate (mtrans.up *-input.gettouch (0). deltaposition.y * panspeed);<br>}<br>}<br>}<br>}</p></p><p><p><br>PC On-machine Control<br>If (isPc)<br>{<br>Keyboard operation, Rotation and drag<br>If (input.getmousebutton (0))<br>{<br>_isfocus = false;<br>MouseX + = Input.getaxis ("Mouse X") * rotatespeed;<br>mousey-= input.getaxis ("Mouse Y") * rotatespeed;<br>Tex.text = "in keyboard rotation";</p></p><p><p>}</p></p><p><p>If (input.getmousebutton (1))<br>{<br>_isfocus = false;<br>Ktarget.rotation = mtrans.rotation;<br>Ktarget.translate (vector2.right *-input.getaxis ("Mouse X") * panspeed);<br>Ktarget.translate (mtrans.up *-input.getaxis ("Mouse Y") * panspeed, space.world);<br>}</p></p><p><p><br></p></p><p><p><br>Keyboard scaling along the z axis<br>float Scrollvalue = Input.getaxis ("Mouse scrollwheel");<br>If (scrollvalue! = 0)<br>{<br>_isfocus = false;<br>desireddistance-= Scrollvalue * scrollspeed;<br>Tex.text = "keyboard control distance closer or farther away";<br>}<br>}</p></p><p><p>Mousey = Clampangle (mousey, -30f, 30f);<br>ktarget.position = new Vector3 (mathf.clamp (ktarget.position.x, -15f, 15f), mathf.clamp (ktarget.position.y, -10f, 10f), Mathf.clamp (ktarget.position.z, -30f, 30f));</p></p><p><p>Limit distance<br>If (desireddistance > distancemax)<br>Desireddistance = distancemax;</p></p><p><p>If (desireddistance < 1.25f)<br>{<br>Desireddistance = 1.25f;<br>}</p></p><p><p>Currentrotation = mtrans.rotation;<br>Desiredrotation = Quaternion.euler (mousey, mousex, 0);</p></p><p><p>quaternion rotation = quaternion.slerp (currentrotation, desiredrotation, time.deltatime * 15f);</p></p><p><p><br>Perdistance = desireddistance;<br>Perdistance = Mathf.smoothdamp (lastdistance, perdistance, ref velocityz, delayz);<br>Lastdistance = perdistance;</p></p><p><p>Vector3 position = rotation * New Vector3 (0f, 0f,-perdistance) + ktarget.position;<br>Mtrans.rotation = rotation;<br>Mtrans.position = position;<br></p></p><p><p>}</p></p><p><p>Float clampangle (float angle, float min, float max)<br>{<br>If (angle <-360)<br>Angle + = 360;<br>If (angle > 360)<br>angle-= 360;<br>return Mathf.clamp (angle, min, max);<br>}</p></p><p><p>BOOL Isenlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)<br>{<br>float length1 = mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (op1.y-op2.y) * (op1.y-op2.y));<br>float length2 = mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (np1.y-np2.y) * (np1.y-np2.y));</p></p><p><p>If (length1 < Length2)<br>{<br>Return true;<br>}<br>Else<br>{<br>Return false;<br>}<br>}<br>}</p></p><p><p>Pc,android,iphone Camera complex operation on three platforms</p></p></span>