diff options
author | chai <chaifix@163.com> | 2021-09-20 00:42:33 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-09-20 00:42:33 +0800 |
commit | 02b44c07adfcf921da594120b4cd8fc18b982725 (patch) | |
tree | 723e001ed8c5f7c39419cc4a50a3202a0cf59961 /Assets/Scripts/Camera/MainCamera.cs | |
parent | d4581317f904b870c482a3274e7cc47d1732a673 (diff) |
+command buffer
Diffstat (limited to 'Assets/Scripts/Camera/MainCamera.cs')
-rw-r--r-- | Assets/Scripts/Camera/MainCamera.cs | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/Assets/Scripts/Camera/MainCamera.cs b/Assets/Scripts/Camera/MainCamera.cs new file mode 100644 index 00000000..f439cf22 --- /dev/null +++ b/Assets/Scripts/Camera/MainCamera.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +// 主相机 +[ExecuteAlways] +public class MainCamera : SingletonMB<MainCamera> +{ + #region inspector + [Serializable] + public struct Region + { + [SerializeField] public float l; + [SerializeField] public float r; + [SerializeField] public float t; + [SerializeField] public float b; + } + + public new Camera camera { get; private set; } + + public Vector3 offset = Vector3.zero; + + [SerializeField] private float z; + + [SerializeField] private Region region; + + [SerializeField] private float moveSpeed; + + [SerializeField] private AnimationCurve speedCurve; + + [SerializeField] private float threshold; + #endregion + + private LensEffectHandler m_LensEffectHandler; + public LensEffectHandler lensEffectHandler + { + get + { + if (m_LensEffectHandler == null) + m_LensEffectHandler = this.gameObject.GetOrAddComponent<LensEffectHandler>(); + return m_LensEffectHandler; + } + } + + private Vector2 position2D + { + get + { + return transform.position; + } + set + { + transform.position = new Vector3(value.x, value.y, transform.position.z); + } + } + + private Vector3 position + { + get + { + return transform.position; + } + set + { + transform.position = value; + } + } + + protected override void Awake() + { + base.Awake(); + camera = gameObject.GetComponent<Camera>(); + } + + private void Start() + { + Vector3 pos = transform.position; + pos.z = this.z; + transform.position = pos; + + Quaternion rot = Quaternion.LookRotation(Vector3.forward); + transform.rotation = rot; + } + + void FollowPlayerCharacter() + { + if (UnitManager.Instance == null || UnitManager.Instance.pc == null) + return; + + Region region = this.region; + + //if (!UnitManager.Instance.pc.isTowardRight) + //{ + // region.l = -this.region.r; + // region.r = -this.region.l; + //} + + Vector3 camPos = position; + + Vector3 worldPos = UnitManager.Instance.pc.transform.position + offset; + + Vector2 viewPos = ((Vector2)camera.WorldToViewportPoint(worldPos)) * 2 - Vector2.one; + + Vector3 dir = (worldPos - camPos).normalized; + float dest = (worldPos - camPos).magnitude; + float curve = speedCurve.Evaluate((threshold - Mathf.Clamp(dest, 0f, threshold)) / threshold); + Vector3 move = curve * dir * moveSpeed * Time.deltaTime; + + if (IsInRegion(viewPos, region)) + { + if(!IsInCenter(viewPos)) + { + position2D = camPos + move; + if((worldPos - position).magnitude <= 0.1f) + { + position2D = worldPos; + } + } + return; + } + position2D = camPos + move; + if ((worldPos - position).magnitude <= 0.1f) + { + position2D = worldPos; + } + } + + bool IsInRegion(Vector2 p, Region region) + { + return p.x > region.l + && p.x < region.r + && p.y > region.b + && p.y < region.t; + } + + bool IsInCenter(Vector2 p) + { + return p.x == (region.l + region.r) / 2 && p.y == (region.t + region.b) / 2; + } + + void Update() + { + FollowPlayerCharacter(); + } + + public void OnGUI() + { + if(MainCameraDebug.OnGUIHandlers != null) + MainCameraDebug.OnGUIHandlers.Invoke(); + } + +}
\ No newline at end of file |