LoginSignup
7
4

More than 5 years have passed since last update.

Debug.Log()って打つの面倒くさい

Last updated at Posted at 2018-06-24

とにかく少しでも楽に書けるようにする方法を考えてみました
(スニペット使えとか言わないでください)

環境: Unity 2017 .NET 3.5

普通にメソッドを作る

定義.cs
public class d
{
    public static void l(object message)
    {
        Debug.Log(message);
    }
}
使う側.cs
d.l("あああ");
d.l(123.4f);
d.l("位置は " + transform.position);

普通ですね

拡張メソッドを作る

定義.cs
public static class Aaa
{
    public static void l(this object self)
    {
        Debug.Log(self);
    }
}
使う側.cs
"あああ".l();
123.4f.l();
("位置は " + transform.position).l();

連結しない限りは普通のメソッドよりこっちのほうが楽そうです

コンストラクタでやる

定義.cs
public class w
{
    public w(object message)
    {
        Debug.Log(message);
    }
}
使う側.cs
new w("あああ");
new w(123.4f);
new w("位置は " + transform.position);

ドットを打たなくていいという利点があります

インデクサーを使う

定義.cs
public class s
{
    public static s o = new s();

    private string str;

    public s this[object obj]
    {
        get
        {
            str += " " + obj.ToString();
            return this;
        }

        set {}
    }

    public static s operator --(s self)
    {
        Debug.Log(self.str);
        self.str = "";
        return self;
    }
}
使う側.cs
var o = s.o; //変数に入れないときつい

--o["あああ"];
o[123.4f]--;
--o["位置は " + transform.position];
--o["位置は"][transform.position];

うーん

演算子オーバーロードを使う

定義.cs
public class c
{
    public static c o = new c("");

    private string str;

    public c(string str)
    {
        this.str = str;
    }

    public static implicit operator c(string str)
    {
        return new c(str);
    }

    public static implicit operator c(UnityEngine.Object obj)
    {
        return new c(obj.ToString());
    }

    public static implicit operator c(ValueType val)
    {
        return new c(val.ToString());
    }

    public static c operator -(c left, c right)
    {
        Debug.Log(right.str);
        return null;
    }
}
使う側.cs
var o = c.o;

o-="あああ";
o-=123.4f;
o-="位置は " + transform.position;

かわいい

感想


Action<object> l = obj => Debug.Log(obj);
7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4