【Unity】 相机移动脚本

发表于2017-08-28
评论0 4.7k浏览
下面给大家介绍Unity 中的相机移动脚本,由于在项目制作中经常要对相机进行移动,所以采用了公司的工具类CameraOrbit 和 CameraControl,
在项目中需要添加 FingerGestures 插件, 相机脚本是基于FingerGestures 上改写 并搭配,

在相机物体中添加

Root 和Orgin 是和相机相同位置的 相机的移动也是基于 root 和origin的



以下是CameraOrbit代码
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. ///   
  5. /// Adaptation of the standard MouseOrbit script to use the finger drag gesture to rotate the current object using  
  6. /// the fingers/mouse around a target object  
  7. ///   
  8. public class XgCameraOrbit : MonoBehaviour  
  9. {  
  10.   
  11.     public enum PanMode  
  12.     {  
  13.         Disabled,  
  14.         OneFinger,  
  15.         TwoFingers  
  16.     }  
  17.   
  18.     ///   
  19.     /// The object to orbit around  
  20.     ///   
  21.     public Transform target;  
  22.   
  23.     public Transform camRoot;  
  24.     public Transform camOrigin;  
  25.     private XgCameraManager xg_Cm;  
  26.   
  27.     //private XgIntellControl xg_IC;  
  28.     //private XgPictrueControl xg_Pc;  
  29.   
  30.     ///   
  31.     /// Initial camera distance to target  
  32.     ///   
  33.     public float initialDistance = 10.0f;  
  34.   
  35.     ///   
  36.     /// Minimum distance between camera and target  
  37.     ///   
  38.     public float minDistance = 1.0f;  
  39.   
  40.     ///   
  41.     /// Maximum distance between camera and target  
  42.     ///   
  43.     public float maxDistance = 30.0f;  
  44.   
  45.     ///   
  46.     /// Affects horizontal rotation speed (in degrees per cm)  
  47.     ///   
  48.     public float yawSensitivity = 45.0f;  
  49.   
  50.     ///   
  51.     /// Affects vertical rotation speed (in degrees per cm)  
  52.     ///   
  53.     public float pitchSensitivity = 45.0f;  
  54.   
  55.     ///   
  56.     /// Keep yaw angle value between minYaw and maxYaw?  
  57.     ///   
  58.     public bool clampYawAngle = false;  
  59.   
  60.     public float minYaw = -75;  
  61.     public float maxYaw = 75;  
  62.   
  63.     ///   
  64.     /// Keep pitch angle value between minPitch and maxPitch?  
  65.     ///   
  66.     public bool clampPitchAngle = true;  
  67.   
  68.     public float minPitch = -20;  
  69.     public float maxPitch = 80;  
  70.   
  71.     ///   
  72.     /// Allow the user to affect the orbit distance using the pinch zoom gesture  
  73.     ///   
  74.     public bool allowPinchZoom = true;  
  75.   
  76.     ///   
  77.     /// Affects pinch zoom speed  
  78.     ///   
  79.     public float pinchZoomSensitivity = 5.0f;  
  80.   
  81.     private float pinchZoomSensMax;  
  82.     private float pinchZoomSens;  
  83.   
  84.     ///   
  85.     /// Use smooth camera motions?  
  86.     ///   
  87.     public bool smoothMotion = true;  
  88.   
  89.     public float smoothZoomSpeed = 5.0f;  
  90.     public float smoothOrbitSpeed = 10.0f;  
  91.   
  92.     ///   
  93.     /// Two-Finger camera panning.  
  94.     /// Panning will apply an offset to the pivot/camera target point  
  95.     ///   
  96.     public bool allowPanning = false;  
  97.   
  98.     public bool invertPanningDirections = false;  
  99.     public float panningSensitivity = 1.0f;  
  100.     private float panningSensMax;  
  101.     private float panningSens;  
  102.   
  103.     public Transform panningPlane;  
  104.     // reference transform used to apply the panning translation (using panningPlane.right and panningPlane.up vectors)  
  105.   
  106.     public bool smoothPanning = true;  
  107.     public float smoothPanningSpeed = 12.0f;  
  108.   
  109.     // collision test  
  110.     public LayerMask collisionLayerMask;  
  111.     private Transform myTrans;  
  112.     private float distance = 10.0f;  
  113.     private float yaw = 0;  
  114.     private float pitch = 0;  
  115.     private float idealDistance = 0;  
  116.     private float idealYaw = 0;  
  117.     private float idealPitch = 0;  
  118.     private Vector3 idealPanOffset = Vector3.zero;  
  119.     private Vector3 panOffset = Vector3.zero;  
  120.     private Vector3 rPanOffset;  
  121.     private bool slideCentered = false;  
  122.     private Vector3 slideOffset;  
  123.     private PinchRecognizer pinchRecognizer;  
  124.     private bool UIdrag = false;  
  125.     private bool canControl = true;  
  126.     private bool canInput = true;  
  127.   
  128.     private float fl_MovePos = 0f;  
  129.     private float fl_IdeaMovePos;  
  130.     bool canPan;  
  131.     bool canPanY;  
  132.     bool canPanX;  
  133.   
  134.     //messager  
  135.     //SLCameraMessager sCm;  
  136.   
  137.     //调用  
  138.     [HideInInspector]  
  139.     //public HRMainControl main_control;  
  140.   
  141.     void Start()  
  142.     {  
  143.         //main_control = GameObject.Find("Control").GetComponent();  
  144.     }  
  145.   
  146.     public bool UIDrag  
  147.     {  
  148.         get { return UIdrag; }  
  149.         set { UIdrag = value; }  
  150.     }  
  151.   
  152.     public float Distance  
  153.     {  
  154.         get { return distance; }  
  155.     }  
  156.   
  157.     public float IdealDistance  
  158.     {  
  159.         get { return idealDistance; }  
  160.         set { idealDistance = Mathf.Clamp(value, minDistance, maxDistance); }  
  161.     }  
  162.   
  163.     public float Yaw  
  164.     {  
  165.         get { return yaw; }  
  166.     }  
  167.   
  168.     public float IdealYaw  
  169.     {  
  170.         get { return idealYaw; }  
  171.         set { idealYaw = clampYawAngle ? ClampAngle(value, minYaw, maxYaw) : value; }  
  172.     }  
  173.   
  174.     public float Pitch  
  175.     {  
  176.         get { return pitch; }  
  177.     }  
  178.   
  179.     public float IdealPitch  
  180.     {  
  181.         get { return idealPitch; }  
  182.         set { idealPitch = clampPitchAngle ? ClampAngle(value, minPitch, maxPitch) : value; }  
  183.     }  
  184.   
  185.     public Vector3 IdealPanOffset  
  186.     {  
  187.         get { return idealPanOffset; }  
  188.         set { idealPanOffset = value; }  
  189.     }  
  190.   
  191.     public Vector3 PanOffset  
  192.     {  
  193.         get { return panOffset; }  
  194.     }  
  195.   
  196.     public Vector3 ROffset  
  197.     {  
  198.         get { return rPanOffset; }  
  199.     }  
  200.   
  201.     private void InstallGestureRecognizers()  
  202.     {  
  203.         //xg_IC = GameObject.Find(str_Intell_Control).GetComponent();  
  204.         //xg_Pc = GameObject.Find(str_Pic_Control).GetComponent();  
  205.   
  206.         List recogniers = new List(GetComponents());  
  207.         DragRecognizer drag = recogniers.Find(r => r.EventMessageName == "OnDrag"as DragRecognizer;  
  208.         DragRecognizer twoFingerDrag =  
  209.             recogniers.Find(r => r.EventMessageName == "OnTwoFingerDrag"as DragRecognizer;  
  210.         PinchRecognizer pinch = recogniers.Find(r => r.EventMessageName == "OnPinch"as PinchRecognizer;  
  211.   
  212.         // check if we need to automatically add a screenraycaster  
  213.         if (OnlyRotateWhenDragStartsOnObject)  
  214.         {  
  215.             ScreenRaycaster raycaster = gameObject.GetComponent();  
  216.   
  217.             if (!raycaster)  
  218.                 raycaster = gameObject.AddComponent();  
  219.         }  
  220.   
  221.         if (!drag)  
  222.         {  
  223.             drag = gameObject.AddComponent();  
  224.             drag.RequiredFingerCount = 1;  
  225.             drag.IsExclusive = true;  
  226.             drag.MaxSimultaneousGestures = 1;  
  227.             drag.SendMessageToSelection = GestureRecognizer.SelectionType.None;  
  228.         }  
  229.   
  230.         if (!pinch)  
  231.             pinch = gameObject.AddComponent();  
  232.   
  233.         if (!twoFingerDrag)  
  234.         {  
  235.             twoFingerDrag = gameObject.AddComponent();  
  236.             twoFingerDrag.RequiredFingerCount = 2;  
  237.             twoFingerDrag.IsExclusive = true;  
  238.             twoFingerDrag.MaxSimultaneousGestures = 1;  
  239.             twoFingerDrag.ApplySameDirectionConstraint = true;  
  240.             twoFingerDrag.EventMessageName = "OnTwoFingerDrag";  
  241.         }  
  242.     }  
  243.   
  244.     public void Init_Camera()  
  245.     {  
  246.   
  247.   
  248.   
  249.         xg_Cm = gameObject.GetComponent();  
  250.         if (xg_Cm != null)  
  251.             xg_Cm.Init_CameraManager();  
  252.         InstallGestureRecognizers();  
  253.   
  254.         myTrans = transform;  
  255.   
  256.         if (!panningPlane)  
  257.             panningPlane = myTrans;  
  258.   
  259.         Vector3 angles = myTrans.eulerAngles;  
  260.   
  261.         distance = IdealDistance = initialDistance;  
  262.         yaw = IdealYaw = angles.y;  
  263.         pitch = IdealPitch = angles.x;  
  264.   
  265.         pinchZoomSensMax = pinchZoomSensitivity;  
  266.         panningSensMax = panningSensitivity;  
  267.   
  268.         // Make the rigid body not change rotation  
  269.         if (GetComponent())  
  270.             GetComponent().freezeRotation = true;  
  271.   
  272.         Apply();  
  273.     }  
  274.  
  275.     #region Gesture Event Messages  
  276.   
  277.     private float nextDragTime = 0.8f;  
  278.     public bool OnlyRotateWhenDragStartsOnObject = false;  
  279.   
  280.     private void OnDrag(DragGesture gesture)  
  281.     {  
  282.   
  283.         if (canControl)  
  284.         {  
  285.             //Debug.Log("22222");  
  286.             if (!canInput) return;  
  287.             if (!UIdrag)  
  288.             {  
  289.                 // don't rotate unless the drag started on our target object  
  290.                 if (OnlyRotateWhenDragStartsOnObject)  
  291.                 {  
  292.                     if (gesture.Phase == ContinuousGesturePhase.Started)  
  293.                     {  
  294.                         if (!gesture.Recognizer.Raycaster)  
  295.                         {  
  296.                             Debug.LogWarning("The drag recognizer on "   gesture.Recognizer.name    
  297.                                              " has no ScreenRaycaster component set. This will prevent OnlyRotateWhenDragStartsOnObject flag from working.");  
  298.                             OnlyRotateWhenDragStartsOnObject = false;  
  299.                             return;  
  300.                         }  
  301.   
  302.                         if (target && !target.GetComponent())  
  303.                         {  
  304.                             Debug.LogWarning(  
  305.                                 "The target object has no collider set. OnlyRotateWhenDragStartsOnObject won't work.");  
  306.                             OnlyRotateWhenDragStartsOnObject = false;  
  307.                             return;  
  308.                         }  
  309.                     }  
  310.   
  311.                     if (!target || gesture.StartSelection != target.gameObject)  
  312.                         return;  
  313.                 }  
  314.   
  315.                 // wait for drag cooldown timer to wear off  
  316.                 //  used to avoid dragging right after a pinch or pan, when lifting off one finger but the other one is still on screen  
  317.                 if (Time.time < nextDragTime)  
  318.                     return;  
  319.   
  320.                 if (target)  
  321.                 {  
  322.                     IdealYaw  = gesture.DeltaMove.x.Centimeters() * yawSensitivity;  
  323.                     IdealPitch -= gesture.DeltaMove.y.Centimeters() * pitchSensitivity;  
  324.                     //if (main_control.uiControl.pinghenghuan_Index == 1)  
  325.                     //{  
  326.                     //    main_control.uiControl.bl_pinghenghuan = true;  
  327.                     //    main_control.uiControl.OpenPinghenghuan();  
  328.                     //}  
  329.                     //if (main_control.uiControl.dianji_Index == 1)  
  330.                     //{  
  331.                     //    main_control.uiControl.bl_dianji = true;  
  332.                     //    main_control.uiControl.OpenDianJi();  
  333.                     //}  
  334.                     //--------------关闭技能计数-----------------  
  335.                     //main_control.uiControl.isOpenCD = false;  
  336.                     //StopAllCoroutines();  
  337.                     ////如果有滑动的话  也会让 UI计数为false  
  338.                     //main_control.uiControl.uiClick = false;  
  339.                     //if (Input.GetMouseButtonUp(0))  
  340.                     //{  
  341.                     //    if (main_control.uiControl.pinghenghuan_Index == 1)  
  342.                     //    {  
  343.                     //        main_control.uiControl.bl_pinghenghuan = false;  
  344.                     //        main_control.uiControl.OpenPinghenghuan();  
  345.                     //    }  
  346.                     //    if (main_control.uiControl.dianji_Index == 1)  
  347.                     //    {  
  348.                     //        main_control.uiControl.bl_dianji = false;  
  349.                     //        main_control.uiControl.OpenDianJi();  
  350.                     //    }  
  351.                     //    if (!main_control.uiControl.isMainUI)  
  352.                     //    {  
  353.                     //        main_control.uiControl.isOpenCD = true;  
  354.                     //    }  
  355.                     //}  
  356.                     //if (Input.GetMouseButtonUp(0) || UICamera)  
  357.                     //{  
  358.                     //    main_control.uiControl.isOpenCD = true;  
  359.                     //}  
  360.                 }  
  361.             }  
  362.         }  
  363.     }  
  364.   
  365.     private void OnPinch(PinchGesture gesture)  
  366.     {  
  367.   
  368.         if (canControl)  
  369.         {  
  370.             if (!canInput) return;  
  371.             if (allowPinchZoom)  
  372.             {  
  373.                 pinchZoomSens = Distance / 5.0f * pinchZoomSensMax;  
  374.                 IdealDistance -= gesture.Delta.Centimeters() * pinchZoomSens;  
  375.                 nextDragTime = Time.time   0.25f;  
  376.             }  
  377.         }  
  378.     }  
  379.   
  380.     private void OnTwoFingerDrag(DragGesture gesture)  
  381.     {  
  382.         if (canControl)  
  383.         {  
  384.             if (!canInput) return;  
  385.             if (allowPanning)  
  386.             {  
  387.   
  388.                 panningSens = Distance / 5.0f * panningSensMax;  
  389.                 Vector3 move = -panningSens *  
  390.                                (panningPlane.right * gesture.DeltaMove.x.Centimeters()    
  391.                                 panningPlane.up * gesture.DeltaMove.y.Centimeters());  
  392.   
  393.                 if (invertPanningDirections)  
  394.                     IdealPanOffset = -move;  
  395.                 else  
  396.                     IdealPanOffset = move;  
  397.   
  398.                 //reset idealPanPositon  
  399.                 if (gesture.Phase == ContinuousGesturePhase.Ended)  
  400.                     IdealPanOffset = Vector3.zero;  
  401.   
  402.                 nextDragTime = Time.time   0.25f;  
  403.             }  
  404.         }  
  405.     }  
  406.   
  407.     private void OnTap(TapGesture gesture)  
  408.     {  
  409.         if (canControl && gesture.Selection)  
  410.         {  
  411.             xg_Cm.OnManagerTap(gesture);  
  412.         }  
  413.         //if (canControl && gesture.Selection)  
  414.         //{  
  415.         //    if (gesture.Selection.CompareTag("MD_Main"))  
  416.         //    {  
  417.         //        main_control.  
  418.         //    }  
  419.         //}  
  420.     }  
  421.   
  422.   
  423.     //IN assemble mode, double tap to auto disassemble  
  424.     private void OnDoubleTap(TapGesture gesture)  
  425.     {  
  426.         if (canControl)  
  427.             if (gesture.Selection)  
  428.                 xg_Cm.OnManagerTap(gesture);  
  429.     }  
  430.  
  431.     #endregion  
  432.   
  433.     private void Apply()  
  434.     {  
  435.   
  436.         if (smoothMotion)  
  437.         {  
  438.             distance = Mathf.Lerp(distance, IdealDistance, Time.deltaTime * smoothZoomSpeed);  
  439.             yaw = Mathf.LerpAngle(yaw, IdealYaw, Time.deltaTime * smoothOrbitSpeed);  
  440.             pitch = Mathf.LerpAngle(pitch, IdealPitch, Time.deltaTime * smoothOrbitSpeed);  
  441.         }  
  442.         else  
  443.         {  
  444.             distance = IdealDistance;  
  445.             yaw = IdealYaw;  
  446.             pitch = IdealPitch;  
  447.         }  
  448.         //print(IdealYaw);  
  449.         if (smoothPanning)  
  450.             panOffset = Vector3.Lerp(panOffset, idealPanOffset, Time.deltaTime * smoothPanningSpeed);  
  451.         else  
  452.             panOffset = idealPanOffset;  
  453.         if (canInput)  
  454.             rPanOffset = camOrigin.InverseTransformPoint(panOffset   myTrans.position);  
  455.   
  456.         //rotate the orignal camera  
  457.         camOrigin.rotation = Quaternion.Euler(pitch, yaw, 0);  
  458.   
  459.         //change rotation to the orginal camera  
  460.         myTrans.rotation = camOrigin.rotation;  
  461.   
  462.         camOrigin.position = target.position - camOrigin.forward * distance;  
  463.   
  464.         //set position for camera  
  465.         myTrans.position = camOrigin.TransformPoint(rPanOffset);  
  466.   
  467.         //Debug.Log(rPanOffset);  
  468.   
  469.     }  
  470.   
  471.   
  472.     private void LateUpdate()  
  473.     {  
  474.         if (canControl)  
  475.         {  
  476.             Apply();  
  477.         }  
  478.     }  
  479.   
  480.     public void CameraProperty(bool pan, bool zoom)  
  481.     {  
  482.   
  483.         allowPanning = pan;  
  484.         allowPinchZoom = zoom;  
  485.     }  
  486.   
  487.     private static float ClampAngle(float angle, float min, float max)  
  488.     {  
  489.         if (angle < -360)  
  490.             angle  = 360;  
  491.   
  492.         if (angle > 360)  
  493.             angle -= 360;  
  494.   
  495.         return Mathf.Clamp(angle, min, max);  
  496.     }  
  497.   
  498.   
  499.     public void SlideCenter(bool enable, Vector3 offset)  
  500.     {  
  501.   
  502.         slideCentered = enable;  
  503.         if (enable)  
  504.         {  
  505.             slideOffset = offset;  
  506.         }  
  507.         else  
  508.         {  
  509.             slideOffset = Vector3.zero;  
  510.         }  
  511.     }  
  512.   
  513.     public void CanControl(bool enable)  
  514.     {  
  515.   
  516.         canControl = enable;  
  517.     }  
  518.   
  519.     // recenter the camera  
  520.     public void ResetPanning()  
  521.     {  
  522.         IdealPanOffset = Vector3.zero;  
  523.     }  
  524.   
  525.   
  526.     //获取角度  
  527.     public float GetCamYawAngle()  
  528.     {  
  529.         return yaw % 360 < 0 ? 360   yaw % 360 : Yaw % 360;  
  530.     }  
  531.   
  532.     public float GetCamPitchAngle()  
  533.     {  
  534.         return pitch;  
  535.     }  
  536.   
  537.     public IEnumerator PauseControl(float time)  
  538.     {  
  539.   
  540.         canControl = false;  
  541.   
  542.         yield return new WaitForSeconds(time   0.1f);  
  543.   
  544.         canControl = true;  
  545.     }  
  546.   
  547.   
  548.     public void PauseInput(float time)  
  549.     {  
  550.         StopCoroutine("PauseCamInput");  
  551.         StartCoroutine("PauseCamInput", time);  
  552.     }  
  553.   
  554.     private IEnumerator PauseCamInput(float time)  
  555.     {  
  556.         canInput = false;  
  557.         yield return new WaitForSeconds(time);  
  558.         canInput = true;  
  559.     }  
  560.   
  561.     public void TeleportCam(Transform pivot, Vector3 originPos, Vector3 rot, float dis, Vector3 pan)  
  562.     {  
  563.   
  564.         target = pivot;  
  565.         distance = idealDistance = dis;  
  566.   
  567.         yaw = idealYaw = rot.y >= 180 ? rot.y - 360 : rot.y;  
  568.         pitch = idealPitch = rot.x >= 180 ? rot.x - 360 : rot.x;  
  569.   
  570.         panOffset = idealPanOffset = Vector3.zero;  
  571.   
  572.         rPanOffset = pan;  
  573.   
  574.         camOrigin.position = originPos;  
  575.         camOrigin.eulerAngles = rot;  
  576.     }  
  577.   
  578.     public void Cam_CoreComValue()  
  579.     {  
  580.         maxDistance = 20f;  
  581.         minDistance = 15f;  
  582.   
  583.         maxPitch = 40;  
  584.   
  585.         allowPanning = false;  
  586.     }  
  587.   
  588.     public void Cam_DefaultValue()  
  589.     {  
  590.         maxDistance = 20f;  
  591.         minDistance = 12f;  
  592.   
  593.         maxPitch = 40;  
  594.         minPitch = 0;  
  595.         clampYawAngle = false;  
  596.         allowPanning = false;  
  597.     }  
  598.   
  599.     public void Cam_SellingPoint_BaoXianDeng()  
  600.     {  
  601.         minYaw = 10;  
  602.         maxYaw = 170;  
  603.         maxPitch = 60;  
  604.         minPitch = 1;  
  605.         minDistance = 1f;  
  606.         maxDistance = 10f;  
  607.   
  608.   
  609.         clampYawAngle = true;  
  610.         allowPanning = false;  
  611.     }  
  612.   
  613.     public void Cam_SellingPoint_TiaoShiMoKuai()  
  614.     {  
  615.         maxDistance = 5f;  
  616.         minDistance = 1f;  
  617.         maxPitch = 80;  
  618.         minPitch = 0;  
  619.   
  620.         clampYawAngle = false;  
  621.         allowPanning = false;  
  622.     }  
  623.   
  624.   
  625.     public void OnDistance(float dis)  
  626.     {  
  627.   
  628.         //Debug.Log("distance: "   dis);  
  629.         distance = dis;  
  630.         idealDistance = dis;  
  631.     }  
  632.   
  633.     public void OnViewAngle(Vector3 angle)  
  634.     {  
  635.   
  636.         //Debug.Log("angle: "   angle);  
  637.         camOrigin.eulerAngles = angle;  
  638.         yaw = idealYaw = angle.y;  
  639.         pitch = idealPitch = angle.x;  
  640.     }  
  641.   
  642.     public void OnComplete()  
  643.     {  
  644.         print("sdf");  
  645.     }  
  646.   
  647.     public void PanAxis(bool canX, bool canY)  
  648.     {  
  649.   
  650.         canPanX = canX;  
  651.         canPanY = canY;  
  652.     }  
  653.     public void OnPanOffset(Vector3 offset)  
  654.     {  
  655.   
  656.         panOffset = idealPanOffset = Vector3.zero;  
  657.         rPanOffset = offset;  
  658.     }  
  659. }  



以下是CameraControl代码
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class XgCameraControl : MonoBehaviour  
  5. {  
  6.     public delegate void TravelCallback();  
  7.     public delegate void CACProcess();  
  8.   
  9.     public float fl_Time;  
  10.     private float time_Shift = 0.8f;  
  11.     private float spd_Shift = 0.5f;  
  12.   
  13.     private static iTween.EaseType posEaseType = iTween.EaseType.easeOutQuad;  
  14.     private static iTween.EaseType rotEaseType = iTween.EaseType.easeOutQuad;  
  15.   
  16.     private XgCameraOrbit xGo;  
  17.   
  18.     private void Awake()  
  19.     {  
  20.         xGo = GameObject.Find("Main Camera").GetComponent();  
  21.     }  
  22.   
  23.     ///   
  24.     /// 设置相机的Yaw  
  25.     ///   
  26.     ///   
  27.     public void CamAutoCorrection(float yaw)  
  28.     {  
  29.         xGo.IdealYaw = yaw;  
  30.     }  
  31.   
  32.     ///   
  33.     /// 获取相机角度  
  34.     ///   
  35.     ///   
  36.     public float GetCamYawAngle()  
  37.     {  
  38.         return xGo.GetCamYawAngle();  
  39.     }  
  40.   
  41.     public float GetCamPitchAngle()  
  42.     {  
  43.         return xGo.GetCamPitchAngle();  
  44.     }  
  45.   
  46.   
  47.     public void TravelToMenu(Transform cam, Transform center, TravelCallback callback = nullfloat time = -1f)  
  48.     {  
  49.   
  50.         float hybirdPitch = 2f;  
  51.   
  52.         float distance = 9;  
  53.   
  54.         Vector3 rot = new Vector3(hybirdPitch, cam.eulerAngles.y, cam.eulerAngles.z);  
  55.   
  56.         Transform temp = GameObject.FindGameObjectWithTag("camRoot").transform;  
  57.   
  58.         Vector3 offset = Vector3.zero;  
  59.   
  60.         temp.eulerAngles = rot;  
  61.         Vector3 pos = center.position - temp.forward * distance   offset;  
  62.   
  63.         StopAllCoroutines();  
  64.   
  65.         time = time == -1f ? fl_Time : time;  
  66.   
  67.         StartCoroutine(xGo.PauseControl(fl_Time));  
  68.   
  69.         xGo.TeleportCam(center, pos, rot, distance, offset);  
  70.   
  71.         Travel(cam, pos, rot, time);  
  72.   
  73.         if (callback != null)  
  74.             StartCoroutine(Callback(time, callback));  
  75.     }  
  76.   
  77.     private IEnumerator Callback(float time, TravelCallback callback)  
  78.     {  
  79.         yield return new WaitForSeconds(time);  
  80.         callback();  
  81.     }  
  82.   
  83.     //public void TravelToPoint(Transform cam, Transform viewPoint, Transform center = null)  
  84.     //{  
  85.     //    if (!center)  
  86.     //        center = xGo.target.transform;  
  87.   
  88.     //    Vector3 pos = viewPoint.position;  
  89.   
  90.     //    viewPoint.LookAt(center);  
  91.   
  92.     //    Vector3 rot = viewPoint.eulerAngles;  
  93.   
  94.     //    Vector3 desiredPos = center.position - Vector3.Project(center.position - pos, viewPoint.forward);  
  95.   
  96.     //    float distance = Vector3.Distance(desiredPos, center.position);  
  97.   
  98.     //    //offset between new pos and the current position  
  99.     //    Vector3 offset = pos - desiredPos;  
  100.   
  101.     //    StopAllCoroutines();  
  102.   
  103.     //    StartCoroutine(xGo.PauseControl(fl_Time));  
  104.   
  105.     //    xGo.TeleportCam(center, desiredPos, rot, distance, offset);  
  106.   
  107.     //    Travel(cam, pos, rot, fl_Time);  
  108.     //}  
  109.   
  110.     public void TravelToPointNotDistance(Transform cam, Transform viewPoint, Transform tran_lookPos, Transform center = nullfloat time = -1f)  
  111.     {  
  112.         if (!center)  
  113.             center = xGo.target.transform;  
  114.         if (time == -1f)  
  115.             time = fl_Time;  
  116.   
  117.         Vector3 pos = viewPoint.position;  
  118.   
  119.         viewPoint.LookAt(tran_lookPos);  
  120.   
  121.         Vector3 rot = viewPoint.eulerAngles;  
  122.   
  123.         Vector3 desiredPos = center.position - Vector3.Project(center.position - pos, viewPoint.forward);  
  124.   
  125.         float distance = Vector3.Distance(desiredPos, center.position);  
  126.   
  127.         //offset between new pos and the current position  
  128.         Vector3 offset = pos - desiredPos;  
  129.   
  130.         StopAllCoroutines();  
  131.   
  132.         StartCoroutine(xGo.PauseControl(time));  
  133.   
  134.         xGo.TeleportCam(center, desiredPos, rot, distance, offset);  
  135.   
  136.         Travel(cam, pos, rot, time);  
  137.     }  
  138.   
  139.     public void TravelToDistance(Transform cam, float distance, Transform center = null)  
  140.     {  
  141.         if (!center)  
  142.             center = xGo.target.transform;  
  143.         Vector3 pos = center.position - (center.position - cam.position).normalized * distance;  
  144.         Vector3 rot = CamEulerAngles(center.position - cam.position);  
  145.   
  146.         StopAllCoroutines();  
  147.         StartCoroutine(xGo.PauseControl(fl_Time));  
  148.   
  149.         //Transform.ReferenceEquals  
  150.         xGo.TeleportCam(center, pos, rot, distance, Vector3.zero);  
  151.   
  152.         Travel(cam, pos, rot, fl_Time);  
  153.     }  
  154.   
  155.     public void TravelWithDistanceOffset(Transform cam, float distance, Vector3 offset, Transform center = null)  
  156.     {  
  157.         if (!center)  
  158.             center = xGo.target.transform;  
  159.   
  160.         Vector3 pos = center.position - (center.position - cam.position).normalized * distance;  
  161.         Vector3 rot = CamEulerAngles(center.position - cam.position);  
  162.   
  163.         StopAllCoroutines();  
  164.         StartCoroutine(xGo.PauseControl(fl_Time));  
  165.   
  166.         Transform temp = GameObject.FindGameObjectWithTag("camRoot").transform;  
  167.         temp.position = pos;  
  168.         temp.eulerAngles = rot;  
  169.   
  170.   
  171.         Vector3 v = temp.TransformPoint(offset);  
  172.   
  173.         xGo.TeleportCam(center, pos, rot, distance, offset);  
  174.   
  175.         Travel(cam, v, rot, fl_Time);  
  176.   
  177.     }  
  178.   
  179.   
  180.     public void TravelWithPointOffset(Transform cam, Transform viewPoint, Vector3 offset, Transform center = null)  
  181.     {  
  182.   
  183.         if (!center)  
  184.             center = xGo.target.transform;  
  185.   
  186.         Vector3 pos = viewPoint.position;  
  187.   
  188.         viewPoint.LookAt(center.position   offset);  
  189.   
  190.         Vector3 rot = viewPoint.eulerAngles;  
  191.   
  192.         Vector3 desiredPos = center.position - Vector3.Project(center.position - pos, viewPoint.forward);  
  193.   
  194.         float distance = Vector3.Distance(desiredPos, center.position);  
  195.   
  196.         StopAllCoroutines();  
  197.   
  198.         StartCoroutine(xGo.PauseControl(fl_Time));  
  199.   
  200.         xGo.TeleportCam(center, desiredPos, rot, distance, offset);  
  201.   
  202.         Travel(cam, pos, rot, fl_Time);  
  203.     }  
  204.   
  205.     //public void TravelToPoint(Transform cam, Transform pos,Transform center,Vector3 v3_Offset=default(Vector3))  
  206.     //{  
  207.     //    Transform camTrans = cam.transform;  
  208.   
  209.     //    Vector3 desiredPos = position - Vector3.Project(center.position - position, forward);  
  210.     //    float newDistance = Vector3.Distance(desiredPos, center.position);  
  211.     //    Vector3 newOffset = v3_Offset == Vector3.zero ? position - desiredPos : v3_Offset;  
  212.     //    Vector3 newViewAngle = eulerAngles;  
  213.   
  214.     //    Transform oldPivot = xGo.target;  
  215.     //    Vector3 oldViewAngle = camTrans.eulerAngles;  
  216.     //    //print(newViewAngle - oldViewAngle);  
  217.     //    float oldDistance = xGo.Distance;  
  218.     //    Vector3 oldOffset = xGo.ROffset;  
  219.   
  220.     //    float time = time_Shift;  
  221.   
  222.     //    StopAllCoroutines();  
  223.     //    xGo.PauseInput(time   0.1f);  
  224.   
  225.   
  226.     //    //shifting  
  227.     //    iTween.MoveTo(xGo.target.gameObject, iTween.Hash("position", center.position, "time", time, "easeType", posEaseType));  
  228.   
  229.     //    //shift view angles  
  230.     //    float anplyYDiff = oldViewAngle.y - newViewAngle.y;  
  231.     //    float anglyXDiff = oldViewAngle.x - newViewAngle.x;  
  232.   
  233.     //    if (Mathf.Abs(anplyYDiff) > 180)  
  234.     //        newViewAngle = new Vector3(newViewAngle.x, oldViewAngle.y   (anplyYDiff > 0 ? 360 - anplyYDiff : -(360   anplyYDiff)), newViewAngle.z);  
  235.   
  236.     //    if (Mathf.Abs(anglyXDiff) > 180)  
  237.     //        newViewAngle = new Vector3(oldViewAngle.x   (anglyXDiff > 0 ? 360 - anglyXDiff : -(360   anglyXDiff)), newViewAngle.y, newViewAngle.z);  
  238.   
  239.     //    iTween.ValueTo(cam.gameObject, iTween.Hash(  
  240.     //             "from", oldViewAngle,  
  241.     //             "to", newViewAngle,  
  242.     //             "time", time,  
  243.     //             "onupdatetarget", cam.gameObject,  
  244.     //             "onupdate", "OnViewAngle",  
  245.     //             "easetype", rotEaseType));  
  246.   
  247.     //    //shift view distance  
  248.     //    iTween.ValueTo(cam.gameObject, iTween.Hash(  
  249.     //        "from", oldDistance,  
  250.     //        "to", newDistance,  
  251.     //        "time", time,  
  252.     //        "onupdatetarget", cam.gameObject,  
  253.     //        "onupdate", "OnDistance",  
  254.     //        "easetype", rotEaseType));  
  255.   
  256.     //    //shift view offset  
  257.     //    iTween.ValueTo(cam.gameObject, iTween.Hash(  
  258.     //        "from", oldOffset,  
  259.     //        "to", newOffset,  
  260.     //        "time", time,  
  261.     //        "onupdatetarget", cam.gameObject,  
  262.     //        "onupdate", "OnPanOffset",  
  263.     //        "easetype", rotEaseType));  
  264.   
  265.     //}  
  266.   
  267.     public void TravelToPoint(Transform cam, Transform tran_Pos, Transform tran_Center, Vector3 v3_Offset = default(Vector3))  
  268.     {  
  269.         Transform camTrans = cam.transform;  
  270.   
  271.         Vector3 desiredPos = tran_Center.position - Vector3.Project(tran_Center.position - tran_Pos.position, tran_Pos.forward);  
  272.         float newDistance = Vector3.Distance(desiredPos, tran_Center.position);  
  273.         Vector3 newOffset = v3_Offset == Vector3.zero ? tran_Pos.position - desiredPos : v3_Offset;  
  274.         Vector3 newViewAngle = tran_Pos.eulerAngles;  
  275.   
  276.         Transform oldPivot = xGo.target;  
  277.         Vector3 oldViewAngle = camTrans.eulerAngles;  
  278.         //print(newViewAngle - oldViewAngle);  
  279.         float oldDistance = xGo.Distance;  
  280.         Vector3 oldOffset = xGo.ROffset;  
  281.   
  282.         float time = fl_Time;  
  283.   
  284.         StopAllCoroutines();  
  285.         xGo.PauseInput(time   0.1f);  
  286.   
  287.   
  288.         //shifting  
  289.         iTween.MoveTo(xGo.target.gameObject, iTween.Hash("position", tran_Center.position, "time", time, "easeType", posEaseType));  
  290.   
  291.         //shift view angles  
  292.         float anplyYDiff = oldViewAngle.y - newViewAngle.y;  
  293.         float anglyXDiff = oldViewAngle.x - newViewAngle.x;  
  294.   
  295.         if (Mathf.Abs(anplyYDiff) > 180)  
  296.             newViewAngle = new Vector3(newViewAngle.x, oldViewAngle.y   (anplyYDiff > 0 ? 360 - anplyYDiff : -(360   anplyYDiff)), newViewAngle.z);  
  297.   
  298.         if (Mathf.Abs(anglyXDiff) > 180)  
  299.             newViewAngle = new Vector3(oldViewAngle.x   (anglyXDiff > 0 ? 360 - anglyXDiff : -(360   anglyXDiff)), newViewAngle.y, newViewAngle.z);  
  300.   
  301.         iTween.ValueTo(cam.gameObject, iTween.Hash(  
  302.                  "from", oldViewAngle,  
  303.                  "to", newViewAngle,  
  304.                  "time", time,  
  305.                  "onupdatetarget", cam.gameObject,  
  306.                  "onupdate""OnViewAngle",  
  307.                  "easetype", rotEaseType));  
  308.   
  309.         //shift view distance  
  310.         iTween.ValueTo(cam.gameObject, iTween.Hash(  
  311.             "from", oldDistance,  
  312.             "to", newDistance,  
  313.             "time", time,  
  314.             "onupdatetarget", cam.gameObject,  
  315.             "onupdate""OnDistance",  
  316.             "easetype", rotEaseType));  
  317.   
  318.         //shift view offset  
  319.         iTween.ValueTo(cam.gameObject, iTween.Hash(  
  320.             "from", oldOffset,  
  321.             "to", newOffset,  
  322.             "time", time,  
  323.             "onupdatetarget", cam.gameObject,  
  324.             "onupdate""OnPanOffset",  
  325.             "easetype", rotEaseType));  
  326.   
  327.     }  
  328.   
  329.   
  330.     private Vector3 CamEulerAngles(Vector3 forward)  
  331.     {  
  332.   
  333.         Quaternion q = Quaternion.LookRotation(forward);  
  334.         //Vector3 v = (fromPos - toPos).normalized;  
  335.         //Quaternion q = camTrams.rotation;  
  336.         //q.SetLookRotation(camTrams.forward, toPos);  
  337.   
  338.         //Quaternion.FromToRotation(Vector3.up, transform.forward);  
  339.   
  340.         return q.eulerAngles;  
  341.     }  
  342.   
  343.     //public void TravelToPoint(Transform tran_Point, Transform center = null, Transform tran_Cam = null)  
  344.     //{  
  345.     //    if (!center)  
  346.     //        center = xGo.target.transform;  
  347.     //    if (!tran_Cam)  
  348.     //        tran_Cam = transform;  
  349.   
  350.     //    Vector3 v3_Pos = tran_Point.position;  
  351.     //    Vector3 v3_Rot = tran_Point.eulerAngles;  
  352.   
  353.     //    Vector3 v3_DesiredPos = center.position -  
  354.     //                            Vector3.Project(center.position - v3_Pos, tran_Point.forward);  
  355.   
  356.     //    float fl_Distance = Vector3.Distance(v3_Pos, center.position);  
  357.   
  358.     //    Vector3 v3_Offset = v3_Pos - v3_DesiredPos;  
  359.   
  360.     //    StopAllCoroutines();  
  361.   
  362.     //    StartCoroutine(PauseMove(fl_Time));  
  363.   
  364.     //    xGo.TeleportCam(center, v3_DesiredPos, v3_Rot, fl_Distance, v3_Offset);  
  365.   
  366.     //    Travel(tran_Cam, v3_Pos, v3_Rot);  
  367.     //}  
  368.   
  369.     private void Travel(Transform tran_Cam, Vector3 v3_Pos, Vector3 v3_Rot, float time)  
  370.     {  
  371.         XgControlTools.Move(tran_Cam, v3_Pos, time);  
  372.         XgControlTools.Rotate(tran_Cam, v3_Rot, time);  
  373.     }  
  374. }  
  375.   
  376. [System.Serializable]  
  377. public class TravelPosition  
  378. {  
  379.     public Transform tran_Center;  
  380.     public Transform tran_Pos;  
  381.     public Vector3 v3_Offset;  
  382. }  

Manger控制

[csharp] view plain copy
  1. <pre name=< span="">"code" class="csharp">using UnityEngine;  </pre name=<>
  2. using System.Collections;  
  3.   
  4. public class XgCameraManager : MonoBehaviour  
  5. {  
  6.   
  7.     private string str_Control = "ScriptsControl";  
  8.     //public string str_OverAllControlName = "S1";  
  9.     //public string str_OptionalControlName = "S2";  
  10.     //public string str_CoreComControlName = "S3";  
  11.     //public string str_SellingPointControlName = "S4";  
  12.     [HideInInspector]  
  13.     public MainControl main_Control;  
  14.   
  15.     //private S1_Control s1_Control;  
  16.     //private S2_Control s2_Control;  
  17.     //private S4_Control s4_Control;  
  18.     public void Init_CameraManager()  
  19.     {  
  20.         main_Control = GameObject.Find(str_Control).GetComponent();  
  21.         //s1_Control = GameObject.Find(str_OverAllControlName).GetComponent();  
  22.         //s2_Control = GameObject.Find(str_OptionalControlName).GetComponent();  
  23.         //s4_Control = GameObject.Find(str_SellingPointControlName).GetComponent();  
  24.     }  
  25.   
  26.     public void OnManagerTap(TapGesture gesture)  
  27.     {  
  28.         if (gesture.Selection.layer == 10 && UICamera.isOverUI == false)  
  29.         {  
  30.             main_Control.MoveToPoint1(0);  
  31.         }  
  32.         if (gesture.Selection.layer == 11 && UICamera.isOverUI == false)  
  33.         {  
  34.             main_Control.MoveToPoint1(1);  
  35.         }  
  36.         if (gesture.Selection.layer == 12 && UICamera.isOverUI == false)  
  37.         {  
  38.             main_Control.MoveToPoint1(2);  
  39.         }  
  40.         if (gesture.Selection.layer == 13 && UICamera.isOverUI == false)  
  41.         {  
  42.             main_Control.MoveToPoint1(3);  
  43.         }  
  44.         if (gesture.Selection.layer == 14 && UICamera.isOverUI == false)  
  45.         {  
  46.             main_Control.MoveToPoint1(4);  
  47.         }  
  48.         //if (gesture.Selection.layer == 15 && UICamera.isOverUI == false)  
  49.         //{  
  50.         //    main_Control.MoveToPoint1(5);  
  51.         //}  
  52.         if (gesture.Selection.layer == 16 && UICamera.isOverUI == false)  
  53.         {  
  54.             main_Control.MoveToPoint1(5);  
  55.         }  
  56.         if (gesture.Selection.layer == 17 && UICamera.isOverUI == false)  
  57.         {  
  58.             main_Control.MoveToPoint1(6);  
  59.         }  
  60.         if (gesture.Selection.layer == 18 && UICamera.isOverUI == false)  
  61.         {  
  62.             main_Control.HotPointAnim(0);  
  63.         }  
  64.         if (gesture.Selection.layer == 19 && UICamera.isOverUI == false)  
  65.         {  
  66.             main_Control.HotPointAnim(1);  
  67.         }  
  68.         if (gesture.Selection.layer == 20 && UICamera.isOverUI == false)  
  69.         {  
  70.             main_Control.HotPointAnim(2);  
  71.         }  
  72.   
  73.     }  
  74. }  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引