Skip to content

Commit

Permalink
Fixes for Screenshots
Browse files Browse the repository at this point in the history
Needed a bit of refactor
  • Loading branch information
peeweek committed Apr 24, 2018
1 parent 38b749c commit 9d91ece
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Assets/Console/CommandLibrary/ScreenshotCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public void Execute(string[] args)
string datetime = string.Format("{0}{1}{2}-{3}{4}{5}",now.Year, now.Month, now.Day, now.Hour,now.Minute,now.Second);
string filename = string.Format("Unity-{0}-{1}-{2}.png", Application.productName, SceneManager.GetActiveScene().name, datetime);
Console.Log(GetName(), string.Format("Taking Screenshot at {0}x resolution : {1}",size, filename));
ScreenCapture.CaptureScreenshot(filename,1);
Console.CaptureScreenshot(filename, size);
}


public string GetHelp()
{
return @"usage: screenshot [supersize 1~5]";
Expand Down
96 changes: 64 additions & 32 deletions Assets/Console/Script/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,32 @@ public class Console : MonoBehaviour
[Range(1.0f, 30.0f)]
public float ScrollSpeed = 5.0f;

private static ConsoleData s_ConsoleData = new ConsoleData();
private static ConsoleData s_ConsoleData;
private static Console s_Console;

private bool bVisible = false;
private int history = -1;

void OnEnable()
{
if(s_ConsoleData == null)
{
s_ConsoleData = new ConsoleData();
s_ConsoleData.AutoRegisterConsoleCommands();
}
s_ConsoleData.OnLogUpdated = UpdateLog;
s_Console = this;

Application.logMessageReceived += HandleUnityLog;

// Use reflection to add automatically console commands
var autoCommandTypes = new List<Type>();
Log("Console initialized successfully");
}

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in assembly.GetTypes())
{
if (type.GetInterfaces().Contains(typeof(IConsoleCommand)) && type.GetCustomAttributes(typeof(AutoRegisterConsoleCommandAttribute), true).Length > 0)
{
autoCommandTypes.Add(type);
}
}
}
foreach(var type in autoCommandTypes)
{
AddCommand(Activator.CreateInstance(type) as IConsoleCommand);
}

Log("Console initialized successfully");
void OnDisable()
{
s_ConsoleData.OnLogUpdated = null;
s_Console = null;
Application.logMessageReceived -= HandleUnityLog;
}

public void UpdateAutoPanel()
Expand Down Expand Up @@ -104,16 +100,6 @@ public void UpdateAutoPanel()

}

void OnDisable()
{
Application.logMessageReceived -= HandleUnityLog;
}

private void Start()
{

}

void Update()
{
if (Input.GetKeyDown(ToggleKey))
Expand Down Expand Up @@ -161,9 +147,34 @@ void Update()
}
}

void ToggleVisibility()
public static void SetVisible(bool visible)
{
s_Console.SetVisibility(visible);
}

public static void CaptureScreenshot(string filename, int size)
{
s_Console.StartCoroutine(s_Console.Screenshot(filename, size));
}

public IEnumerator Screenshot(string filename, int size)
{
Console.SetVisible(false);
yield return new WaitForEndOfFrame();
ScreenCapture.CaptureScreenshot(filename, 1);
Console.SetVisible(true);
}

public void ToggleVisibility()
{
SetVisibility(!bVisible);
}

public void SetVisibility(bool visible)
{
bVisible = !bVisible;
if (bVisible == visible)
return;
bVisible = visible;
Canvas.gameObject.SetActive(bVisible);
if (bVisible)
{
Expand All @@ -172,7 +183,6 @@ void ToggleVisibility()
InputField.ActivateInputField();
UpdateLog();
}

}

public void ValidateCommand()
Expand Down Expand Up @@ -339,6 +349,28 @@ public ConsoleData()
aliases = new Dictionary<string,string>();
commandHistory = new List<string>();
}

public void AutoRegisterConsoleCommands()
{
// Use reflection to add automatically console commands
var autoCommandTypes = new List<Type>();

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in assembly.GetTypes())
{
if (type.GetInterfaces().Contains(typeof(IConsoleCommand)) && type.GetCustomAttributes(typeof(AutoRegisterConsoleCommandAttribute), true).Length > 0)
{
autoCommandTypes.Add(type);
}
}
}
foreach (var type in autoCommandTypes)
{
AddCommand(Activator.CreateInstance(type) as IConsoleCommand);
}

}
}

[AutoRegisterConsoleCommand]
Expand Down

0 comments on commit 9d91ece

Please sign in to comment.