Skip to content

Commit a1c9b17

Browse files
committed
Made ScreenEdgeColliders more efficient. AddCollider() is more efficient than StandaloneAddCollider() by using cached variables set upon Awake() but StandaloneAddCollider() is still theoretically a little bit better. When used in Update() on an i5 3570k, AddCollider() averages 0.02 Time ms and 0.03 Time ms for StandaloneAddCollider() according to Unity Profiler
1 parent 14e9a04 commit a1c9b17

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed
Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,73 @@
11
// adds EdgeCollider2D colliders to screen edges
22
// only works with orthographic camera
33

4+
//Includes two different ways of implementation into your project
5+
//One is a method that uses cached fields on Awake() that requires this entire class but is more slightly more efficient (should use this if you plan to use the method in Update())
6+
//The other is a standalone method that doesn't need the rest of the class and can be copy-pasted directly into any project but is slightly less efficient
7+
48
using UnityEngine;
5-
using System.Collections;
69

710
namespace UnityLibrary
811
{
9-
public class ScreenEdgeColliders : MonoBehaviour
10-
{
11-
void Awake ()
12+
public class ScreenEdgeColliders : MonoBehaviour
1213
{
13-
AddCollider();
14-
}
14+
Camera cam;
15+
EdgeCollider2D edge;
16+
Vector2[] edgePoints;
1517

16-
void AddCollider ()
17-
{
18-
if (Camera.main==null) {Debug.LogError("Camera.main not found, failed to create edge colliders"); return;}
18+
void Awake()
19+
{
20+
if (Camera.main == null) Debug.LogError("Camera.main not found, failed to create edge colliders");
21+
else cam = Camera.main;
22+
23+
if (!cam.orthographic) Debug.LogError("Camera.main is not Orthographic, failed to create edge colliders");
24+
25+
// add or use existing EdgeCollider2D
26+
edge = GetComponent<EdgeCollider2D>() == null ? gameObject.AddComponent<EdgeCollider2D>() : GetComponent<EdgeCollider2D>();
27+
28+
edgePoints = new Vector2[5];
29+
30+
AddCollider();
31+
}
32+
33+
//Use this if you're okay with using the global fields and code in Awake() (more efficient)
34+
void AddCollider()
35+
{
36+
//Vector2's for the corners of the screen
37+
Vector2 bottomLeft = cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane));
38+
Vector2 topRight = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane));
39+
Vector2 topLeft = new Vector2(bottomLeft.x, topRight.y);
40+
Vector2 bottomRight = new Vector2(topRight.x, bottomLeft.y);
41+
42+
//Update Vector2 array for edge collider
43+
edgePoints[0] = bottomLeft;
44+
edgePoints[1] = topLeft;
45+
edgePoints[2] = topRight;
46+
edgePoints[3] = bottomRight;
47+
edgePoints[4] = bottomLeft;
48+
49+
edge.points = edgePoints;
50+
}
51+
52+
//Use this if you want a single function to handle everything (less efficient)
53+
//You can just ignore/delete the rest of this class if thats the case
54+
void StandaloneAddCollider()
55+
{
56+
if (Camera.main == null) { Debug.LogError("Camera.main not found, failed to create edge colliders"); return; }
1957

20-
var cam = Camera.main;
21-
if (!cam.orthographic) {Debug.LogError("Camera.main is not Orthographic, failed to create edge colliders"); return;}
58+
var cam = Camera.main;
59+
if (!cam.orthographic) { Debug.LogError("Camera.main is not Orthographic, failed to create edge colliders"); return; }
2260

23-
var bottomLeft = (Vector2)cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane));
24-
var topLeft = (Vector2)cam.ScreenToWorldPoint(new Vector3(0, cam.pixelHeight, cam.nearClipPlane));
25-
var topRight = (Vector2)cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane));
26-
var bottomRight = (Vector2)cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, 0, cam.nearClipPlane));
61+
Vector2 bottomLeft = cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane));
62+
Vector2 topRight = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane));
63+
Vector2 topLeft = new Vector2(bottomLeft.x, topRight.y);
64+
Vector2 bottomRight = new Vector2(topRight.x, bottomLeft.y);
2765

28-
// add or use existing EdgeCollider2D
29-
var edge = GetComponent<EdgeCollider2D>()==null?gameObject.AddComponent<EdgeCollider2D>():GetComponent<EdgeCollider2D>();
66+
// add or use existing EdgeCollider2D
67+
var edge = GetComponent<EdgeCollider2D>() == null ? gameObject.AddComponent<EdgeCollider2D>() : GetComponent<EdgeCollider2D>();
3068

31-
var edgePoints = new [] {bottomLeft,topLeft,topRight,bottomRight, bottomLeft};
32-
edge.points = edgePoints;
69+
var edgePoints = new[] { bottomLeft, topLeft, topRight, bottomRight, bottomLeft };
70+
edge.points = edgePoints;
71+
}
3372
}
34-
}
3573
}

0 commit comments

Comments
 (0)