浏览代码

Updated Console package (#5)

* Updated Console Package

* Updated API for Console Commands
/main
GitHub Enterprise 5 年前
当前提交
7efb145b
共有 19 个文件被更改,包括 237 次插入849 次删除
  1. 8
      Assets/Scripts/ConsoleCommnands/LoadLevelConsoleCommand.cs
  2. 12
      Assets/Scripts/Debug/POVConsoleCommand.cs
  3. 14
      Assets/Scripts/Debug/SceneCommand.cs
  4. 6
      Assets/Scripts/Debug/SendMessageCommand.cs
  5. 3
      Assets/Scripts/Shake/ShakeManager.cs
  6. 2
      LocalPackages/net.peeweek.console/Console/CommandLibrary/ExitCommand.cs
  7. 2
      LocalPackages/net.peeweek.console/Console/CommandLibrary/PhysicsCommand.cs
  8. 2
      LocalPackages/net.peeweek.console/Console/CommandLibrary/ScreenCommand.cs
  9. 2
      LocalPackages/net.peeweek.console/Console/CommandLibrary/ScreenshotCommand.cs
  10. 2
      LocalPackages/net.peeweek.console/Console/CommandLibrary/TimeCommand.cs
  11. 2
      LocalPackages/net.peeweek.console/Console/ConsoleUtility.cs
  12. 3
      LocalPackages/net.peeweek.console/Console/Font/Inconsolata.otf.meta
  13. 154
      LocalPackages/net.peeweek.console/Console/Script/Console.cs
  14. 14
      LocalPackages/net.peeweek.console/README.md
  15. 814
      LocalPackages/net.peeweek.console/Resources/Console.prefab
  16. 14
      LocalPackages/net.peeweek.console/package.json
  17. 2
      LocalPackages/net.peeweek.console/package.json.meta
  18. 23
      LocalPackages/net.peeweek.console/CHANGELOG.md
  19. 7
      LocalPackages/net.peeweek.console/CHANGELOG.md.meta

8
Assets/Scripts/ConsoleCommnands/LoadLevelConsoleCommand.cs


using Console;
using ConsoleUtility;
using GameplayIngredients;
using System.Collections.Generic;
using UnityEngine;

public string help => "load <level-Id>";
public IEnumerable<Console.Console.Alias> aliases
public IEnumerable<Console.Alias> aliases
yield return new Console.Console.Alias() { AliasString = "mainmenu", Command = "load -1" } ;
yield return new Console.Alias() { AliasString = "mainmenu", Command = "load -1" } ;
}
}

{
var manager = GameplayIngredients.Manager.Get<GameManager>();
idx = Mathf.Clamp(idx, -1, manager.MainGameLevels.Length-1);
Console.Console.Log(name, $"Loading Game level #{idx} ...");
Console.Log(name, $"Loading Game level #{idx} ...");
manager.SwitchLevel(idx, true, null);
}
}

12
Assets/Scripts/Debug/POVConsoleCommand.cs


using UnityEngine;
using System.Collections.Generic;
using GameplayIngredients;
using Console;
using ConsoleUtility;
[AutoRegisterConsoleCommand]
public class POVConsoleCommand : IConsoleCommand

public string help => @"pov [index]";
public IEnumerable<Console.Console.Alias> aliases => null;
public IEnumerable<Console.Alias> aliases => null;
public void Execute(string[] args)
{

}
else
{
Console.Console.Log("POV", $"Could not set POV #{index} : found {allPOVs.Length} ScenePOVRoot objects", LogType.Error);
Console.Log("POV", $"Could not set POV #{index} : found {allPOVs.Length} ScenePOVRoot objects", LogType.Error);
Console.Console.Log("POV", $"Could not find any ScenePOVRoot objects", LogType.Error);
Console.Log("POV", $"Could not find any ScenePOVRoot objects", LogType.Error);
Console.Console.Log("POV", $"Invalid argument: {args[0]}", LogType.Error);
Console.Log("POV", $"Invalid argument: {args[0]}", LogType.Error);
Console.Console.Log("POV", $"Disabled POV", LogType.Log);
Console.Log("POV", $"Disabled POV", LogType.Log);
}
}
}

14
Assets/Scripts/Debug/SceneCommand.cs


using Console;
using ConsoleUtility;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public string help => @"scene [command] {scene name}
commands : load / add / unload";
public IEnumerable<Console.Console.Alias> aliases => null;
public IEnumerable<Console.Alias> aliases => null;
Console.Console.Log("SCENE", "Enumerating Loaded Scenes....");
Console.Log("SCENE", "Enumerating Loaded Scenes....");
Console.Console.Log("SCENE", $"#{scene.buildIndex} - {scene.name} : {(scene.isLoaded ? "Loaded" : "Unloaded")} {(active ? "ACTIVE" : "")}");
Console.Log("SCENE", $"#{scene.buildIndex} - {scene.name} : {(scene.isLoaded ? "Loaded" : "Unloaded")} {(active ? "ACTIVE" : "")}");
Console.Console.Log("SCENE", $"Loading Single Scene {args[1]}");
Console.Log("SCENE", $"Loading Single Scene {args[1]}");
Console.Console.Log("SCENE", $"Loading Additive Scene {args[1]}");
Console.Log("SCENE", $"Loading Additive Scene {args[1]}");
Console.Console.Log("SCENE", $"Unloading Scene {args[1]}");
Console.Log("SCENE", $"Unloading Scene {args[1]}");
}
}
}

6
Assets/Scripts/Debug/SendMessageCommand.cs


using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using Console;
using ConsoleUtility;
using GameplayIngredients;
[AutoRegisterConsoleCommand]

public string help => "usage: send <message>";
public IEnumerable<Console.Console.Alias> aliases => null;
public IEnumerable<Console.Alias> aliases => null;
public void Execute(string[] args)
{

}
else
{
Console.Console.Log(name, help, LogType.Warning);
Console.Log(name, help, LogType.Warning);
}
}
}

3
Assets/Scripts/Shake/ShakeManager.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ConsoleUtility;
[ManagerDefaultPrefab("ShakeManager")]
public class ShakeManager : Manager

void DebugLog(string message)
{
if(LogShakes)
Console.Console.Log("ShakeManager", message, LogType.Log);
Console.Log("ShakeManager", message, LogType.Log);
}
public void Shake(Settings shake, bool force = false)

2
LocalPackages/net.peeweek.console/Console/CommandLibrary/ExitCommand.cs


using System.Collections.Generic;
using UnityEngine;
namespace Console
namespace ConsoleUtility
{
[AutoRegisterConsoleCommand]
public class ExitCommand : IConsoleCommand

2
LocalPackages/net.peeweek.console/Console/CommandLibrary/PhysicsCommand.cs


using System.Collections.Generic;
using UnityEngine;
namespace Console
namespace ConsoleUtility
{
[AutoRegisterConsoleCommand]
public class PhysicsCommand : IConsoleCommand

2
LocalPackages/net.peeweek.console/Console/CommandLibrary/ScreenCommand.cs


using System.Collections.Generic;
using UnityEngine;
namespace Console
namespace ConsoleUtility
{
[AutoRegisterConsoleCommand]
public class ScreenCommand : IConsoleCommand

2
LocalPackages/net.peeweek.console/Console/CommandLibrary/ScreenshotCommand.cs


using UnityEngine;
using UnityEngine.SceneManagement;
namespace Console
namespace ConsoleUtility
{
[AutoRegisterConsoleCommand]
public class ScreenshotCommand : IConsoleCommand

2
LocalPackages/net.peeweek.console/Console/CommandLibrary/TimeCommand.cs


using System.Collections.Generic;
using UnityEngine;
namespace Console
namespace ConsoleUtility
{
[AutoRegisterConsoleCommand]
public class TimeCommand : IConsoleCommand

2
LocalPackages/net.peeweek.console/Console/ConsoleUtility.cs


using System.Collections.Generic;
using UnityEngine;
namespace Console
namespace ConsoleUtility
{
static class ConsoleUtility
{

3
LocalPackages/net.peeweek.console/Console/Font/Inconsolata.otf.meta


- Inconsolata
fallbackFontReferences: []
customCharacters:
fontRenderingMode: 0
fontRenderingMode: 1
shouldRoundAdvanceValue: 1
userData:
assetBundleName:
assetBundleVariant:

154
LocalPackages/net.peeweek.console/Console/Script/Console.cs


using System.Reflection;
using System.Linq;
namespace Console
namespace ConsoleUtility
static readonly int MAX_CHARS = 8192;
[Header("Keys")]
public KeyCode ToggleKey = KeyCode.F12;
public KeyCode PreviousCommandKey = KeyCode.UpArrow;

public Canvas Canvas;
public InputField InputField;
public Text LogText;
public RectTransform LogContents;
public ScrollRect ScrollRect;
public Text ScrollInfo;
public GameObject AutoPanelRoot;
public Text AutoPanelText;

s_Console = this;
Application.logMessageReceived += HandleUnityLog;
LogText.font.RequestCharactersInTexture("qwertyuiopasdfghjklzxcvbnmQWERYTUIOPASDFGHJKLZXCVBNM1234567890~`!@#$%^&*()_+{}[]:;\"'/.,?><");
Log("Console initialized successfully");

}
else if (Input.GetKeyDown(ScrollUpKey))
{
if (ScrollRect != null)
ScrollRect.verticalNormalizedPosition += ScrollSpeed / s_ConsoleData.lines.Count;
ScrollUp();
if (ScrollRect != null)
ScrollRect.verticalNormalizedPosition -= ScrollSpeed/ s_ConsoleData.lines.Count;
ScrollDown();
}
}

}
}
public void ValidateCommand()
public static void ExecuteCommand(string command)
if (InputField.text == "") return;
string command = InputField.text;
InputField.text = "";
else if(s_ConsoleData.aliases.ContainsKey(words[0].ToUpper()))
else if (s_ConsoleData.aliases.ContainsKey(words[0].ToUpper()))
{
string alias = words[0];
string aliascommand = command.Replace(alias, s_ConsoleData.aliases[alias.ToUpper()]);

InputField.text = "";
InputField.text = "";
s_ConsoleData.commandHistory.Insert(0, command);
// Ensure no duplicates in history
if (s_ConsoleData.commandHistory.Count == 0 || command != s_ConsoleData.commandHistory[0])
s_ConsoleData.commandHistory.Insert(0, command);
}
public void ValidateCommand()
{
if (InputField.text == "")
return;
string command = InputField.text;
ExecuteCommand(command);
InputField.text = "";
InputField.Select();
InputField.MoveTextStart(false);
InputField.ActivateInputField();

}
}
private int m_Scroll=-1;
private int GetScrollPageSize()
{
float lineSize = LogText.lineSpacing + LogText.font.fontSize;
int count = Mathf.FloorToInt(LogText.rectTransform.rect.height / lineSize);
return count;
}
private void ScrollUp()
{
int pageSize = GetScrollPageSize();
if (s_ConsoleData.lines.Count < pageSize)
return;
if(m_Scroll == -1)
{
m_Scroll = s_ConsoleData.lines.Count - pageSize;
}
m_Scroll = Math.Max(0, m_Scroll - pageSize);
UpdateLog();
}
private void ScrollDown()
{
int pageSize = GetScrollPageSize();
if (s_ConsoleData.lines.Count < pageSize)
return;
if (m_Scroll == -1)
return;
m_Scroll += pageSize;
if (m_Scroll >= (s_ConsoleData.lines.Count - pageSize))
m_Scroll = -1; // Snap Again
UpdateLog();
}
private string GetScrolledText()
{
int pageSize = GetScrollPageSize();
int count = s_ConsoleData.lines.Count;
CharacterInfo info;
LogText.font.GetCharacterInfo('X', out info);
int maxCharsInLine = Mathf.FloorToInt(LogText.rectTransform.rect.width / info.advance);
int init = m_Scroll == -1 ? count - pageSize : m_Scroll;
StringBuilder sb = new StringBuilder();
for (int i = init; i < (init + pageSize); i++)
{
if (i < 0)
continue;
string line = s_ConsoleData.lines[i];
if (line.Length > maxCharsInLine)
line = line.Substring(0, maxCharsInLine - 4) + " ...";
sb.AppendLine(line);
}
return sb.ToString();
}
// Ensure m_Scroll is Consistent when clearing;
if (m_Scroll > s_ConsoleData.lines.Count)
m_Scroll = -1;
// Update Log Text
//string text = s_ConsoleData.lines.Aggregate((a, b) => a + "\n" + b);
LogText.text = GetScrolledText();
LogText.GraphicUpdateComplete();
}
// Update Scroll Info Text
if (ScrollInfo != null)
{
int pageSize = GetScrollPageSize();
int count = s_ConsoleData.lines.Count;
for(int i = s_ConsoleData.lines.Count-1; i >= 0; i--)
if (m_Scroll == -1)
string line = s_ConsoleData.lines[i];
if(text != string.Empty)
line += "\n";
if(line.Length + text.Length > MAX_CHARS)
break;
else
text = line + text;
ScrollInfo.text = $"*[{Mathf.FloorToInt(count / pageSize)}/{Mathf.CeilToInt(count / pageSize)}]";
LogText.text = text;
LogText.GraphicUpdateComplete();
if (LogContents != null)
else
int count = LogText.text.Count(o => o == '\n');
float height = Math.Max(count * (LogText.fontSize + 2) * LogText.lineSpacing + 32, 64);
LogContents.sizeDelta = new Vector2(LogContents.sizeDelta.x, height);
ScrollInfo.text = $"[{Mathf.FloorToInt(m_Scroll / pageSize)}/{Mathf.CeilToInt(count / pageSize)}]";
if (ScrollRect != null)
ScrollRect.verticalNormalizedPosition = 0.0f;
ScrollInfo.GraphicUpdateComplete();
}
}

14
LocalPackages/net.peeweek.console/README.md


## Install
Either Clone the repository and reference the package.json in the Package Manager UI, or add directly this line to the `Packages/manifest.json` under `dependencies` :
Either Clone the repository and reference the `package.json` in the Package Manager UI, or add directly this line to the `Packages/manifest.json` under `dependencies` :
`"net.peeweek.console": "https://github.com/peeweek/net.peeweek.console.git#1.0.0-preview",`
`"net.peeweek.console": "https://github.com/peeweek/net.peeweek.console.git#1.1.0",`
## Usage
## How to use
* ~~Drop the Console/Console.prefab into your scene~~
* Voilà! You can access the console by pressing F12 at runtime
* Press the Backslash `\` key at runtime to toggle the Console (Can be customized if you use the package as local package)
* Use the input field to input commands and execute button or return key to execute.
### Navigation (Defaults)

* `clear` clears the console output.
### API Summary
* `using ConsoleUtility;`
* `Console.Log("Module", "Message", LogType.Warning);`for a detailed log and coloration.
* `Console.Log("Module", "Message");`shortcut with `LogType.Log`.
* `Console.Log("Message");`Simplest one, without module.

using System.Collections.Generic;
using UnityEngine;
namespace Console
namespace ConsoleUtility
{
[AutoRegisterConsoleCommand]
public class MyConsoleCommand : IConsoleCommand

814
LocalPackages/net.peeweek.console/Resources/Console.prefab
文件差异内容过多而无法显示
查看文件

14
LocalPackages/net.peeweek.console/package.json


{
"name": "net.peeweek.console",
"displayName": "Console",
"version": "1.0.1-preview",
"unity": "2018.3",
"description": "Console for your games"
}
"name": "net.peeweek.console",
"displayName": "Console",
"version": "1.1.0",
"unity": "2018.4",
"description": "Console GUI for your games. Enable display of logs at runtime and in players, executing console commands and adding your own console commands using a simple C# API.",
"type": "tool",
"hideInEditor": false
}

2
LocalPackages/net.peeweek.console/package.json.meta


fileFormatVersion: 2
guid: 2f67d47412141ae4ab28053a65f17e0f
TextScriptImporter:
PackageManifestImporter:
externalObjects: {}
userData:
assetBundleName:

23
LocalPackages/net.peeweek.console/CHANGELOG.md


# Console Changelog
## [2020-XX-XX] - 1.2.0
* Added a `Console.ExecuteCommand(string command)` to execute a command from script.
## [2019-08-24] - 1.1.0
* No more preview label for the package
* Got rid of UGUI ScrollBar, only displays one TextField
* Scrolling uses PageUp/PageDown and custom scrolling system to reduce text rendering overhead
* In order to avoid using `Console.Console` even when `using Console;`due to ambiguous calls, `Console` namespace has been renamed to `ConsoleUtility`
* Ensure command repeat history do not store contiguous duplicates.
## [2019-04-10] - 1.0.1-preview
* Make Console Log Stacktraces on Error/Exception
* Disabled EventSystem GameObject (Can be enabled if used locally)
* Fixed Time Digits
## [2019-01-15] - 1.0.0-preview
* Initial Release

7
LocalPackages/net.peeweek.console/CHANGELOG.md.meta


fileFormatVersion: 2
guid: ba425ded7c9ea3b47b479611efddc286
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存