|
1 | 1 | // adds EdgeCollider2D colliders to screen edges
|
2 | 2 | // only works with orthographic camera
|
3 | 3 |
|
| 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 | + |
4 | 8 | using UnityEngine;
|
5 |
| -using System.Collections; |
6 | 9 |
|
7 | 10 | namespace UnityLibrary
|
8 | 11 | {
|
9 |
| - public class ScreenEdgeColliders : MonoBehaviour |
10 |
| - { |
11 |
| - void Awake () |
| 12 | + public class ScreenEdgeColliders : MonoBehaviour |
12 | 13 | {
|
13 |
| - AddCollider(); |
14 |
| - } |
| 14 | + Camera cam; |
| 15 | + EdgeCollider2D edge; |
| 16 | + Vector2[] edgePoints; |
15 | 17 |
|
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; } |
19 | 57 |
|
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; } |
22 | 60 |
|
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); |
27 | 65 |
|
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>(); |
30 | 68 |
|
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 | + } |
33 | 72 | }
|
34 |
| - } |
35 | 73 | }
|
0 commit comments