查看: 4000|回复: 9
打印 上一主题 下一主题

[教程] 【转载】与Easy Console相同功能的代码

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-5-10 13:47:34 |只看该作者 |倒序浏览
转自 : 杰杰猫

写了一个unity3d的Debug Console类,可以通过命令字符串调用相应的函数,方便测试和调试,但还没写UI,大家自己去实现吧。
在Unity商店里有类似功能的插件Easyconsole,卖25$,觉得太贵了,就自己实现了一个。

DebugConsole.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;



static public class DebugConsole
{
    class Command {         
        public readonly string        CommandName;
        public readonly string        FuncName;
        public readonly Object        Callee;  

        public  Command(string cmdName, Object callee, string funcName) {
            CommandName = cmdName;
            FuncName    = funcName;
            Callee      = callee;
        }      
    }

    static private Dictionary<string, Command> mCmdLst;

    static DebugConsole() {
        mCmdLst = new Dictionary<string, Command>();            
    }      

    static public void RegisterCommand(string cmdName, Object callee, string funcName) {
        if (mCmdLst == null)
            mCmdLst = new Dictionary<string, Command>();

        Command cmd = new Command(cmdName, callee, funcName);
        if(!mCmdLst.ContainsKey(cmdName)) {
            mCmdLst.Add(cmdName, cmd);
        }
    }      

    static public bool Execute(string cmdStr) {
        string[] argv = ParseArguments(cmdStr);
        Command cmd = mCmdLst[argv[0]];
        if (cmd == null) {
            return false;
        }

        // get method
        MethodInfo method = cmd.Callee.GetType().GetMethod(cmd.FuncName);

        // create parameter list
        ParameterInfo[] paramInfoLst = method.GetParameters();
        Object[] paramLst     = new Object[paramInfoLst.Length];
        for(int i = 0; i < paramInfoLst.Length; ++i) {
            paramLst[i] = TypeDescriptor.GetConverter(paramInfoLst[i].ParameterType).ConvertFrom(argv[i + 1]);
        }

        // call methord with parameter list
        method.Invoke(cmd.Callee, paramLst);
        return true;
    }

    static private string[] ParseArguments(string commandLine) {
        char[] parmChars = commandLine.ToCharArray();
        bool inQuote = false;
        for (int index = 0; index < parmChars.Length; index++)
        {
            if (parmChars[index] == '"')
                inQuote = !inQuote;
            if (!inQuote && Char.IsWhiteSpace(parmChars[index]))
                parmChars[index] = '\n';
        }

        // remove double quote from begin and end
        string[] result = new string(parmChars).Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
        for(int i = 0; i< result.Length; i++) {
            result[i] = result[i].Trim('"');
        }
        return result;            
    }
}

TestDebugConsole.cs

using System;
using System.ComponentModel;
using UnityEngine;


[TypeConverter(typeof(JPointConverter))]
public class JPoint {
    public  int X;
    public  int Y;

    public JPoint(int x, int y) {
        X = x;
        Y = y;
    }
}

public class JPointConverter : TypeConverter {
    // must implement functions CanConvertFrom¡¢CanConvertTo¡¢ConvertFrom and ConvertTo.
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType is string) {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string) {
            string tmp =(string) value;
            string[] point = tmp.Split('#');
            int x = (int)Convert.ToInt32(point[0]);
            int y = (int)Convert.ToInt32(point[1]);
            return new JPoint(x, y);
        }
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

public class DebugConsoleTest : MonoBehaviour {

    void Start() {
        DebugConsole.RegisterCommand("output", this, "OutputMessage");
        DebugConsole.RegisterCommand("point", this, "CreatePoint");            
    }           

    public void OutputMessage(string str) {
        Debug.Log(str);
    }

    public void CreatePoint(JPoint point) {
        Debug.Log("JPoint x: " + point.X + " y: " + point.Y);
    }

    void  OnGUI() {
        if (GUILayout.Button("Test")) {
           DebugConsole.Execute("output hello!");

           DebugConsole.Execute("point 1#3");
        }
    }   
}  




分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

2

主题

1

听众

469

积分

设计实习生

Rank: 2

纳金币
0
精华
0

最佳新人

沙发
发表于 2013-5-10 18:21:47 |只看该作者
强力啊,学习了。。。。。。。
回复

使用道具 举报

0

主题

2

听众

6150

积分

高级设计师

Rank: 6Rank: 6

纳金币
62
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-5-10 20:04:51 |只看该作者
看起很棒啊,感谢分享!
回复

使用道具 举报

1

主题

1

听众

38

积分

设计初学者

Rank: 1

纳金币
25
精华
0

活跃会员 灌水之王

地板
发表于 2013-5-16 21:19:28 |只看该作者
打log的时候很方便吗?
回复

使用道具 举报

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53464
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

5#
发表于 2013-5-17 00:07:51 |只看该作者
谁可以介绍一下这个东东的用途?
回复

使用道具 举报

6#
无效楼层,该帖已经被删除
ku 智囊团   

89

主题

2

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
25
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2013-5-17 09:14:14 |只看该作者
谢谢分享,但我还不知道怎么用
回复

使用道具 举报

2

主题

1

听众

65

积分

设计初学者

Rank: 1

纳金币
59
精华
0

活跃会员 灌水之王

8#
发表于 2013-5-18 17:19:21 |只看该作者

看起很棒啊,感谢分享!
回复

使用道具 举报

1

主题

1

听众

1867

积分

助理设计师

Rank: 4

纳金币
63
精华
0

活跃会员

9#
发表于 2013-12-10 20:38:04 |只看该作者
好东西啊,谢谢分享啊!!!!!!!!!!!
回复

使用道具 举报

1

主题

1

听众

1867

积分

助理设计师

Rank: 4

纳金币
63
精华
0

活跃会员

10#
发表于 2013-12-10 20:39:24 |只看该作者
好东西啊,谢谢分享啊!!!!!!!!!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-6-5 21:04 , Processed in 0.094939 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部