Thanks for share DrKucho!
So, I already tried to solve using the Create method.
In true nothing changes using this in my case.
In some post someone said to do something like:
rt = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
if (!rt.isCreated(){
rt.Create();
}
Well… I have good news (for me at least
).
I solved this issue disabling the camera that use rendertexture every time that I need to do some of this actions:
- When changing any attribute of RenderTexture after isCreated().
- When changing Screen size or Resolution of the camera where was added a RenderTexture.
- When changing VSyncCount of QualitySettings.
- When changing Antialising of QualitySettings.
We don’t need to use the WaitForEndOfFrame or even Coroutine in most cases
The only case for is while changing the resolution to fullscreen, because it take some time, and even with WaitForEndOfFrame I didn’t have success, so was better to use a delay using WaitForSeconds(2);
In short, all that we need is to disable the camera before some action and then enable again after all changes was completed.
I created this method to be called from any case:
public void SwitchOnOffAllCameras(bool enabled) {
for (int i = 0; i < XMLHandler.Instance.layersNow; i++) {
camLayer[i].GetComponent<Camera>().enabled = enabled;
}
}
NOTE: In my case I use 1,2,3 or 4 cameras using RenderTexture, but you can simplify using only something like:
public void SwitchOnOffCameraWithRenderTexture(bool enabled) {
camera.enabled = enabled;
}
I like to use methods to be more easy to identify where I’m changing the attribute, as I need to call it from other script too.
So… I called that method before to change something that was causing the error.
After all is done I called it again: SwitchOnOffAllCameras(true);
Example 1 (quality settings):
SwitchOnOffAllCameras(false);
QualitySettings.antiAliasing = QualitySettings.antiAliasing ==0?2: QualitySettings.antiAliasing *2;
SwitchOnOffAllCameras(true);
Example 2 (fullscreen issue):
private IEnumerator SwitchFullscreen() {
CameraHandler.Instance.SwitchOnOffAllCameras(false);
if (Screen.fullScreen) {
Screen.SetResolution(940, 720, false);
} else {
Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
}
yield return new WaitForSeconds(2);
CameraHandler.Instance.SwitchOnOffAllCameras(true);
}
Example 3 (creating camera and RenderTexture):
In this case I didn’t use that method as I’m creating cameras and RenderTextures for the first time and would generate a null exception.
private void CreateCamerasForRenderTexture() {
Debug.LogAssertion("Before Create RT");
camLayer = new GameObject[XMLHandler.Instance.layersNow];
renderTextureLayer = new RenderTexture[XMLHandler.Instance.layersNow];
for (int i = 0; i < XMLHandler.Instance.layersNow; i++) {
camLayer[i] = new GameObject("CamLayerAB" + i);
camLayer[i].AddComponent<Camera>();
camLayer[i].GetComponent<Camera>().enabled = false;
camLayer[i].GetComponent<Camera>().orthographic = true;
camLayer[i].GetComponent<Camera>().orthographicSize = 0.6f * (XMLHandler.Instance.scale);
camLayer[i].transform.parent = cam.transform;
camLayer[i].transform.localPosition = Vector3.zero;
camLayer[i].transform.localRotation = Quaternion.identity;
camLayer[i].GetComponent<Camera>().backgroundColor = new Color(0, 0, 0, 0);
camLayer[i].GetComponent<Camera>().cullingMask = 1 << LayerMask.NameToLayer(XMLHandler.Instance.layerNameToApply[i]);
camLayer[i].GetComponent<Camera>().useOcclusionCulling = false;
renderTextureLayer[i] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
renderTextureLayer[i].name = "RenderTextureLayerAB" + i;
camLayer[i].GetComponent<Camera>().targetTexture = renderTextureLayer[i];
camLayer[i].GetComponent<Camera>().renderingPath = RenderingPath.Forward;
camLayer[i].GetComponent<Camera>().enabled = true;
}
Debug.LogAssertion("After Create RT");
}
To discover where (in my case) was occurring the error I create a LOT of Debug.LogAssertion() entries to see in the console of Standalone build.
NOTE: I listed 4 cases, but probably have more situations where the issue could happens.
I hope that it can help someone.