Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

MikeGringauz commented Apr 24, 2016

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