Skip to content

Instantly share code, notes, and snippets.

@fadookie
Created January 27, 2015 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadookie/6220ad6b9913246dfd4e to your computer and use it in GitHub Desktop.
Save fadookie/6220ad6b9913246dfd4e to your computer and use it in GitHub Desktop.
Spawn area component for Unity, spawns prefabs in a defined 2D area
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class SpawnArea : MonoBehaviour {
public GameObject spawnedPrefab;
BoxCollider2D spawnArea;
Vector2 maxSpawnPos;
float lastSpawnTimeS = -1;
public float spawnDelayS = 5;
// Use this for initialization
void Start () {
spawnArea = GetComponent<BoxCollider2D>();
spawnArea.enabled = false; //We don't need this to test for any collisions, just to show visual bounds info in the editor.
maxSpawnPos = new Vector2(spawnArea.size.x / 2, spawnArea.size.y / 2);
}
// Update is called once per frame
void Update () {
if (lastSpawnTimeS < 0) {
lastSpawnTimeS = Time.time;
print ("spawn timer fire");
GameObject spawned = Instantiate(spawnedPrefab, Vector3.zero, Quaternion.identity) as GameObject;
spawned.transform.parent = transform;
Vector3 pos = new Vector3(Random.Range(-maxSpawnPos.x, maxSpawnPos.x), Random.Range(-maxSpawnPos.y, maxSpawnPos.y), 0);
spawned.transform.localPosition = pos;
} else if (lastSpawnTimeS >= 0 && Time.time - lastSpawnTimeS > spawnDelayS) {
lastSpawnTimeS = -1;
}
}
}
@MikeGringauz
Copy link

Hi Marcel and thank you!
I was looking for multi resolution support in Unity and found this script which is a huge time saver!

One thing, I've noticed is that the script uses "fixed width fit policy" in Landscape mode i.e. if a scene doesn't fit to a screen then part of a scene's top and bottom sides are truncated. In Portrait mode the script uses "fixed height fit policy" i.e. if a scene doesn't fit to a screen then part of a scene's left and right sides are truncated.

Can the script be modified to add an "fit policy" option to choose as well as orientation? I.e. ability to choose "fixed height fit policy" in Landscape mode i.e. if a scene doesn't fit to a screen then part of a scene's left and right sides are truncated (instead of top and bottom as with "fixed width fit policy").
Or ability to choose "fixed width fit policy" in Portrait mode i.e. if a scene doesn't fit to a screen then part of a scene's top and bottom sides are truncated (instead of left and right as with "fixed height fit policy").

I hope I've made myself clear. :)

These option will make CameraFit script complete, imho. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment