在平时开发游戏过程中,遇到一些编写代码很繁琐的问题. 我发现我团队中每个人都会遇到,就算打写出来分享下经验.
条件断点
利用IDE提供的工具, 右键断点的时候 输入条件, 当条件达成的时候,断点才能命中. (以前不知道这个功能总是要关闭游戏->编写代码-> 重新运行游戏 –> 查看结果 这个流程非常麻烦)
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
if (i == 50) //利用条件断点,不写代码情况下 断点到某一个条件
{
Console.WriteLine("我断点到了");
}
}
Console.ReadKey();
}
}
运行表达式
你在想在某一个时段运行xxx代码, 可以通过以下方式. 选择某一个变量右键-> 快速监视
利用反射更好的编写测试代码
在我编写代码的时候,需要编写一些测试工具方便调试游戏, 但是在编写一些测试代码的时候, 总是为了方便,快速不小心破坏了代码的原有结构, 比如一个字段private 为了快速的访问到 就改成public. 我写了阶段简单实用的代码
public static class RefStatic
{ public static FieldInfo RefFieldVal(this object t, string name)
{
FieldInfo info = t.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
return info;
} public static FieldInfo RefStaticFieldVal(this object t, string name)
{
FieldInfo info = t.GetType().GetField(name, BindingFlags.Static | BindingFlags.NonPublic);
return info;
} public static FieldInfo RefSetFieldVal(this object t, string name, object val)
{
FieldInfo info = t.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
if (info != null)
info.SetValue(t, val); return info;
} public static FieldInfo RefSetStaticFieldVal(this object t, string name, object val)
{
FieldInfo info = t.GetType().GetField(name, BindingFlags.Static | BindingFlags.NonPublic);
if (info != null)
info.SetValue(t, val); return info;
} public static PropertyInfo RefSetPropertyVal(this object t, string name, object val)
{
PropertyInfo info = t.GetType().GetProperty(name);
if (info != null)
info.SetValue(t, val, null); return info;
} public static MethodInfo RefExecuteMethod(this object t, string name, object[] parameters)
{
MethodInfo info = t.GetType().GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
if (info != null)
info.Invoke(t, parameters); return info;
}
}
使用的栗子:
if (GUILayout.Button("生成全部单元格"))
{
var bag = (Panel_CommonBag)target;
GameObject[] go = (GameObject[])bag.RefFieldVal("m_CellList").GetValue(bag);
for (int i = 0; i < go.Length; i++)
{
KnapsackColumn k = go[i].GetComponent<KnapsackColumn>();
k.CreateCell();
}
}