C# 最佳重载方法匹配在Unity 5中有一些无效参数
我试图在Unity中为我的游戏创建一个命令行,当添加系统信息命令(如内存)时,我遇到了这个问题。我希望社区能帮助我。提前谢谢。错误发生在第216、223和225行C# 最佳重载方法匹配在Unity 5中有一些无效参数,c#,unity3d,C#,Unity3d,我试图在Unity中为我的游戏创建一个命令行,当添加系统信息命令(如内存)时,我遇到了这个问题。我希望社区能帮助我。提前谢谢。错误发生在第216、223和225行 using UnityEngine; using System; using System.Collections.Generic; using System.Text; public delegate void CommandHandler(string[] args); pub
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
public delegate void CommandHandler(string[] args);
public class ConsoleController {
#region Event declarations
// Used to communicate with ConsoleView
public delegate void LogChangedHandler(string[] log);
public event LogChangedHandler logChanged;
public delegate void VisibilityChangedHandler(bool visible);
public event VisibilityChangedHandler visibilityChanged;
#endregion
/// <summary>
/// Object to hold information about each command
/// </summary>
class CommandRegistration {
public string command { get; private set; }
public CommandHandler handler { get; private set; }
public string help { get; private set; }
public CommandRegistration(string command, CommandHandler handler, string help) {
this.command = command;
this.handler = handler;
this.help = help;
}
}
/// <summary>
/// How many log lines should be retained?
/// Note that strings submitted to appendLogLine with embedded newlines will be counted as a single line.
/// </summary>
const int scrollbackSize = 20;
Queue<string> scrollback = new Queue<string>(scrollbackSize);
List<string> commandHistory = new List<string>();
Dictionary<string, CommandRegistration> commands = new Dictionary<string, CommandRegistration>();
public string[] log { get; private set; } //Copy of scrollback as an array for easier use by ConsoleView
const string repeatCmdName = "!!"; //Name of the repeat command, constant since it needs to skip these if they are in the command history
public ConsoleController() {
//When adding commands, you must add a call below to registerCommand() with its name, implementation method, and help text.
registerCommand("babble", babble, "Example command that demonstrates how to parse arguments. babble [word] [# of times to repeat]");
registerCommand("echo", echo, "echoes arguments back as array (for testing argument parser)");
registerCommand("help", help, "Print this help.");
registerCommand("hide", hide, "Hide the console.");
registerCommand(repeatCmdName, repeatCommand, "Repeat last command.");
registerCommand("reload", reload, "Reload game.");
registerCommand("resetprefs", resetPrefs, "Reset & saves PlayerPrefs.");
registerCommand("ver", ver, "Displays the current game version.");
registerCommand("buildver", buildver, "Displays the current build.");
registerCommand("sys", sys, "Displays basic system information.");
registerCommand("devinfo", devinfo, "Displays important developer information.");
}
void registerCommand(string command, CommandHandler handler, string help) {
commands.Add(command, new CommandRegistration(command, handler, help));
}
public void appendLogLine(string line) {
Debug.Log(line);
if (scrollback.Count >= ConsoleController.scrollbackSize) {
scrollback.Dequeue();
}
scrollback.Enqueue(line);
log = scrollback.ToArray();
if (logChanged != null) {
logChanged(log);
}
}
public void runCommandString(string commandString) {
appendLogLine("$ " + commandString);
string[] commandSplit = parseArguments(commandString);
string[] args = new string[0];
if (commandSplit.Length < 1) {
appendLogLine(string.Format("Unable to process command '{0}'", commandString));
return;
} else if (commandSplit.Length >= 2) {
int numArgs = commandSplit.Length - 1;
args = new string[numArgs];
Array.Copy(commandSplit, 1, args, 0, numArgs);
}
runCommand(commandSplit[0].ToLower(), args);
commandHistory.Add(commandString);
}
public void runCommand(string command, string[] args) {
CommandRegistration reg = null;
if (!commands.TryGetValue(command, out reg)) {
appendLogLine(string.Format("Unknown command '{0}', type 'help' for list.", command));
} else {
if (reg.handler == null) {
appendLogLine(string.Format("Unable to process command '{0}', handler was null.", command));
} else {
reg.handler(args);
}
}
}
static string[] parseArguments(string commandString)
{
LinkedList<char> parmChars = new LinkedList<char>(commandString.ToCharArray());
bool inQuote = false;
var node = parmChars.First;
while (node != null)
{
var next = node.Next;
if (node.Value == '"') {
inQuote = !inQuote;
parmChars.Remove(node);
}
if (!inQuote && node.Value == ' ') {
node.Value = '\n';
}
node = next;
}
char[] parmCharsArr = new char[parmChars.Count];
parmChars.CopyTo(parmCharsArr, 0);
return (new string(parmCharsArr)).Split(new char[] {'\n'} , StringSplitOptions.RemoveEmptyEntries);
}
#region Command handlers
//Implement new commands in this region of the file.
/// <summary>
/// A test command to demonstrate argument checking/parsing.
/// Will repeat the given word a specified number of times.
/// </summary>
void babble(string[] args) {
if (args.Length < 2) {
appendLogLine("Expected 2 arguments.");
return;
}
string text = args[0];
if (string.IsNullOrEmpty(text)) {
appendLogLine("Expected arg1 to be text.");
} else {
int repeat = 0;
if (!Int32.TryParse(args[1], out repeat)) {
appendLogLine("Expected an integer for arg2.");
} else {
for(int i = 0; i < repeat; ++i) {
appendLogLine(string.Format("{0} {1}", text, i));
}
}
}
}
void echo(string[] args) {
StringBuilder sb = new StringBuilder();
foreach (string arg in args)
{
sb.AppendFormat("{0},", arg);
}
sb.Remove(sb.Length - 1, 1);
appendLogLine(sb.ToString());
}
void help(string[] args) {
foreach(CommandRegistration reg in commands.Values) {
appendLogLine(string.Format("{0}: {1}", reg.command, reg.help));
}
}
void hide(string[] args) {
if (visibilityChanged != null) {
visibilityChanged(false);
}
}
void repeatCommand(string[] args) {
for (int cmdIdx = commandHistory.Count - 1; cmdIdx >= 0; --cmdIdx) {
string cmd = commandHistory[cmdIdx];
if (String.Equals(repeatCmdName, cmd)) {
continue;
}
runCommandString(cmd);
break;
}
}
void reload(string[] args) {
Application.LoadLevel(Application.loadedLevel);
}
void resetPrefs(string[] args) {
PlayerPrefs.DeleteAll();
PlayerPrefs.Save();
}
void ver(string[] args) {
appendLogLine("La Llorona 16w14~");
}
void buildver(string[] args)
{
appendLogLine("Build 040916.04");
}
void sys(string[] args)
{
appendLogLine(SystemInfo.operatingSystem);
appendLogLine(SystemInfo.processorType);
appendLogLine(SystemInfo.systemMemorySize);
appendLogLine(SystemInfo.graphicsDeviceName);
}
void devinfo(string[] args)
{
appendLogLine(SystemInfo.deviceModel);
appendLogLine(SystemInfo.deviceType);
appendLogLine(SystemInfo.graphicsDeviceName);
appendLogLine(SystemInfo.graphicsMemorySize);
}
#endregion
使用UnityEngine;
使用制度;
使用System.Collections.Generic;
使用系统文本;
公共委托void命令处理程序(字符串[]args);
公共类控制台控制器{
#区域事件声明
//用于与ConsoleView通信
公共委托void LogChangedHandler(字符串[]日志);
公共事件日志更改Handler日志更改;
公共代理无效可见更改句柄(bool可见);
公共事件可视性更改Handler可视性更改;
#端区
///
///对象来保存有关每个命令的信息
///
类命令注册{
公共字符串命令{get;private set;}
公共命令处理程序{get;private set;}
公共字符串帮助{get;private set;}
公共命令注册(字符串命令、CommandHandler处理程序、字符串帮助){
this.command=命令;
this.handler=handler;
this.help=帮助;
}
}
///
///应该保留多少日志行?
///请注意,提交给带有嵌入换行符的appendLogLine的字符串将被计为一行。
///
常量int scrollbackSize=20;
Queue scrollback=新队列(scrollbackSize);
List commandHistory=新列表();
Dictionary命令=新建Dictionary();
public string[]log{get;private set;}//作为数组的回滚副本,便于ConsoleView使用
const string repeatCmdName=“!!”//repeat命令的名称,常量,因为如果它们在命令历史记录中,则需要跳过它们
公共控制台控制器(){
//添加命令时,必须在下面添加对registerCommand()的调用及其名称、实现方法和帮助文本。
registerCommand(“babble”,babble,“演示如何解析参数的示例命令”。babble[word][#重复次数]”;
registerCommand(“echo”,echo,“将参数作为数组回显(用于测试参数解析器)”;
注册表命令(“帮助”,帮助,“打印此帮助”);
注册表命令(“隐藏”,隐藏,“隐藏控制台”);
registerCommand(repeatCmdName,repeatCommand,“重复上一个命令”);
注册表命令(“重新加载”,重新加载,“重新加载游戏”);
注册表命令(“resetprefs”,resetprefs,“Reset&saves PlayerPrefs.”);
registerCommand(“ver”,ver,“显示当前游戏版本”);
registerCommand(“buildver”,buildver,“显示当前生成”);
注册表命令(“sys”,sys,“显示基本系统信息”);
registerCommand(“devinfo”,devinfo,“显示重要的开发人员信息”);
}
无效注册表命令(字符串命令、命令处理程序、字符串帮助){
添加(命令,新命令注册(命令,处理程序,帮助));
}
公共虚线(字符串线){
Debug.Log(行);
if(scrollback.Count>=ConsoleController.scrollbackSize){
scrollback.Dequeue();
}
回滚。排队(行);
log=scrollback.ToArray();
if(logChanged!=null){
对数变化(log);
}
}
public void runCommandString(string commandString){
appendLogLine(“$”+命令字符串);
string[]commandSplit=parseArguments(commandString);
字符串[]args=新字符串[0];
if(commandSplit.Length<1){
appendLogLine(string.Format(“无法处理命令“{0}”,commandString));
返回;
}else if(commandSplit.Length>=2){
int numArgs=commandSplit.Length-1;
args=新字符串[numArgs];
复制(commandSplit,1,args,0,numArgs);
}
runCommand(commandSplit[0].ToLower(),args);
commandHistory.Add(commandString);
}
public void runCommand(string命令,string[]args){
CommandRegistration reg=null;
如果(!commands.TryGetValue(command,out reg)){
appendLogLine(string.Format(“未知命令”{0}),键入“help”以获取列表,command));
}否则{
if(reg.handler==null){
appendLogLine(string.Format(“无法处理命令“{0}”,处理程序为空。”,命令));
}否则{
注册处理程序(args);
}
}
}
静态字符串[]解析参数(字符串命令字符串)
{
LinkedList parmChars=新的LinkedList(commandString.ToCharArray());
bool inQuote=false;
var node=parmChars.First;
while(节点!=null)
{
var next=node.next;
如果(node.Value==“”){
inQuote=!inQuote;
parmChars.Remove(节点);
}
如果(!inQuote&&node.Value=''){
node.Value='\n';
}
节点=下一个;
}
char[]parmCharsArr=新字符[parmChars.Count];
parmChars.CopyTo(parmChars,0);
return(新字符串(parmCharsArr)).Split(新字符[]{'\n'},StringSplitOptions.RemoveEmptyEntries);
}
#区域命令处理程序
//在文件的此区域中实现新命令。
///
///用于演示参数检查/解析的测试命令。
///将给定的单词重复指定的次数。
///
void-babble(字符串[]参数){
如果(参数长度<2){
appendLogLine(“应为2个参数”);
返回;
}
字符串text=args[0];
if(string.IsNullOrEmpty(text)){
appendLogLine(“预期arg1为文本”);
}否则{
int repeat=0;
如果(!Int32.TryParse(args[1],out repeat)){
appendLogLine(“arg2应为整数”);
}否则{
对于(int i=0;i appendLogLine(SystemInfo.systemMemorySize.ToString());
appendLogLine(SystemInfo.deviceType.ToString());
appendLogLine(SystemInfo.graphicsMemorySize.ToString());
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine.SceneManagement;
public delegate void CommandHandler(string[] args);
public class ConsoleController
{
#region Event declarations
// Used to communicate with ConsoleView
public delegate void LogChangedHandler(string[] log);
public event LogChangedHandler logChanged;
public delegate void VisibilityChangedHandler(bool visible);
public event VisibilityChangedHandler visibilityChanged;
#endregion
/// <summary>
/// Object to hold information about each command
/// </summary>
class CommandRegistration
{
public string command { get; private set; }
public CommandHandler handler { get; private set; }
public string help { get; private set; }
public CommandRegistration(string command, CommandHandler handler, string help)
{
this.command = command;
this.handler = handler;
this.help = help;
}
}
/// <summary>
/// How many log lines should be retained?
/// Note that strings submitted to appendLogLine with embedded newlines will be counted as a single line.
/// </summary>
const int scrollbackSize = 20;
Queue<string> scrollback = new Queue<string>(scrollbackSize);
List<string> commandHistory = new List<string>();
Dictionary<string, CommandRegistration> commands = new Dictionary<string, CommandRegistration>();
public string[] log { get; private set; } //Copy of scrollback as an array for easier use by ConsoleView
const string repeatCmdName = "!!"; //Name of the repeat command, constant since it needs to skip these if they are in the command history
public ConsoleController()
{
//When adding commands, you must add a call below to registerCommand() with its name, implementation method, and help text.
registerCommand("babble", babble, "Example command that demonstrates how to parse arguments. babble [word] [# of times to repeat]");
registerCommand("echo", echo, "echoes arguments back as array (for testing argument parser)");
registerCommand("help", help, "Print this help.");
registerCommand("hide", hide, "Hide the console.");
registerCommand(repeatCmdName, repeatCommand, "Repeat last command.");
registerCommand("reload", reload, "Reload game.");
registerCommand("resetprefs", resetPrefs, "Reset & saves PlayerPrefs.");
registerCommand("ver", ver, "Displays the current game version.");
registerCommand("buildver", buildver, "Displays the current build.");
registerCommand("sys", sys, "Displays basic system information.");
registerCommand("devinfo", devinfo, "Displays important developer information.");
}
void registerCommand(string command, CommandHandler handler, string help)
{
commands.Add(command, new CommandRegistration(command, handler, help));
}
public void appendLogLine(string line)
{
Debug.Log(line);
if (scrollback.Count >= ConsoleController.scrollbackSize)
{
scrollback.Dequeue();
}
scrollback.Enqueue(line);
log = scrollback.ToArray();
if (logChanged != null)
{
logChanged(log);
}
}
public void runCommandString(string commandString)
{
appendLogLine("$ " + commandString);
string[] commandSplit = parseArguments(commandString);
string[] args = new string[0];
if (commandSplit.Length < 1)
{
appendLogLine(string.Format("Unable to process command '{0}'", commandString));
return;
}
else if (commandSplit.Length >= 2)
{
int numArgs = commandSplit.Length - 1;
args = new string[numArgs];
Array.Copy(commandSplit, 1, args, 0, numArgs);
}
runCommand(commandSplit[0].ToLower(), args);
commandHistory.Add(commandString);
}
public void runCommand(string command, string[] args)
{
CommandRegistration reg = null;
if (!commands.TryGetValue(command, out reg))
{
appendLogLine(string.Format("Unknown command '{0}', type 'help' for list.", command));
}
else
{
if (reg.handler == null)
{
appendLogLine(string.Format("Unable to process command '{0}', handler was null.", command));
}
else
{
reg.handler(args);
}
}
}
static string[] parseArguments(string commandString)
{
LinkedList<char> parmChars = new LinkedList<char>(commandString.ToCharArray());
bool inQuote = false;
var node = parmChars.First;
while (node != null)
{
var next = node.Next;
if (node.Value == '"')
{
inQuote = !inQuote;
parmChars.Remove(node);
}
if (!inQuote && node.Value == ' ')
{
node.Value = '\n';
}
node = next;
}
char[] parmCharsArr = new char[parmChars.Count];
parmChars.CopyTo(parmCharsArr, 0);
return (new string(parmCharsArr)).Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
}
#region Command handlers
//Implement new commands in this region of the file.
/// <summary>
/// A test command to demonstrate argument checking/parsing.
/// Will repeat the given word a specified number of times.
/// </summary>
void babble(string[] args)
{
if (args.Length < 2)
{
appendLogLine("Expected 2 arguments.");
return;
}
string text = args[0];
if (string.IsNullOrEmpty(text))
{
appendLogLine("Expected arg1 to be text.");
}
else
{
int repeat = 0;
if (!Int32.TryParse(args[1], out repeat))
{
appendLogLine("Expected an integer for arg2.");
}
else
{
for (int i = 0; i < repeat; ++i)
{
appendLogLine(string.Format("{0} {1}", text, i));
}
}
}
}
void echo(string[] args)
{
StringBuilder sb = new StringBuilder();
foreach (string arg in args)
{
sb.AppendFormat("{0},", arg);
}
sb.Remove(sb.Length - 1, 1);
appendLogLine(sb.ToString());
}
void help(string[] args)
{
foreach (CommandRegistration reg in commands.Values)
{
appendLogLine(string.Format("{0}: {1}", reg.command, reg.help));
}
}
void hide(string[] args)
{
if (visibilityChanged != null)
{
visibilityChanged(false);
}
}
void repeatCommand(string[] args)
{
for (int cmdIdx = commandHistory.Count - 1; cmdIdx >= 0; --cmdIdx)
{
string cmd = commandHistory[cmdIdx];
if (String.Equals(repeatCmdName, cmd))
{
continue;
}
runCommandString(cmd);
break;
}
}
void reload(string[] args)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
void resetPrefs(string[] args)
{
PlayerPrefs.DeleteAll();
PlayerPrefs.Save();
}
void ver(string[] args)
{
appendLogLine("La Llorona 16w14~");
}
void buildver(string[] args)
{
appendLogLine("Build 040916.04");
}
void sys(string[] args)
{
appendLogLine(SystemInfo.operatingSystem);
appendLogLine(SystemInfo.processorType);
appendLogLine(SystemInfo.systemMemorySize.ToString());
appendLogLine(SystemInfo.graphicsDeviceName);
}
void devinfo(string[] args)
{
appendLogLine(SystemInfo.deviceModel);
appendLogLine(SystemInfo.deviceType.ToString());
appendLogLine(SystemInfo.graphicsDeviceName);
appendLogLine(SystemInfo.graphicsMemorySize.ToString());
}
#endregion
}