I am (in my spare time) developing urban survival game (no zombies, but many ruined cities). One of things I want to add is ability for the user to put up CCTV cameras.
Each “camera kit” as I call it comes in two parts: actual camera and a screen where what camera sees is getting transferred to. So far so good. Unfortunately, I want player to be able to put as many cameras as he or she pleases (as far as computer resources allow).
Naturally, my first hunch was to go for render textures, but I’ve quickly found out that you have to make “pool” of those in editor and if two cameras try to write to same RT (say, cameras instantiated from a prefab), bad things happen.
Had anyone in the past developed similar system and if so, could you share some tips on how to make it work?
You can create render textures at runtime through code by doing something like this:
RenderTexture rendTex = new RenderTexture(width, height, 24);
rendTex.isPowerOfTwo = false;
GameObject camera = (GameObject)Instantiate(cameraPrefab, position, rotation);
camera.targetTexture = rendTex;
GameObject cctv = (GameObject)Instantiate(cctvPrefab, position, rotation);
Material rendMat = new Material(baseMaterial);
rendMat.SetTexture("_MainTex", rendTex );
cctv.GetComponent<MeshRenderer>().material = rendMat;
beside , create your texture
in your start function keep it alive along the game play use when it s necessary
but do not create and destroy on the fly if you dont want to get into memory issues or laggy
search for “object pooling” textures are not the kind of object you whant to create and destroy 30 time a second