diff --git a/BulkCargo_UnityProject/Assets/Scripts/Data/EnumBase.cs b/BulkCargo_UnityProject/Assets/Scripts/Data/EnumBase.cs index 88191b23..d6340f3e 100644 --- a/BulkCargo_UnityProject/Assets/Scripts/Data/EnumBase.cs +++ b/BulkCargo_UnityProject/Assets/Scripts/Data/EnumBase.cs @@ -59,4 +59,14 @@ public enum Purpose { 进货 = 1, 出货 = 2 +} + +/// +/// +/// +public enum LookType +{ + PATH = 5, + TRANSFORM, + POINT, } \ No newline at end of file diff --git a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/CameraRoamManager.cs b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/CameraRoamManager.cs index 44311850..b05e1c49 100644 --- a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/CameraRoamManager.cs +++ b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/CameraRoamManager.cs @@ -70,10 +70,26 @@ public class CameraRoamManager : Singleton [Range(0, 1)] public float look = 0; + /// + /// 约束X轴 + /// public bool ConstraintXAxis; + /// + /// 约束Y轴 + /// public bool ConstraintYAxis; + /// + /// 约束Z轴 + /// public bool ConstraintZAxis; + /// + /// 约束值 + /// public Vector3 ConstraintValue; + ///// + ///// 漫游相机查看类型 + ///// + //public LookType LookType; // Start is called before the first frame update void Start() { @@ -130,51 +146,227 @@ public class CameraRoamManager : Singleton /// public void AutoRoaming(bool _roaming, bool _looping = false, bool _roam_continue = true, Action _roming_finish_callback = null) { - isRoaming = _roaming; + #region Old + //isRoaming = _roaming; + //if (_roaming) + //{ + // CameraMove.Instance.ShieldingOperation = true; + // //判断是否继续上次的漫游 + // if (_roam_continue) + // { + // if (m_RoamingTween != null) + // { + // TargetCamera.eulerAngles = Vector3.zero; + // m_RoamingTween.Play(); + // return; + // } + // } + + // //重新开始漫游 + + // if (BezierDrawer.UseCurvePath) + // { + // if (BezierCurve == null) + // { + // BezierCurve = new BezierCurve(m_RoamingPoints, BezierDrawer != null ? BezierDrawer.accuracy : 10); + // } + // TargetCamera.position = BezierCurve.CurvePoints[0]; + // TargetCamera.LookAt(BezierCurve.CurvePoints[1]); + // m_RoamingTween = TargetCamera.DOPath(BezierCurve.CurvePoints, BezierCurve.CurveLength / RoamingSpeed).SetEase(Ease.Linear).SetLoops(_looping ? -1 : 0).SetLookAt(look).OnComplete(() => { _roming_finish_callback?.Invoke(); }); + // } + // else + // { + // TargetCamera.position = m_RoamingPoints[0]; + // TargetCamera.LookAt(m_RoamingPoints[1]); + // m_RoamingTween = TargetCamera.DOPath(m_RoamingPoints, RoamingPathLength / RoamingSpeed).SetEase(Ease.Linear).SetLoops(_looping ? -1 : 0).SetLookAt(look).OnComplete(() => { _roming_finish_callback?.Invoke(); }); + // } + + //} + //else + //{ + // if (m_RoamingTween != null) + // { + // if (_roam_continue) + // { + // m_RoamingTween.Pause(); + // } + // } + // CameraMove.Instance.ReInitTransform(); + //} + #endregion if (_roaming) { CameraMove.Instance.ShieldingOperation = true; - //判断是否继续上次的漫游 - if (_roam_continue) - { - if (m_RoamingTween != null) - { - TargetCamera.eulerAngles = Vector3.zero; - m_RoamingTween.Play(); - return; - } - } - - //重新开始漫游 - - if (BezierDrawer.UseCurvePath) - { - if (BezierCurve == null) - { - BezierCurve = new BezierCurve(m_RoamingPoints, BezierDrawer != null ? BezierDrawer.accuracy : 10); - } - TargetCamera.position = BezierCurve.CurvePoints[0]; - TargetCamera.LookAt(BezierCurve.CurvePoints[1]); - m_RoamingTween = TargetCamera.DOPath(BezierCurve.CurvePoints, BezierCurve.CurveLength / RoamingSpeed).SetEase(Ease.Linear).SetLoops(_looping ? -1 : 0).SetLookAt(look).OnComplete(() => { _roming_finish_callback?.Invoke(); }); - } - else - { - TargetCamera.position = m_RoamingPoints[0]; - TargetCamera.LookAt(m_RoamingPoints[1]); - m_RoamingTween = TargetCamera.DOPath(m_RoamingPoints, RoamingPathLength / RoamingSpeed).SetEase(Ease.Linear).SetLoops(_looping ? -1 : 0).SetLookAt(look).OnComplete(() => { _roming_finish_callback?.Invoke(); }); - } - + Coroutine = StartCoroutine(PathMove(-1, LoopStartPoint, true)); } else { - if (m_RoamingTween != null) - { - if (_roam_continue) - { - m_RoamingTween.Pause(); - } - } + StopCoroutine(Coroutine); CameraMove.Instance.ReInitTransform(); } + isRoaming = _roaming; + } + /// + /// 路径移动协程 + /// + Coroutine Coroutine; + /// + /// 等待时间 + /// + WaitForSeconds WaitForSeconds = null; + /// + /// 模拟帧率 + /// + float _frame = 0.02f; + /// + /// 下一个路径点 + /// + int _Next_Index = -1; + /// + /// 实时路径点 + /// + List _Anchor_Point = new List(); + /// + /// 计算循环点 + /// + bool _init_anchor_points = false; + /// + /// 漫游是否已开始 + /// + bool _roaming_started = false; + /// + /// + /// + public int LoopStartPoint = 3; + + /// + /// + /// + /// + /// 循环起始点 + /// 是否继续漫游 + /// + IEnumerator PathMove(int _loop, int _loop_point = 0, bool _roam_continue = false) + { + int _loop_time = 0; + + if (WaitForSeconds == null) + { + WaitForSeconds = new WaitForSeconds(_frame); + } + + if (!_roaming_started) + { + ForceInit(); + _roaming_started = true; + } + else + { + //初始化 + if (_roam_continue) + { + TargetCamera.position = m_Position; + TargetCamera.rotation = m_Rotation; + } + else + { + ForceInit(); + } + } + + + bool _at_point = false;//是否完成当此Lerp + var _pos = TargetCamera.position;//每次Lerp的基坐标 + Vector3 _rt_pos;//实时坐标 + float _t = 0.1f;//Lerp频次 + float _to = 0;//Lerp进度 + float _lerp_speed = 10;//Lerp速度 + + + while (_loop < 0 ? true : _loop_time <= _loop)//判断循环次数 + { + _loop_time++; + + if (_at_point) + { + if (_Next_Index < _Anchor_Point.Count - 1) + { + _Next_Index += 1; + } + else + { + _Next_Index = 0; + if (!_init_anchor_points && _loop_point > 0 && _loop_point < m_RoamingAnchors.Count - 2) + { + _init_anchor_points = true; + _Anchor_Point.Clear(); + for (int i = _loop_point; i < m_RoamingAnchors.Count; i++) + { + _Anchor_Point.Add(m_RoamingAnchors[i]); + } + } + } + _pos = TargetCamera.position; + _to = 0; + } + + _to += _t; + _rt_pos = Lerp(_pos, _Anchor_Point[_Next_Index].Position, _to / Vector3.Distance(_pos, _Anchor_Point[_Next_Index].Position) * _lerp_speed); + TargetCamera.position = _rt_pos; + _at_point = TargetCamera.position == _Anchor_Point[_Next_Index].Position; + + switch (_Anchor_Point[_Next_Index].LookType) + { + case LookType.PATH: + TargetCamera.LookAt(_Anchor_Point[_Next_Index].Position); + break; + case LookType.TRANSFORM: + TargetCamera.LookAt(_Anchor_Point[_Next_Index].LookTransform); + break; + case LookType.POINT: + TargetCamera.LookAt(_Anchor_Point[_Next_Index].LookPoint); + break; + default: + break; + } + + yield return WaitForSeconds; + } + yield return null; + } + + private void ForceInit() + { + TargetCamera.position = m_RoamingAnchors[0].Position; + switch (m_RoamingAnchors[0].LookType) + { + case LookType.PATH: + TargetCamera.LookAt(m_RoamingAnchors[1].Position); + break; + case LookType.TRANSFORM: + TargetCamera.LookAt(m_RoamingAnchors[1].LookTransform); + break; + case LookType.POINT: + TargetCamera.LookAt(m_RoamingAnchors[1].LookPoint); + break; + default: + break; + } + _Next_Index = 1; + _Anchor_Point = new List(m_RoamingAnchors); + } + + public Vector3 Lerp(Vector3 _a, Vector3 _b, float _t) + { + if (_t <= 0) + { + return _a; + } + else if (_t >= 1) + { + return _b; + } + + return _a + (_b - _a) * _t; } } diff --git a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor.meta b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor.meta new file mode 100644 index 00000000..39af8c9d --- /dev/null +++ b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1dd3b57be54dc384c9941537ee3c134d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor/RoamingAnchorEditor.cs b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor/RoamingAnchorEditor.cs new file mode 100644 index 00000000..41548ca7 --- /dev/null +++ b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor/RoamingAnchorEditor.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; + +[CustomEditor(typeof(RoamingAnchor)),CanEditMultipleObjects] +public class RoamingAnchorEditor : Editor +{ + RoamingAnchor _target; + SerializedProperty LookTransform; + SerializedProperty LookPoint; + SerializedProperty LookType; + + private void OnEnable() + { + _target = (RoamingAnchor)target; + + LookTransform = serializedObject.FindProperty("LookTransform"); + LookPoint = serializedObject.FindProperty("LookPoint"); + LookType = serializedObject.FindProperty("LookType"); + } + + public override void OnInspectorGUI() + { + //base.OnInspectorGUI(); + serializedObject.Update(); + + EditorGUILayout.PropertyField(LookType); + switch (_target.LookType) + { + case global::LookType.PATH: + break; + case global::LookType.TRANSFORM: + EditorGUILayout.PropertyField(LookTransform); + break; + case global::LookType.POINT: + EditorGUILayout.PropertyField(LookPoint); + break; + default: + break; + } + + serializedObject.ApplyModifiedProperties(); + } +} diff --git a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor/RoamingAnchorEditor.cs.meta b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor/RoamingAnchorEditor.cs.meta new file mode 100644 index 00000000..b2e5aafb --- /dev/null +++ b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/Editor/RoamingAnchorEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9ac8cd38c5ff6774ab31c68b6e6fdb0d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/RoamingAnchor.cs b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/RoamingAnchor.cs index 64aaa675..66b7bdbe 100644 --- a/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/RoamingAnchor.cs +++ b/BulkCargo_UnityProject/Assets/Scripts/wj/Camera/RoamingAnchor.cs @@ -1,33 +1,38 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoamingAnchor : MonoBehaviour -{ - /// - /// 锚点编号 - /// - public int Index; - /// - /// 视角 - /// - public Vector3 Angle; - /// - /// 位置 - /// - public Vector3 Position => transform.position; - /// - /// 路径用时 - /// - public float Duration; - - //public void Init() - // { - // Position = transform.position; - // } - - //public void OnDrawGizmos() - //{ - // Gizmos.DrawGUITexture() - //} -} +using JetBrains.Annotations; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoamingAnchor : MonoBehaviour +{ + /// + /// 锚点编号 + /// + public int Index; + /// + /// 视角 + /// + public Vector3 Angle; + /// + /// 位置 + /// + public Vector3 Position => transform.position; + /// + /// 路径用时 + /// + public float Duration; + + public Transform LookTransform; + public Vector3 LookPoint; + + public LookType LookType = LookType.PATH; + //public void Init() + // { + // Position = transform.position; + // } + + //public void OnDrawGizmos() + //{ + // Gizmos.DrawGUITexture() + //} +}