zoukankan      html  css  js  c++  java
  • Unity 开发游戏编写代码的技巧

    在平时开发游戏过程中,遇到一些编写代码很繁琐的问题. 我发现我团队中每个人都会遇到,就算打写出来分享下经验.

    条件断点

    利用IDE提供的工具, 右键断点的时候 输入条件, 当条件达成的时候,断点才能命中. (以前不知道这个功能总是要关闭游戏->编写代码-> 重新运行游戏 –> 查看结果 这个流程非常麻烦)

    class Program
        {
            static void Main(string[] args)
            {
                for (int i = 0; i < 100; i++)
                {
                    if (i == 50)      //利用条件断点,不写代码情况下 断点到某一个条件
                       {
                        Console.WriteLine("我断点到了");
                    }
                }
                Console.ReadKey();
            }
        }

    image

    运行表达式

    你在想在某一个时段运行xxx代码, 可以通过以下方式. 选择某一个变量右键-> 快速监视

    image

    image

    利用反射更好的编写测试代码

    在我编写代码的时候,需要编写一些测试工具方便调试游戏,  但是在编写一些测试代码的时候,  总是为了方便,快速不小心破坏了代码的原有结构, 比如一个字段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();
       }
     }
  • 相关阅读:
    【线型DP】【LCS】洛谷P4303 [AHOI2006]基因匹配
    【状压DP】SCOI2005-洛谷P1896-互不侵犯 (状压例题)
    【01背包】百度之星--度度熊剪纸条
    【线型DP】CF1012C Hills 小山坡
    【经典DP】洛谷P2285 [HZOI]2004 打鼹鼠
    【盗版动归】Codeforces998C——Convert to Ones 归一操作
    MySQL使用笔记(1)
    大学物理——光的干涉和衍射(2)
    大学物理——光的干涉和衍射(1)
    hdu5747 Aaronson 贪心
  • 原文地址:https://www.cnblogs.com/plateFace/p/7810401.html
Copyright © 2011-2022 走看看