Question by
KBEK · Jul 03, 2016 at 06:01 PM ·
2d-platformergameobjectsperformance optimization
Bad practice to create a GameObject for every BoxCollider2D?
I am creating a 2D Platformer and need more complex BoxCollider2Ds to be able to handle the collisions (multiple tags, addable & removable properties, etc.).
My current solution:
Create a prefab named "CustomBC2D" that has a BoxCollider2D, a Transform, and a C# script.
Replace BoxCollider2Ds with this new prefab.
My worry is the performance impact behind creating so many small GameObjects in a Scene. I think my game is probably small scale enough for it to not be an issue, but you can never be too paranoid.
Here is the CustomBC2D code in case it helps:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CustomBC2D : MonoBehaviour {
[HideInInspector] public string pName;
[HideInInspector] public string pTag;
[SerializeField] private List<string> effects;
private BoxCollider2D bc;
// Use this for initialization
void Start () {
effects = new List<string>();
}
public void Initialize(string t, Vector2 bcsize, Vector2 bcoffset, GameObject parent) {
transform.parent = parent.transform;
tag = t;
pName = parent.name;
pTag = parent.tag;
// Initialize BoxCollider2D
transform.position = parent.transform.position;
bc = gameObject.AddComponent<BoxCollider2D>();
bc.size = bcsize;
bc.offset = bcoffset;
}
public BoxCollider2D getBC2D() {
return bc;
}
public void addGlueEffect() {
if (effects.IndexOf("Glue") < 0) {
effects.Add("Glue");
}
}
public void removeGlueEffect() {
effects.Remove("Glue");
}
public bool hasGlueEffect()
{
return effects.Contains("Glue");
}
// Update is called once per frame
void Update () {
}
}
Comment