Hey guys, I have this free asset I downloaded to take screen shots and I’m trying to learn from it since I couldn’t get my screenshot to print lol. Anyway I can’t tell what is making this only work when it’s in play mode? I’m trying to have my custom editor run shot() when I click a GUIbutton within the interface while in edit mode. It works just fine in play mode just not in edit mode. Here’s the screen shot script.
public void Shot()
{
StartCoroutine(ShotCoroutine(prefix));
}
IEnumerator ShotCoroutine(string prefix)
{
// if (!Application.isPlaying)
// print("You should be in play mode !");
previousClearFlag = Camera.main.clearFlags;
previousCullingMask = Camera.main.cullingMask;
int ex = 0;
foreach (Layer l in layers)
{
if(l.transparent)
{
PrepareTransparentCamera(Camera.main);
}
else
{
ResetCamera(Camera.main);
}
Camera.main.cullingMask = l.mask;
yield return new WaitForEndOfFrame();
string sname = prefix + l.name;
Texture2D tex = new Texture2D(screenWidth, screenHeight, l.transparent ? TextureFormat.ARGB32 : TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(50, 0, screenWidth, screenHeight), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
DestroyImmediate(tex);
File.WriteAllBytes(Application.dataPath + "/PotionIcons/" + sname + ".png", bytes);
print("Saving " + Application.dataPath + "/PotionIcons/" + sname + ".png");
yield return new WaitForSeconds(0.01f);
ex++;
}
ResetCamera(Camera.main);
}
private void ResetCamera(Camera c)
{
c.clearFlags = previousClearFlag;
c.cullingMask = previousCullingMask;
MonoBehaviour[] bs = c.gameObject.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour b in bs)
{
b.enabled = true;
}
}
private void PrepareTransparentCamera(Camera c)
{
c.clearFlags = CameraClearFlags.Color;
c.backgroundColor = new Color(0,0,0,0);
MonoBehaviour[] bs = c.gameObject.GetComponents<MonoBehaviour>();
foreach(MonoBehaviour b in bs)
{
b.enabled = false;
}
}
And this is the code I have to pull run the shot() method
if(GUILayout.Button("Screeny"))
{
myScript.Shot();
Debug.Log("I hope you created an Screeny muahahhah");
}
Not to drag this on, but I only need to take a screen shot off little object around 100x100 and 300x300. Is there a way to force the camera to have the same parameters (if thats the right term) ad my 2D object?
Thanks a bunch