From 7fce11c5a47a6c4879b536ac0f706e19adcf715f Mon Sep 17 00:00:00 2001 From: hyskai <2545507770@qq.com> Date: Sat, 19 Oct 2024 16:54:28 +0800 Subject: [PATCH] add --- .../develop_the_main_interface_slices.unity | 289 +++++++++++++++++- .../Assets/communal/camera/CameraControl.cs | 12 +- .../communal/camera/Cinemachine_2dCon.cs | 165 ++++++++++ .../communal/camera/Cinemachine_2dCon.cs.meta | 11 + 4 files changed, 465 insertions(+), 12 deletions(-) create mode 100644 meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs create mode 100644 meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs.meta diff --git a/meng_yao/Assets/Scenes/develop_the_main_interface_slices.unity b/meng_yao/Assets/Scenes/develop_the_main_interface_slices.unity index 450372fa..dad0831d 100644 --- a/meng_yao/Assets/Scenes/develop_the_main_interface_slices.unity +++ b/meng_yao/Assets/Scenes/develop_the_main_interface_slices.unity @@ -708,6 +708,68 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 253757490} m_CullTransparentMesh: 1 +--- !u!1 &264973825 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 264973827} + - component: {fileID: 264973826} + m_Layer: 0 + m_Name: edg + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!60 &264973826 +PolygonCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 264973825} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0, y: 0} + oldSize: {x: 0, y: 0} + newSize: {x: 0, y: 0} + adaptiveTilingThreshold: 0 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + m_Points: + m_Paths: + - - {x: 5.8125525, y: 11.065638} + - {x: -5.4066706, y: 11.083502} + - {x: -5.3876967, y: -8.849378} + - {x: 5.792753, y: -8.869627} + - {x: 5.872485, y: 0.8558425} +--- !u!4 &264973827 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 264973825} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -0.21272701, y: -1.0854781, z: -21.766853} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 5 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &334655375 GameObject: m_ObjectHideFlags: 0 @@ -975,7 +1037,7 @@ GameObject: - component: {fileID: 524602613} - component: {fileID: 524602612} - component: {fileID: 524602611} - - component: {fileID: 524602614} + - component: {fileID: 524602615} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -1042,14 +1104,14 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 524602610} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalPosition: {x: -0.00029921532, y: 0.02265215, z: -10} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &524602614 +--- !u!114 &524602615 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -1058,11 +1120,31 @@ MonoBehaviour: m_GameObject: {fileID: 524602610} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 4efefbc5a63231543b64d1633b583230, type: 3} + m_Script: {fileID: 11500000, guid: 72ece51f2901e7445ab60da3685d6b5f, type: 3} m_Name: m_EditorClassIdentifier: - cameraMaxSize: 10 - cameraMinSize: 5 + m_ShowDebugText: 0 + m_ShowCameraFrustum: 1 + m_IgnoreTimeScale: 0 + m_WorldUpOverride: {fileID: 0} + m_UpdateMethod: 2 + m_BlendUpdateMethod: 1 + m_DefaultBlend: + m_Style: 1 + m_Time: 2 + m_CustomCurve: + serializedVersion: 2 + m_Curve: [] + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + m_CustomBlends: {fileID: 0} + m_CameraCutEvent: + m_PersistentCalls: + m_Calls: [] + m_CameraActivatedEvent: + m_PersistentCalls: + m_Calls: [] --- !u!1 &557092423 GameObject: m_ObjectHideFlags: 0 @@ -1688,6 +1770,94 @@ Transform: m_Father: {fileID: 1077006413} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1095284821 +GameObject: + m_ObjectHideFlags: 3 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1095284822} + - component: {fileID: 1095284824} + - component: {fileID: 1095284823} + m_Layer: 0 + m_Name: cm + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1095284822 +Transform: + m_ObjectHideFlags: 3 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1095284821} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 2115856299} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1095284823 +MonoBehaviour: + m_ObjectHideFlags: 3 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1095284821} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6ad980451443d70438faac0bc6c235a0, type: 3} + m_Name: + m_EditorClassIdentifier: + m_TrackedObjectOffset: {x: 0, y: 0, z: 0} + m_LookaheadTime: 0 + m_LookaheadSmoothing: 0 + m_LookaheadIgnoreY: 0 + m_XDamping: 1 + m_YDamping: 1 + m_ZDamping: 1 + m_TargetMovementOnly: 1 + m_ScreenX: 0.5 + m_ScreenY: 0.5 + m_CameraDistance: 10 + m_DeadZoneWidth: 0 + m_DeadZoneHeight: 0 + m_DeadZoneDepth: 0 + m_UnlimitedSoftZone: 0 + m_SoftZoneWidth: 0.8 + m_SoftZoneHeight: 0.8 + m_BiasX: 0 + m_BiasY: 0 + m_CenterOnActivate: 1 + m_GroupFramingMode: 2 + m_AdjustmentMode: 0 + m_GroupFramingSize: 0.8 + m_MaxDollyIn: 5000 + m_MaxDollyOut: 5000 + m_MinimumDistance: 1 + m_MaximumDistance: 5000 + m_MinimumFOV: 3 + m_MaximumFOV: 60 + m_MinimumOrthoSize: 1 + m_MaximumOrthoSize: 5000 +--- !u!114 &1095284824 +MonoBehaviour: + m_ObjectHideFlags: 3 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1095284821} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ac0b09e7857660247b1477e93731de29, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1108455072 GameObject: m_ObjectHideFlags: 0 @@ -3694,6 +3864,113 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &2115856297 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2115856299} + - component: {fileID: 2115856298} + - component: {fileID: 2115856300} + - component: {fileID: 2115856301} + m_Layer: 0 + m_Name: Virtual Camera + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &2115856298 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2115856297} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 45e653bab7fb20e499bda25e1b646fea, type: 3} + m_Name: + m_EditorClassIdentifier: + m_ExcludedPropertiesInInspector: + - m_Script + m_LockStageInInspector: + m_StreamingVersion: 20170927 + m_Priority: 16 + m_StandbyUpdate: 2 + m_LookAt: {fileID: 0} + m_Follow: {fileID: 0} + m_Lens: + FieldOfView: 60 + OrthographicSize: 10 + NearClipPlane: 0.3 + FarClipPlane: 1000 + Dutch: 0 + ModeOverride: 0 + LensShift: {x: 0, y: 0} + GateFit: 2 + m_SensorSize: {x: 1, y: 1} + m_Transitions: + m_BlendHint: 0 + m_InheritPosition: 0 + m_OnCameraLive: + m_PersistentCalls: + m_Calls: [] + m_LegacyBlendHint: 0 + m_ComponentOwner: {fileID: 1095284822} +--- !u!4 &2115856299 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2115856297} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 1095284822} + m_Father: {fileID: 0} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2115856300 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2115856297} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a2fba25a5cd15594e8f050a11e386c80, type: 3} + m_Name: + m_EditorClassIdentifier: + m_ConfineMode: 0 + m_BoundingVolume: {fileID: 0} + m_BoundingShape2D: {fileID: 264973826} + m_ConfineScreenEdges: 1 + m_Damping: 0 +--- !u!114 &2115856301 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2115856297} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b45d990875b62d948af35ad97c764820, type: 3} + m_Name: + m_EditorClassIdentifier: + cameraMaxSize: 10 + cameraMinSize: 5 + zoomSpeed: 0.5 + v_cam: {fileID: 2115856298} + m_cam: {fileID: 524602612} --- !u!1 &2116091709 GameObject: m_ObjectHideFlags: 0 diff --git a/meng_yao/Assets/communal/camera/CameraControl.cs b/meng_yao/Assets/communal/camera/CameraControl.cs index cd642d2e..ca6aa464 100644 --- a/meng_yao/Assets/communal/camera/CameraControl.cs +++ b/meng_yao/Assets/communal/camera/CameraControl.cs @@ -1,18 +1,19 @@ using UnityEngine; -[RequireComponent(typeof(Camera))] +//[RequireComponent(typeof(Camera))] -// public class CameraControl : MonoBehaviour { public float cameraMaxSize; public float cameraMinSize; - private Camera m_cam; + public Camera m_cam; private Transform m_selfTrans; private bool m_fingerDown = false; + + /// /// 单指滑动的手指位置 /// @@ -24,8 +25,7 @@ public class CameraControl : MonoBehaviour private void Awake() - { - m_cam = GetComponent(); + { m_selfTrans = transform; } @@ -121,7 +121,7 @@ public class CameraControl : MonoBehaviour /// private void ScaleCamere(float scale) { - m_cam.orthographicSize -= scale * 0.1f; + m_cam.orthographicSize -= scale * 0.01f; if (m_cam.orthographicSize < cameraMinSize) { m_cam.orthographicSize = cameraMinSize; diff --git a/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs b/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs new file mode 100644 index 00000000..07de49ae --- /dev/null +++ b/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs @@ -0,0 +1,165 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Cinemachine; + +public class Cinemachine_2dCon : MonoBehaviour +{ + public float cameraMaxSize; + public float cameraMinSize; + public float zoomSpeed; + + public CinemachineVirtualCamera v_cam; + public Camera m_cam; + private Transform m_selfTrans; + private bool m_fingerDown = false; + + + + + /// + /// 单指滑动的手指位置 + /// + private Vector3 m_oneFingerDragStartPos; + /// + /// 双指缩放的上一帧的双指距离 + /// + private float m_twoFingerLastDistance = -1; + + + private void Awake() + { + m_selfTrans = transform; + } + + private void Update() + { +#if UNITY_EDITOR || UNITY_STANDALONE + // 桌面端:鼠标按下时 + if (Input.GetMouseButtonDown(0)) + { + m_fingerDown = true; + m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition); // 获取鼠标按下的起始位置 + } + + // 桌面端:鼠标松开时 + if (Input.GetMouseButtonUp(0)) + { + m_fingerDown = false; + m_twoFingerLastDistance = -1; + } + + // 处理鼠标拖动 + if (m_fingerDown) + { + HandleFingerDragMove(Input.mousePosition); + } + + // 处理鼠标滚轮缩放 + var distance = Input.GetAxis("Mouse ScrollWheel"); + HandleMouseScrollWheel(distance * 10); + +#else + // 移动端:双指缩放 + if (Input.touchCount == 2) + { + HandleTwoFingerScale(); + m_fingerDown = false; // 避免双指缩放时误触单指拖动 + } + // 移动端:单指滑动 + else if (Input.touchCount == 1) + { + Touch touch = Input.touches[0]; + + // 手指刚触碰屏幕时 + if (touch.phase == TouchPhase.Began) + { + m_fingerDown = true; + m_oneFingerDragStartPos = GetWorldPos(touch.position); // 获取手指按下的起始位置 + } + // 手指在屏幕上滑动时 + else if (touch.phase == TouchPhase.Moved) + { + HandleFingerDragMove(touch.position); // 处理手指拖动 + } + + m_twoFingerLastDistance = -1; // 复位双指缩放距离 + } + else + { + m_fingerDown = false; + m_twoFingerLastDistance = -1; + } +#endif + } + + + /// + /// 单指滑动 + /// + private void HandleFingerDragMove(Vector2 fingerPos) + { + //滑动差 + Vector3 cha = m_oneFingerDragStartPos - GetWorldPos(fingerPos); + Vector3 newP = m_cam.transform.position; + newP.x = newP.x + cha.x; + newP.y = newP.y + cha.y; + m_selfTrans.position = newP; + } + + /// + /// 双指缩放 + /// + private void HandleTwoFingerScale() + { + float distance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); + if (m_twoFingerLastDistance == -1) + { + m_twoFingerLastDistance = distance; + return; // 避免首次执行时发生误缩放 + } + + // 计算缩放比例 + float scale = 0.1f * (distance - m_twoFingerLastDistance); + ScaleCamere(scale); + + m_twoFingerLastDistance = distance; + } + + /// + /// 鼠标滚轮缩放 + /// + /// + private void HandleMouseScrollWheel(float distance) + { + if (0 == distance) return; + ScaleCamere(distance); + } + + /// + /// 缩放摄像机的视口 + /// + /// + private void ScaleCamere(float scale) + { + v_cam.m_Lens.OrthographicSize -= scale * zoomSpeed; + if (v_cam.m_Lens.OrthographicSize < cameraMinSize) + { + v_cam.m_Lens.OrthographicSize = cameraMinSize; + } + if (v_cam.m_Lens.OrthographicSize > cameraMaxSize) + { + v_cam.m_Lens.OrthographicSize = cameraMaxSize; + } + } + + /// + /// 屏幕坐标换算成3D坐标 + /// + /// 屏幕坐标 + /// + Vector3 GetWorldPos(Vector2 screenPos) + { + return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(m_cam.transform.position.z))); + } +} diff --git a/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs.meta b/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs.meta new file mode 100644 index 00000000..20f6bb83 --- /dev/null +++ b/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b45d990875b62d948af35ad97c764820 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: