From 1b2aca94cd507abbaa22f8c80b3f7fa68f9942b4 Mon Sep 17 00:00:00 2001 From: Jenny Crowe Date: Thu, 3 Feb 2022 01:25:27 -0700 Subject: [PATCH] Minimizing Camera.main references --- Assets/Scripts/Common/FollowMouse.cs | 9 ++++++++- Assets/Scripts/Common/Parallax.cs | 9 ++++++++- .../Scripts/LevelEditor/Timeline/TempoTimeline.cs | 6 +++--- .../LevelEditor/Timeline/TempoTimelineObj.cs | 6 +++--- Assets/Scripts/LevelEditor/Timeline/Timeline.cs | 6 +++--- .../LevelEditor/Timeline/TimelineEventObj.cs | 14 +++++++------- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/Common/FollowMouse.cs b/Assets/Scripts/Common/FollowMouse.cs index d61ba2224..691df201d 100644 --- a/Assets/Scripts/Common/FollowMouse.cs +++ b/Assets/Scripts/Common/FollowMouse.cs @@ -7,10 +7,17 @@ namespace RhythmHeavenMania.Common public class FollowMouse : MonoBehaviour { public Vector2 offset; + public Camera cam; + + private void Awake() + { + if (cam == null) + cam = Camera.main; + } private void Update() { - var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); + var pos = cam.ScreenToWorldPoint(Input.mousePosition); transform.position = new Vector3(pos.x - offset.x, pos.y - offset.y, 0); } } diff --git a/Assets/Scripts/Common/Parallax.cs b/Assets/Scripts/Common/Parallax.cs index 72d80b01b..f5a40f56a 100644 --- a/Assets/Scripts/Common/Parallax.cs +++ b/Assets/Scripts/Common/Parallax.cs @@ -14,11 +14,18 @@ namespace RhythmHeavenMania.Common private Vector3 lastCamPos; public float textureUnitSizeX; + public Camera cam; public bool sprite = true; + private void Awake() + { + if (cam == null) + cam = Camera.main; + } + private void Start() { - camTransform = Camera.main.transform; + camTransform = cam.transform; lastCamPos = camTransform.position; /*if (sprite) { diff --git a/Assets/Scripts/LevelEditor/Timeline/TempoTimeline.cs b/Assets/Scripts/LevelEditor/Timeline/TempoTimeline.cs index 5c9a76a1a..680736ffb 100644 --- a/Assets/Scripts/LevelEditor/Timeline/TempoTimeline.cs +++ b/Assets/Scripts/LevelEditor/Timeline/TempoTimeline.cs @@ -34,7 +34,7 @@ namespace RhythmHeavenMania.Editor.Track if (Timeline.instance.timelineState.tempoChange && !Conductor.instance.NotStopped()) { - if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Camera.main)) + if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Editor.instance.EditorCamera)) { if (Input.GetMouseButtonDown(0)) { @@ -45,7 +45,7 @@ namespace RhythmHeavenMania.Editor.Track } } - if (RectTransformUtility.RectangleContainsScreenPoint(StartingBPM, Input.mousePosition, Camera.main)) + if (RectTransformUtility.RectangleContainsScreenPoint(StartingBPM, Input.mousePosition, Editor.instance.EditorCamera)) { float increase = Input.mouseScrollDelta.y; if (Input.GetKey(KeyCode.LeftControl)) @@ -82,7 +82,7 @@ namespace RhythmHeavenMania.Editor.Track if (create == true) { - tempoChange.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x + 0.08f, tempoChange.transform.position.y); + tempoChange.transform.position = new Vector3(Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition).x + 0.08f, tempoChange.transform.position.y); tempoChange.transform.localPosition = new Vector3(Starpelly.Mathp.Round2Nearest(tempoChange.transform.localPosition.x, 0.25f), tempoChange.transform.localPosition.y); Beatmap.TempoChange tempoC = new Beatmap.TempoChange(); diff --git a/Assets/Scripts/LevelEditor/Timeline/TempoTimelineObj.cs b/Assets/Scripts/LevelEditor/Timeline/TempoTimelineObj.cs index ca0aeacb8..64fd35f73 100644 --- a/Assets/Scripts/LevelEditor/Timeline/TempoTimelineObj.cs +++ b/Assets/Scripts/LevelEditor/Timeline/TempoTimelineObj.cs @@ -35,7 +35,7 @@ namespace RhythmHeavenMania.Editor.Track { if (Timeline.instance.timelineState.tempoChange && !Conductor.instance.NotStopped()) { - if (RectTransformUtility.RectangleContainsScreenPoint(raycastRect, Input.mousePosition, Camera.main)) + if (RectTransformUtility.RectangleContainsScreenPoint(raycastRect, Input.mousePosition, Editor.instance.EditorCamera)) { float newTempo = Input.mouseScrollDelta.y; @@ -48,7 +48,7 @@ namespace RhythmHeavenMania.Editor.Track if (Input.GetMouseButtonDown(0)) { - Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); + Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); startPosX = mousePos.x - transform.position.x; moving = true; lastPosX = transform.localPosition.x; @@ -68,7 +68,7 @@ namespace RhythmHeavenMania.Editor.Track if (moving) { - Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); + Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); transform.position = new Vector3(mousePos.x - startPosX, transform.position.y, 0); transform.localPosition = new Vector3(Mathf.Clamp(Starpelly.Mathp.Round2Nearest(transform.localPosition.x, 0.25f), 0, Mathf.Infinity), transform.localPosition.y); diff --git a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs index db465f57c..e619a403b 100644 --- a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs +++ b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs @@ -409,12 +409,12 @@ namespace RhythmHeavenMania.Editor.Track public bool CheckIfMouseInTimeline() { - return (this.gameObject.activeSelf && RectTransformUtility.RectangleContainsScreenPoint(TimelineContent.transform.parent.gameObject.GetComponent(), Input.mousePosition, Camera.main)); + return (this.gameObject.activeSelf && RectTransformUtility.RectangleContainsScreenPoint(TimelineContent.transform.parent.gameObject.GetComponent(), Input.mousePosition, Editor.instance.EditorCamera)); } public bool MouseInRectTransform(RectTransform rectTransform) { - return (rectTransform.gameObject.activeSelf && RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Camera.main)); + return (rectTransform.gameObject.activeSelf && RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Editor.instance.EditorCamera)); } #endregion @@ -461,7 +461,7 @@ namespace RhythmHeavenMania.Editor.Track if (dragNDrop) { - var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); + var mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); g.transform.position = new Vector3(mousePos.x, mousePos.y, 0); Selections.instance.ClickSelect(eventObj); diff --git a/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs b/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs index 4dd54cbe6..3aa2911a2 100644 --- a/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs +++ b/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs @@ -64,7 +64,7 @@ namespace RhythmHeavenMania.Editor.Track moveTemp = new GameObject(); moveTemp.transform.SetParent(this.transform.parent); - bool visible = rectTransform.IsVisibleFrom(Camera.main); + bool visible = rectTransform.IsVisibleFrom(Editor.instance.EditorCamera); for (int i = 0; i < this.transform.childCount; i++) { if (i != 4) @@ -77,12 +77,12 @@ namespace RhythmHeavenMania.Editor.Track selected = Selections.instance.eventsSelected.Contains(this); entity = GameManager.instance.Beatmap.entities.Find(a => a.eventObj == this); - mouseHovering = RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Camera.main) && Timeline.instance.timelineState.selected; + mouseHovering = RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Editor.instance.EditorCamera) && Timeline.instance.timelineState.selected; #region Optimizations // problem with long objects but im lazy right now - bool visible = rectTransform.IsVisibleFrom(Camera.main); + bool visible = rectTransform.IsVisibleFrom(Editor.instance.EditorCamera); if (visible != lastVisible) { @@ -144,7 +144,7 @@ namespace RhythmHeavenMania.Editor.Track if (Timeline.instance.eventObjs.FindAll(c => c.moving).Count > 0 && selected) { - Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); + Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); // lastPos_ = transform.localPosition; @@ -168,7 +168,7 @@ namespace RhythmHeavenMania.Editor.Track Vector2 sizeDelta = rectTransform.sizeDelta; Vector2 mousePos; - RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Camera.main, out mousePos); + RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Editor.instance.EditorCamera, out mousePos); sizeDelta = new Vector2(-mousePos.x + 0.15f, sizeDelta.y); sizeDelta = new Vector2(Mathf.Clamp(sizeDelta.x, 0.25f, rectTransform.localPosition.x), sizeDelta.y); @@ -182,7 +182,7 @@ namespace RhythmHeavenMania.Editor.Track Vector2 sizeDelta = rectTransform.sizeDelta; Vector2 mousePos; - RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Camera.main, out mousePos); + RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Editor.instance.EditorCamera, out mousePos); sizeDelta = new Vector2(mousePos.x + 0.15f, sizeDelta.y); sizeDelta = new Vector2(Mathf.Clamp(sizeDelta.x, 0.25f, Mathf.Infinity), sizeDelta.y); @@ -237,7 +237,7 @@ namespace RhythmHeavenMania.Editor.Track for (int i = 0; i < Timeline.instance.eventObjs.Count; i++) { - Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); + Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); Timeline.instance.eventObjs[i].startPosX = mousePos.x - Timeline.instance.eventObjs[i].transform.position.x; Timeline.instance.eventObjs[i].startPosY = mousePos.y - Timeline.instance.eventObjs[i].transform.position.y; }