I’m trying to rewrite Eric5h5’s excellent CrossFade script here:
…(which he wrote in JS) in C#. I’m a beginner to JS, so I’m having some trouble replicating one part of the script. There’s a lot of code here, but if anyone can spare the time to look through it and show me what I’m doing wrong, It’d be a huge help. I’ve been stuck on this one thing all weekend. Here goes:
Here’s Eric’s original script, in JS:
// This only works properly in the editor if the game window aspect is set to none
// so there are no black bars.
var cam1 : GameObject;
var cam2 : GameObject;
var fadeTime : float = 2;
private var inProgress = false;
private var tex : Texture2D;
function Start() {
// Set up GUITexture size
guiTexture.pixelInset = Rect(0, 0, Screen.width, Screen.height);
tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
}
function Update() {
if (Input.GetKeyDown("space")) {ScreenCrossFade(fadeTime);}
}
function ScreenCrossFade(fadeTime : float) {
if (inProgress) {return;}
inProgress = true;
// Get texture from screen
tex.ReadPixels(Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
tex.Apply();
guiTexture.texture = tex;
// Switch cameras
yield; // So the first Time.deltaTime in the loop below isn't huge because of the tex.Apply() hit
cam1.active = !cam1.active;
cam2.active = !cam2.active;
// Do the cross-fade
guiTexture.enabled = true;
for (i = 0.51; i > 0.0; i -= Time.deltaTime * 1/(fadeTime*2)) {
guiTexture.color.a = i;
yield;
}
// Clean up
guiTexture.enabled = false;
inProgress = false;
}
And here’s my attempt in C#, which is almost working, except for the parts I’ve put in red and bold:
using UnityEngine;
using System.Collections;
public class ScreenCrossFadeRedo : MonoBehaviour
{
//////// Fields /////////
// Cameras //
public GameObject camera1;
public GameObject camera2;
// Transitions //
public float fadeTime = 2f;
bool inProgress = false;
Texture2D tex;
float i;
Color transitionColor;
//////// Methods /////////
void Awake()
{
Setup();
}
void Update ()
{
if(Input.anyKeyDown)
{
Transition(fadeTime);
}
}
//////// Used Methods /////////
void Setup()
{
// Setup GUITexture Size //
guiTexture.pixelInset = new Rect(0, 0, Screen.width, Screen.height);
tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
}
void Transition(float fade)
{
//Set inProgress to true
inProgress = true;
//Get Texture from screen
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
tex.Apply();
guiTexture.texture = tex;
//Switch Cameras
[B][COLOR="red"]// In his script here he had a "yield"[/COLOR][/B]
camera1.active = !camera1.active;
camera2.active = !camera2.active;
//do the cross fade
[COLOR="red"]guiTexture.enabled = true;
for (i = 0.51f; i > 0.0f; i -= Time.deltaTime * 1f/(fade*2f))
{
transitionColor.a = i;
guiTexture.color = transitionColor;[/COLOR]
[COLOR="red"][B]//Also here was a yield[/B][/COLOR]
}
//cleanup
guiTexture.enabled = false;
inProgress = false;
}
}
I think I need to make a coroutine in the C# version, but I can’t decipher the JS version well enough to figure out what it would be. Any help…oh man…would be very appreciated!
Thanks,
Orion