<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>abaojin</title>
    <description>Welcome to view and use the blog</description>
    <link>https://abaojin.github.io/</link>
    <atom:link href="https://abaojin.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 23 Nov 2020 11:42:36 +0000</pubDate>
    <lastBuildDate>Mon, 23 Nov 2020 11:42:36 +0000</lastBuildDate>
    <generator>Jekyll v3.9.0</generator>
    
      <item>
        <title>unity assetbundle</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#介绍&quot; id=&quot;markdown-toc-介绍&quot;&gt;介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#打包&quot; id=&quot;markdown-toc-打包&quot;&gt;打包&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#压缩&quot; id=&quot;markdown-toc-压缩&quot;&gt;压缩&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#加载&quot; id=&quot;markdown-toc-加载&quot;&gt;加载&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#注意事项&quot; id=&quot;markdown-toc-注意事项&quot;&gt;注意事项&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考资料&quot; id=&quot;markdown-toc-参考资料&quot;&gt;参考资料&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;介绍&quot;&gt;介绍&lt;/h2&gt;

AssebBundle是Unity引擎处理资源方式，一个AssetBundle可以包含一个或多个Unity识别的Asset，今天
就简单聊聊AssetBundle关键技术点。

&lt;h2 id=&quot;打包&quot;&gt;打包&lt;/h2&gt;

使用BundleName方式构建AssetBundle。

&lt;ul&gt;
  &lt;li&gt;每一个AssetBundle可以包含1个或多个Asset，具体可以是贴图，动画，材质甚至是场景。&lt;/li&gt;
  &lt;li&gt;打包必须处理好AssetBundle与另一个AssetBundle之间的依赖， 否则容易造成资源冗余。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;压缩&quot;&gt;压缩&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;不压缩&lt;/li&gt;
  &lt;li&gt;LZMA 压缩率比较高，但是压缩时间和解压时间都比较慢&lt;/li&gt;
  &lt;li&gt;LZ4 压缩比相对较低，但是压缩时间和解压时间都比较快（推荐方式）&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;加载&quot;&gt;加载&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;AssetBundle.LoadFromFile 同步&lt;/li&gt;
  &lt;li&gt;AssetBundle.LoadFromFileAsync 异步
    &lt;ul&gt;
      &lt;li&gt;推荐加载方式，效率高，节省内存&lt;/li&gt;
      &lt;li&gt;加载只会加载AssetBundle头信息，不会加载具体Asset资源&lt;/li&gt;
      &lt;li&gt;只能加载使用LZ4方式压缩资源，不支持LZMA压缩的资源&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;AssetBundle.LoadFormMemory 同步&lt;/li&gt;
  &lt;li&gt;AssetBundle.LoadFormMemoryAsync 异步&lt;/li&gt;
  &lt;li&gt;AssetBundle.LoadFormStream 同步&lt;/li&gt;
  &lt;li&gt;AssetBundle.LoadFormStreamAsync 异步
    &lt;ul&gt;
      &lt;li&gt;不推荐加载方式，效率一般，主要浪费内容，必须先把资源加载成字节流&lt;/li&gt;
      &lt;li&gt;内容最少占用2份，如果资源采用加密方式，可以考虑这种方式&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;注意事项&quot;&gt;注意事项&lt;/h2&gt;

加载AssetBundle.LoadFromFile(Async)加载资源只加载AssetBundle头信息，是在Unity5.4之后版本支持的。

&lt;h2 id=&quot;参考资料&quot;&gt;参考资料&lt;/h2&gt;

&lt;a href=&quot;https://blog.csdn.net/llsansun/article/details/86215830&quot;&gt;Unity Compress&lt;/a&gt;
&lt;a href=&quot;https://learn.unity.com/tutorial/assets-resources-and-assetbundles#Loading_Asset_Bundles&quot;&gt;Unity AssetBundle&lt;/a&gt;

</description>
        <pubDate>Thu, 25 Jun 2020 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2020/06/25/unity-assetbundle/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2020/06/25/unity-assetbundle/</guid>
        
        <category>unity</category>
        
        <category>assetbundle</category>
        
        
        <category>unity</category>
        
      </item>
    
      <item>
        <title>unity shader 渲染管线和顺序</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#介绍&quot; id=&quot;markdown-toc-介绍&quot;&gt;介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#渲染管线&quot; id=&quot;markdown-toc-渲染管线&quot;&gt;渲染管线&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#渲染顺序&quot; id=&quot;markdown-toc-渲染顺序&quot;&gt;渲染顺序&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#注意事项&quot; id=&quot;markdown-toc-注意事项&quot;&gt;注意事项&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考资料&quot; id=&quot;markdown-toc-参考资料&quot;&gt;参考资料&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;介绍&quot;&gt;介绍&lt;/h2&gt;

熟悉3D渲染顺序，对于游戏开发来说非常重要，不仅仅是基础知识，更是一项非常重要的工作技能，相信很多
同学对于Unity引擎的渲染顺序还是没有没有搞清楚，如果你已经非常清楚了，完全跳过阅读，避免浪费你宝
贵的时间。

&lt;h2 id=&quot;渲染管线&quot;&gt;渲染管线&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;模型数据&lt;/li&gt;
  &lt;li&gt;顶点着色器（自定义操作）
    &lt;ul&gt;
      &lt;li&gt;顶点变换&lt;/li&gt;
      &lt;li&gt;顶点裁剪&lt;/li&gt;
      &lt;li&gt;面剔除Cull&lt;/li&gt;
      &lt;li&gt;图元装配&lt;/li&gt;
      &lt;li&gt;光栅化操作&lt;/li&gt;
      &lt;li&gt;深度测试（Early-Z）&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;片段着色器（自定义操作）
    &lt;ul&gt;
      &lt;li&gt;文理赋值&lt;/li&gt;
      &lt;li&gt;光照处理&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;各种测试
    &lt;ul&gt;
      &lt;li&gt;透明度测试 （Alpha Test Early-Z操作会失效）&lt;/li&gt;
      &lt;li&gt;模板测试（Stencil Test）&lt;/li&gt;
      &lt;li&gt;深度测试 (Depth Test)&lt;/li&gt;
      &lt;li&gt;混合测试 (Blend Mode)
        &lt;ul&gt;
          &lt;li&gt;Blend Off 关闭混合&lt;/li&gt;
          &lt;li&gt;Blend SrcFactor DstFactor 基本配置，并启动混合操作，产生颜色乘以SrcFactor加上已有颜色乘以DstFactor&lt;/li&gt;
          &lt;li&gt;Blend SrcFactor DstFactor SrcFactorA DstFactorA 同上，只是Alpha单独处理&lt;/li&gt;
          &lt;li&gt;
            &lt;table&gt;
              &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td&gt;BlendOp Add&lt;/td&gt;
                  &lt;td&gt;Min&lt;/td&gt;
                  &lt;td&gt;Max&lt;/td&gt;
                  &lt;td&gt;Sub&lt;/td&gt;
                  &lt;td&gt;RevSub 混合的方式&lt;/td&gt;
                &lt;/tr&gt;
              &lt;/tbody&gt;
            &lt;/table&gt;
            &lt;ul&gt;
              &lt;li&gt;Add 默认操作，将源像素和目标像素相加&lt;/li&gt;
              &lt;li&gt;Sub 将源像素减去目标像素&lt;/li&gt;
              &lt;li&gt;Min 取目标像素和源像素较小者&lt;/li&gt;
              &lt;li&gt;Max 取目标像素和源像素较大者&lt;/li&gt;
              &lt;li&gt;RevSub 将目标像素减去源像素&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;Blend Factor 混合因子
            &lt;ul&gt;
              &lt;li&gt;One 值为1，使用该因子，用来使帧缓冲区颜色和目标颜色完全通过&lt;/li&gt;
              &lt;li&gt;Zero 值为0，使用该因子，用来删除帧缓冲区颜色和目标颜色&lt;/li&gt;
              &lt;li&gt;SrcColor——使用此因子为将当前值乘以帧缓冲区源颜色的值&lt;/li&gt;
              &lt;li&gt;SrcAlpha——使用此因子为将当前值乘以帧缓冲区源颜色的Alpha的值&lt;/li&gt;
              &lt;li&gt;DstColor——使用此因子为将当前值乘以帧缓冲区目标颜色的值。&lt;/li&gt;
              &lt;li&gt;DstAlpha——使用此因子为将当前值乘以帧缓冲区目标颜色Alpha分量的值&lt;/li&gt;
              &lt;li&gt;OneMinusSrcColor——使用此因子为将当前值乘以(1-帧缓冲区源颜色值)&lt;/li&gt;
              &lt;li&gt;OneMinusSrcAlpha——使用此因子为将当前值乘以(1-帧缓冲区源颜色Alpha分量的值)&lt;/li&gt;
              &lt;li&gt;OneMinusDstColor——使用此因子为将当前值乘以(1-目标颜色值)&lt;/li&gt;
              &lt;li&gt;OneMinusDstAlpha——使用此因子为将当前值乘以(1-目标颜色Alpha分量的值)&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;更新帧缓冲中颜色值&lt;/li&gt;
&lt;/ul&gt;

备注： 顺序可能存在问题，随时更正。

&lt;h2 id=&quot;渲染顺序&quot;&gt;渲染顺序&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Camera Depth 越小越优先&lt;/li&gt;
  &lt;li&gt;RendererQueue 2500以下的（含2500）
    &lt;ul&gt;
      &lt;li&gt;Sorting Layer/Order in Layer 按照Sorting Layer/Order in Layer 设置的值，越小越优先&lt;/li&gt;
      &lt;li&gt;RenderQueue， 越小越优先&lt;/li&gt;
      &lt;li&gt;RenderQueue 相等，由近到远排序优先&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;RenderQueue 2500已上
    &lt;ul&gt;
      &lt;li&gt;Sorting Layer/Order in Layer 按照Sorting Layer/Order in Layer 设置的值，越小越优先&lt;/li&gt;
      &lt;li&gt;RenderQueue， 越小越优先&lt;/li&gt;
      &lt;li&gt;RenderQueue 相等，由远到近排序优先&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;注意事项&quot;&gt;注意事项&lt;/h2&gt;

Sprite组件和Canvas组件 默认使用的Shader未写入z缓冲，但是进行z缓冲测试 默认 RenderQueue 均为 Transfront=3000

&lt;h2 id=&quot;参考资料&quot;&gt;参考资料&lt;/h2&gt;

&lt;a href=&quot;https://docs.unity3d.com/Manual/SL-Reference.html&quot;&gt;Unity Shader Reference&lt;/a&gt;
&lt;a href=&quot;https://blog.csdn.net/candycat1992/article/details/41599167&quot;&gt;Unity AlphaTest AlphaBlending&lt;/a&gt;

</description>
        <pubDate>Mon, 15 Jun 2020 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2020/06/15/unity-shader-queue/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2020/06/15/unity-shader-queue/</guid>
        
        <category>unity</category>
        
        <category>shader</category>
        
        
        <category>unity</category>
        
      </item>
    
      <item>
        <title>lua 编码规范</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#目的&quot; id=&quot;markdown-toc-目的&quot;&gt;目的&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#编码规范&quot; id=&quot;markdown-toc-编码规范&quot;&gt;编码规范&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#命名惯例&quot; id=&quot;markdown-toc-命名惯例&quot;&gt;命名惯例&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#分割规范&quot; id=&quot;markdown-toc-分割规范&quot;&gt;分割规范&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#代码建议&quot; id=&quot;markdown-toc-代码建议&quot;&gt;代码建议&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考资料&quot; id=&quot;markdown-toc-参考资料&quot;&gt;参考资料&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;目的&quot;&gt;目的&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;统一编码标准，提高开发效率。&lt;/li&gt;
  &lt;li&gt;便于代码阅读，降低维护成本。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;编码规范&quot;&gt;编码规范&lt;/h2&gt;

&lt;h3 id=&quot;命名惯例&quot;&gt;命名惯例&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;所有lua文件命名时使用小写字母、下划线。&lt;/li&gt;
  &lt;li&gt;类名、变量名尽可能使用有意义的英文，类名使用帕斯卡命名法，变量名使用骆驼式命名法。&lt;/li&gt;
  &lt;li&gt;常量、消息号定义时用大写，单词间 _ 分割  eg:KIND_PET_FOOD。&lt;/li&gt;
  &lt;li&gt;函数名使用骆驼式命名法。&lt;/li&gt;
&lt;/ul&gt;

解释：

&lt;ul&gt;
  &lt;li&gt;骆驼式命名法：第一个单字以小写字母开始；第二个单字的首字母大写或每一个单字的首字母都采用大写字母。&lt;/li&gt;
  &lt;li&gt;帕斯卡命名法：和骆驼命名法很像，只有一点区别，就是首字母要大写。(单字之间不以空格断开或连接号)。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;分割规范&quot;&gt;分割规范&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;使用空行
    &lt;ul&gt;
      &lt;li&gt;在方法之间。&lt;/li&gt;
      &lt;li&gt;在方法内部代码的逻辑段落小节之间。&lt;/li&gt;
      &lt;li&gt;在注释行之前。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;使用空格符
    &lt;ul&gt;
      &lt;li&gt;运算符和运算符之间，如： c = a + b。&lt;/li&gt;
      &lt;li&gt;在参数列表中的逗号后面，如: function m1(year, month)。&lt;/li&gt;
      &lt;li&gt;在for语句时，如：for k, v in pairs(t) do。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;不使用空格符
    &lt;ul&gt;
      &lt;li&gt;函数定义，如：function test1( a )。&lt;/li&gt;
      &lt;li&gt;函数调用，如：test1( 1 )。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;使用换行符
    &lt;ul&gt;
      &lt;li&gt;禁止在一行中写多条语句。&lt;/li&gt;
      &lt;li&gt;一条语句的长度一般超过了80个字符时，应该换行。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;代码建议&quot;&gt;代码建议&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;通用逻辑不建议依赖逻辑数据层&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;参考资料&quot;&gt;参考资料&lt;/h2&gt;

&lt;a href=&quot;https://github.com/lua/lua&quot;&gt;lua源码&lt;/a&gt;
</description>
        <pubDate>Sat, 25 Apr 2020 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2020/04/25/lua-coding-standard/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2020/04/25/lua-coding-standard/</guid>
        
        <category>lua</category>
        
        
        <category>lua</category>
        
      </item>
    
      <item>
        <title>lua 性能分析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot; id=&quot;markdown-toc-简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#测试函数&quot; id=&quot;markdown-toc-测试函数&quot;&gt;测试函数&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#lua表性能&quot; id=&quot;markdown-toc-lua表性能&quot;&gt;lua表性能&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#全局和局部变量性能&quot; id=&quot;markdown-toc-全局和局部变量性能&quot;&gt;全局和局部变量性能&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#垃圾回收性能&quot; id=&quot;markdown-toc-垃圾回收性能&quot;&gt;垃圾回收性能&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考资料&quot; id=&quot;markdown-toc-参考资料&quot;&gt;参考资料&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

lua作为一种脚本语言，主要的用途有两个方面，一方面是作为项目或应用的配置脚本使用，另一方面
作为应用的逻辑编写脚本使用，作为逻辑编写脚本主要提交游戏方便，今天主要讨论lua编写时的一些
影响性能的注意事项，希望对于热爱lua这门脚本语言的同学以启发。

&lt;h2 id=&quot;测试函数&quot;&gt;测试函数&lt;/h2&gt;

为了进行性能，这里给出一个简单性能测试通用函数，下面的测试都将使用这个函数进行测试，废话不
多说，直接贴上代码。

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;profiler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;~=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;function&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;os.clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;os.clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;total_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;average_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;

    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;function run times=&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;,average cost time=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;average_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;lua表性能&quot;&gt;lua表性能&lt;/h2&gt;

table是Lua 的一种数据结构用来帮助我们创建不同的数据类型，如：数组、字典等。
下面给出两种方式看看性能数据

&lt;ul&gt;
  &lt;li&gt;第一种方式&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func_table_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;profiler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func_table_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;err&quot;&gt;测试结果：&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;average&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0631&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;第二种方式&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func_table_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;profiler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func_table_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;err&quot;&gt;测试结果：&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;average&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0279&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面两份测试结果可以看出第二种方式速度差不多是第一种的3倍，主要原因是：
默认创建出来的的表，都是空的，在插入元素的过程，逐渐翻倍扩大，从0到1， 1到2，2到4，…都会触发realloc，同时把旧元素拷贝到新申请的空间中，对于最终有成千上万个元素的table，扩张的开销可以接受，但是对于大量生成小的table的场景，会明显拖慢性能，可以通过lua的构造函数，让Lua的编译器预分配空间，Hash的扩展也是同样的原理。

&lt;h2 id=&quot;全局和局部变量性能&quot;&gt;全局和局部变量性能&lt;/h2&gt;

全局变量和局部变量是编写代码时时刻需要涉及的，但是什么时候使用全局，什么时候使用局部，这些都是非常影响代码运行效率，先分析两种方式，看看性能结果。

&lt;ul&gt;
  &lt;li&gt;第一种方式&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func_var_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;math.type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;profiler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func_var_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;err&quot;&gt;测试结果：&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;average&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;06832&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;第二种方式&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func_var_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;math.type&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;profiler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func_var_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;err&quot;&gt;测试结果：&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;average&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;03914&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面测试结果可以看出第二种方式也是第一种方式的2倍效率，其中原因主要是因为
全局变量涉及的到表的查询和修改，所以性能要显著差于local变量，对于函数也是
相同的道理。

&lt;h2 id=&quot;垃圾回收性能&quot;&gt;垃圾回收性能&lt;/h2&gt;

lua5.0之后采用是3色标记的GC回收算法，相比较5.0的双色标记算法的有点是算法
可以分布进行计算，避免阻塞影响逻辑脚本性能，具体实现和影响性能测试改天单
独写一篇文章讲解，今天简单说如何避免GC的开销。

&lt;ul&gt;
  &lt;li&gt;减少创建对象的次数，如果有需要可以循环利用&lt;/li&gt;
  &lt;li&gt;根据实际业务的需要，控制垃圾回收的启动和关闭&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;参考资料&quot;&gt;参考资料&lt;/h2&gt;

&lt;a href=&quot;https://github.com/lua/lua&quot;&gt;lua源码&lt;/a&gt;
</description>
        <pubDate>Mon, 09 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2020/03/09/lua-performance/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2020/03/09/lua-performance/</guid>
        
        <category>lua</category>
        
        
        <category>lua</category>
        
      </item>
    
      <item>
        <title>gc Garbage Collection分类</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot; id=&quot;markdown-toc-简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#分类&quot; id=&quot;markdown-toc-分类&quot;&gt;分类&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#引用计数算法比较老回收算法&quot; id=&quot;markdown-toc-引用计数算法比较老回收算法&quot;&gt;引用计数算法（比较老回收算法）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#根搜索算法&quot; id=&quot;markdown-toc-根搜索算法&quot;&gt;根搜索算法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#标记-清除算法&quot; id=&quot;markdown-toc-标记-清除算法&quot;&gt;标记-清除算法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#标记-复制算法新生代的gc&quot; id=&quot;markdown-toc-标记-复制算法新生代的gc&quot;&gt;标记-复制算法：（新生代的GC）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#标记-整理算法老年代的gc&quot; id=&quot;markdown-toc-标记-整理算法老年代的gc&quot;&gt;标记-整理算法：（老年代的GC）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#分代收集算法新生代的gc老年代的gc&quot; id=&quot;markdown-toc-分代收集算法新生代的gc老年代的gc&quot;&gt;分代收集算法（新生代的GC+老年代的GC）&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

GC：Garbage Collection 垃圾收集

（1）GC：Garbage Collection 垃圾收集。这里所谓的垃圾指的是在系统运行过程当中所产生的一些无用的对象，这些对象占据着一定的内存空间，如果长期不被释放，可能导致OOM。

在C/C++里是由程序猿自己去申请、管理和释放内存空间，因此没有GC的概念。而在Java中，后台专门有一个专门用于垃圾回收的线程来进行监控、扫描，自动将一些无用的内存进行释放，这就是垃圾收集的一个基本思想，目的在于防止由程序猿引入的人为的内存泄露。

（2）事实上，GC的历史比Java久远，1960年诞生于MIT的Lisp是第一门真正使用内存动态分配和垃圾收集技术的语言。当Lisp还在胚胎时期时，人们就在思考GC需要完成的3件事情：哪些内存需要回收？什么时候回收？如何回收？

（3）内存区域中的程序计数器、虚拟机栈、本地方法栈这3个区域随着线程而生，线程而灭；栈中的栈帧随着方法的进入和退出而有条不紊地执行着出栈和入栈的操作，每个栈帧中分配多少内存基本是在类结构确定下来时就已知的。在这几个区域不需要过多考虑回收的问题，因为方法结束或者线程结束时，内存自然就跟着回收了。

&lt;h2 id=&quot;分类&quot;&gt;分类&lt;/h2&gt;

&lt;h3 id=&quot;引用计数算法比较老回收算法&quot;&gt;引用计数算法（比较老回收算法）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    概念

    给对象中添加一个引用计数器，每当有一个地方引用它时，计数器值就加1；当引用失效时，计数器值就减1；任何时刻计数器为0的对象就是不可能再被使用的。
  &lt;/li&gt;
  &lt;li&gt;
    优点

    实现比较简单，判断的效率也比较高，使用的地方也是比较多的。
  &lt;/li&gt;
  &lt;li&gt;
    缺点

    &lt;ul&gt;
      &lt;li&gt;循环引用的对象无法回收&lt;/li&gt;
      &lt;li&gt;伴随加法和减法运算，会消耗掉一定的性能&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;根搜索算法&quot;&gt;根搜索算法&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    概念

    由于引用计数算法的缺陷，所以JVM一般会采用一种新的算法，叫做根搜索算法。它的处理方式就是，设立若干种根对象，当任何一个根对象到某一个对象均不可达时，则认为这个对象是可以被回收的。
  &lt;/li&gt;
  &lt;li&gt;
    算法分析

    &lt;ul&gt;
      &lt;li&gt;
        可达性分析

        从根（GC Roots）的对象作为起始点，开始向下搜索，搜索所走过的路径称为“引用链”，当一个对象到GC Roots没有任何引用链相连（用图论的概念来讲，就是从GC Roots到这个对象不可达）时，则证明此对象是不可用的。
      &lt;/li&gt;
      &lt;li&gt;
        根对象（GC Roots）

        一般来说根对象主要包含以下几个方便，当前不用语言的实现会有所不同。

        &lt;ul&gt;
          &lt;li&gt;栈（栈帧中的本地变量表）中引用的对象。&lt;/li&gt;
          &lt;li&gt;方法区中的静态成员。&lt;/li&gt;
          &lt;li&gt;方法区中的常量引用的对象（全局变量）&lt;/li&gt;
          &lt;li&gt;本地方法栈中JNI（一般说的Native方法）引用的对象。&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;标记-清除算法&quot;&gt;标记-清除算法&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    概念

    标记-清除算法是现代垃圾回收算法的思想基础。标记-清除算法将垃圾回收分为两个阶段：标记阶段和清除阶段。一种可行的实现是，在标记阶段，首先通过根节点，标记所有从根节点开始的可达对象。因此，未被标记的对象就是未被引用的垃圾对象；然后，在清除阶段，清除所有未被标记的对象。
  &lt;/li&gt;
  &lt;li&gt;
    算法分析

    &lt;ul&gt;
      &lt;li&gt;
        标记

        标记的过程其实就是，遍历所有的GC Roots，然后将所有GC Roots可达的对象标记为存活的对象。
      &lt;/li&gt;
      &lt;li&gt;
        清除

        清除的过程将遍历堆中所有的对象，将没有标记的对象全部清除掉。
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    缺点

    &lt;ul&gt;
      &lt;li&gt;
        首先，它的缺点就是效率比较低（递归与全堆对象遍历），导致stop the world的时间比较长，尤其对于交互式的应用程序来说简直是无法接受。试想一下，如果你玩一个网站，这个网站一个小时就挂五分钟，你还玩吗？
      &lt;/li&gt;
      &lt;li&gt;
        第二点主要的缺点，则是这种方式清理出来的空闲内存是不连续的，这点不难理解，我们的死亡对象都是随即的出现在内存的各个角落的，现在把它们清除之后，内存的布局自然会乱七八糟。而为了应付这一点，JVM就不得不维持一个内存的空闲列表，这又是一种开销。而且在分配数组对象的时候，寻找连续的内存空间会不太好找。
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;标记-复制算法新生代的gc&quot;&gt;标记-复制算法：（新生代的GC）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    概念

    将原有的内存空间分为两块，每次只使用其中一块，在垃圾回收时，将正在使用的内存中的存活对象复制到未使用的内存块中，之后，清除正在使用的内存块中的所有对象，交换两个内存的角色，完成垃圾回收。
  &lt;/li&gt;
  &lt;li&gt;
    优点

    回收完成后对象的空间是连续的，解决内存碎片的问题，实现比较简单，效率也比较高。
  &lt;/li&gt;
  &lt;li&gt;
    缺点

    空间的浪费。
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;标记-整理算法老年代的gc&quot;&gt;标记-整理算法：（老年代的GC）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    概念

    标记-压缩算法适合用于存活对象较多的场合，如老年代。它在标记-清除算法的基础上做了一些优化。和标记-清除算法一样，标记-压缩算法也首先需要从根节点开始，对所有可达对象做一次标记；但之后，它并不简单的清理未标记的对象，而是将所有的存活对象压缩到内存的一端；之后，清理边界外所有的空间。
  &lt;/li&gt;
  &lt;li&gt;
    算法分析

    &lt;ul&gt;
      &lt;li&gt;
        标记

        它的第一个阶段与标记/清除算法是一模一样的，均是遍历GC Roots，然后将存活的对象标记。
      &lt;/li&gt;
      &lt;li&gt;
        整理

        移动所有存活的对象，且按照内存地址次序依次排列，然后将末端内存地址以后的内存全部回收。因此，第二阶段才称为整理阶段。
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    优点

    标记/整理算法不仅可以弥补标记/清除算法当中，内存区域分散的缺点，也消除了复制算法当中，内存减半的高额代价。
  &lt;/li&gt;
  &lt;li&gt;
    缺点

    &lt;ul&gt;
      &lt;li&gt;
        效率：复制算法&amp;gt;标记/整理算法&amp;gt;标记/清除算法（此处的效率只是简单的对比时间复杂度，实际情况不一定如此）。
      &lt;/li&gt;
      &lt;li&gt;
        内存整齐度：复制算法=标记/整理算法&amp;gt;标记/清除算法。
      &lt;/li&gt;
      &lt;li&gt;
        内存利用率：标记/整理算法=标记/清除算法&amp;gt;复制算法。
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;分代收集算法新生代的gc老年代的gc&quot;&gt;分代收集算法（新生代的GC+老年代的GC）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    介绍

    当前商业虚拟机的GC都是采用的“分代收集算法”，这并不是什么新的思想，只是根据对象的存活周期的不同将内存划分为几块儿。一般是把Java堆分为新生代和老年代：短命对象归为新生代，长命对象归为老年代。
  &lt;/li&gt;
  &lt;li&gt;
    概念

    &lt;ul&gt;
      &lt;li&gt;少量对象存活，适合复制算法：在新生代中，每次GC时都发现有大批对象死去，只有少量存活，那就选用复制算法，只需要付出少量存活对象的复制成本就可以完成GC。&lt;/li&gt;
      &lt;li&gt;大量对象存活，适合用标记-清理/标记-整理：在老年代中，因为对象存活率高、没有额外空间对他进行分配担保，就必须使用“标记-清理”/“标记-整理”算法进行GC。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    案例

    &lt;ul&gt;
      &lt;li&gt;java 语言的 JVM&lt;/li&gt;
      &lt;li&gt;.net 平台的 CLR&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Mon, 09 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2020/03/09/gc-note/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2020/03/09/gc-note/</guid>
        
        <category>gc</category>
        
        
        <category>gc</category>
        
      </item>
    
      <item>
        <title>unity 热更新版本控制</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#介绍&quot; id=&quot;markdown-toc-介绍&quot;&gt;介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#版本&quot; id=&quot;markdown-toc-版本&quot;&gt;版本&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#结构&quot; id=&quot;markdown-toc-结构&quot;&gt;结构&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#流程&quot; id=&quot;markdown-toc-流程&quot;&gt;流程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#注意事项&quot; id=&quot;markdown-toc-注意事项&quot;&gt;注意事项&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;介绍&quot;&gt;介绍&lt;/h2&gt;

热更新或热修复是现在手游必不可少的东西，这里不介绍怎么进行资源，采用什么脚本进行
游戏的开发，我们假定我们现在需要更新游戏的资源、脚本、配置表等数据资源，该如何来
控制资源的版本呢。

&lt;h2 id=&quot;版本&quot;&gt;版本&lt;/h2&gt;

游戏版本一般推荐使用3位表示

&lt;ul&gt;
  &lt;li&gt;版本号组成： 大版本号.次版本号.资源版本号&lt;/li&gt;
  &lt;li&gt;应用版本号： 大版本号+次版本号+资源版本号&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;结构&quot;&gt;结构&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;cdn
    &lt;ul&gt;
      &lt;li&gt;ios
        &lt;ul&gt;
          &lt;li&gt;ios_app_1.0.ipa&lt;/li&gt;
          &lt;li&gt;1.0 (大版本号)
            &lt;ul&gt;
              &lt;li&gt;1 (资源版本号可以使用SVN号)
                &lt;ul&gt;
                  &lt;li&gt;version.txt&lt;/li&gt;
                  &lt;li&gt;depends&lt;/li&gt;
                  &lt;li&gt;bundles&lt;/li&gt;
                  &lt;li&gt;scripts&lt;/li&gt;
                  &lt;li&gt;configs&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;2 (资源版本号可以使用SVN号)
                &lt;ul&gt;
                  &lt;li&gt;version.txt&lt;/li&gt;
                  &lt;li&gt;depends&lt;/li&gt;
                  &lt;li&gt;bundles&lt;/li&gt;
                  &lt;li&gt;scripts&lt;/li&gt;
                  &lt;li&gt;configs&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;android
        &lt;ul&gt;
          &lt;li&gt;android_app_1.0.apk&lt;/li&gt;
          &lt;li&gt;1.0 (大版本号)
            &lt;ul&gt;
              &lt;li&gt;1 (资源版本号可以使用SVN号)
                &lt;ul&gt;
                  &lt;li&gt;version.txt&lt;/li&gt;
                  &lt;li&gt;depends&lt;/li&gt;
                  &lt;li&gt;bundles&lt;/li&gt;
                  &lt;li&gt;scripts&lt;/li&gt;
                  &lt;li&gt;configs&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;2 (资源版本号可以使用SVN号)
                &lt;ul&gt;
                  &lt;li&gt;version.txt&lt;/li&gt;
                  &lt;li&gt;depends&lt;/li&gt;
                  &lt;li&gt;bundles&lt;/li&gt;
                  &lt;li&gt;scripts&lt;/li&gt;
                  &lt;li&gt;configs&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;osx
        &lt;ul&gt;
          &lt;li&gt;osx_app_1.0.dpg&lt;/li&gt;
          &lt;li&gt;1.0 (大版本号)
            &lt;ul&gt;
              &lt;li&gt;1 (资源版本号可以使用SVN号)
                &lt;ul&gt;
                  &lt;li&gt;version.txt&lt;/li&gt;
                  &lt;li&gt;depends&lt;/li&gt;
                  &lt;li&gt;bundles&lt;/li&gt;
                  &lt;li&gt;scripts&lt;/li&gt;
                  &lt;li&gt;configs&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;2 (资源版本号可以使用SVN号)
                &lt;ul&gt;
                  &lt;li&gt;version.txt&lt;/li&gt;
                  &lt;li&gt;depends&lt;/li&gt;
                  &lt;li&gt;bundles&lt;/li&gt;
                  &lt;li&gt;scripts&lt;/li&gt;
                  &lt;li&gt;configs&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;流程&quot;&gt;流程&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    获得服务器版本号，与客户端当前版本号作比较，如果是大版本更新，去商店下载，如果是小版本更新则去资源服务器下载。
  &lt;/li&gt;
  &lt;li&gt;
    拼接资源服务器地址，获得versionFile下载地址，根据大版本号，手机平台和资源版本号得到下载地址为：baseurl/ios/1.0/170513.1/VersionFile.txt。
  &lt;/li&gt;
  &lt;li&gt;
    下载VersionFile，并与上一次手机缓存的VersionFile作对比，生成从上一次更新到这次更新需要下载的文件列表，然后逐一去对应的地址下载下载，资源地址需要根据VersionFile里面的版本号去生成。例如：baseurl/ios/1.0/170512.1/test.txt,baseurl/ios/1.0/170403.1/test.txt。每一个资源去对应的版本文件夹下去下载。
  &lt;/li&gt;
  &lt;li&gt;
    下载完所有文件后，保存本次下载的VersionFile，等待下一次更新时与新的versionFile再做比较。
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;注意事项&quot;&gt;注意事项&lt;/h2&gt;

当有大版本更新时，更新结束后，应该删除之前缓存的所有更新，因为新包都是默认带着所有资源。这样既减少包的大小，也避免了游戏运行时加载错资源。因为VersionFile存放了我们所有的更新文件，所以游戏加载资源会根据VersionFile是否存在该资源来决定去加载下载目录里的资源还是去加载包里面自带的资源。因此游戏包更新后为了避免加载到上一个版本的资源，应该删除掉之前下载目录里的文件。
</description>
        <pubDate>Sat, 02 Nov 2019 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2019/11/02/unity-hotfix/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2019/11/02/unity-hotfix/</guid>
        
        <category>unity</category>
        
        
        <category>unity</category>
        
      </item>
    
      <item>
        <title>unity 脚本编写记事</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#协程返回&quot; id=&quot;markdown-toc-协程返回&quot;&gt;协程返回&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#对象标记&quot; id=&quot;markdown-toc-对象标记&quot;&gt;对象标记&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#装箱操作&quot; id=&quot;markdown-toc-装箱操作&quot;&gt;装箱操作&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gc工作量&quot; id=&quot;markdown-toc-gc工作量&quot;&gt;GC工作量&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#更新操作&quot; id=&quot;markdown-toc-更新操作&quot;&gt;更新操作&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;协程返回&quot;&gt;协程返回&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;不好方式：yield return 0;
正确方式：yield return null;

不好方式：
while(!IsComplete) {
	yield return new WaitForSeconds(1f);
}
正确方式：
WaitForSeconds delay = new WaitForSeconds(1f);
while(!IsComplete) {
	yield return delay;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;对象标记&quot;&gt;对象标记&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;不好方式：
gameObject.tag == &quot;TagString&quot;;
正确方式：
gameObject.CompareTag(&quot;TagString&quot;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;装箱操作&quot;&gt;装箱操作&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;不好方式：
ArrayList list = new ArrayList();
正确方式：
List&amp;lt;T&amp;gt; list = new List&amp;lt;T&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;gc工作量&quot;&gt;GC工作量&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;不好方式：
public struct Item
{
	public string Name;
	public int Age;
	public Vector3 Postion;
}
public Item[] ItemArray;
正确方式：
public string[] Names;
public int[] Ages;
public Vector3[] Postions;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;更新操作&quot;&gt;更新操作&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;不好方式：
private void Update()
{
	Collider c = GetComponent&amp;lt;Collider&amp;gt;();
}
正确方式：
private Collider m_c;
private void Start() 
{
	m_c = gameObject.GetComponent&amp;lt;Collider&amp;gt;();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

##

</description>
        <pubDate>Thu, 11 Apr 2019 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2019/04/11/unity-script/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2019/04/11/unity-script/</guid>
        
        <category>unity</category>
        
        
        <category>unity</category>
        
      </item>
    
      <item>
        <title>unity 项目优化</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#逻辑优化&quot; id=&quot;markdown-toc-逻辑优化&quot;&gt;逻辑优化&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#工具优化&quot; id=&quot;markdown-toc-工具优化&quot;&gt;工具优化&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#规范优化&quot; id=&quot;markdown-toc-规范优化&quot;&gt;规范优化&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;逻辑优化&quot;&gt;逻辑优化&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    网络模块

    网络层消息分组处理，比如聊天组和逻辑组
  网络层消息组分帧处理，减少网络消息单帧吞吐量
  控制网络层消息优先级队列处理
  &lt;/li&gt;
  &lt;li&gt;
    界面模块

    界面管理分组管理
  界面开发必须通过事件机制解耦
  界面导航采用堆栈方式
  界面关闭应分为：返回和关闭(返回代表返回栈顶元素，关闭直接返回主界面)
  界面关闭指定到跳转界面同样入栈

    梳理界面界面开发流程，界面开发不高效，且缺少通用型组件。
  &lt;/li&gt;
  &lt;li&gt;
    资源管理

    资源Bundle管理
  资源Asset的管理

    资源池策略(引用计数或反向依赖)
  资源加载和卸载
  资源卸载下载一定通过Unload(true)方式卸载

    资源打包策略(主包和依赖包方式)
  资源打包只关心依赖图集、贴图

    主资源通过资源清单方式维护
  &lt;/li&gt;
  &lt;li&gt;
    配置数据模块

    数据生成数据高效数据文件(文件格式和二进制格式)
  数据对象生成一定不能暴露出可修改静态配置数据可能
  &lt;/li&gt;
  &lt;li&gt;
    版本自动化

    高效使用的版本资源化规划
  版本打包脚本实现，建议使用Python
  版本部署参数配置(版本号,CDN服务器,是否打开日志,平台等)
  版本部署参数应分为不同平台
  自动化工具可统一继承Jenkins平台
  &lt;/li&gt;
  &lt;li&gt;
    客户端Tick管理

    不同系统底层心跳管理必须有先后顺序
  系统心跳管理必须可以通过参数控制，便于性能调优
  &lt;/li&gt;
  &lt;li&gt;
    项目适配方案

    适配方案应项目初期确定
  项目适配配置文件应分为不同平台不同配置
  所有适配参数应该统一由设置模块调用
  &lt;/li&gt;
  &lt;li&gt;
    对象池方案

    普通对象缓存池管理方案
  带类型的对象池管理方案
  &lt;/li&gt;
  &lt;li&gt;
    项目SDK和Device

    SDK需要在客户端封装成统一的接口，不同平台不同实现
  设备信息也需要封装成统一接口，不同平台分别实现
  &lt;/li&gt;
  &lt;li&gt;
    音效方案

    音效需要分组播放
  &lt;/li&gt;
  &lt;li&gt;
    动画优化

    控制动画的更新类型，一般是主角是一直更新，其他的动画是在视野范围内更新
  动画状态机的动画剪辑动态替换
  &lt;/li&gt;
  &lt;li&gt;
    特效打包

    所有的特效作为主包，贴图按照依赖关系打包
  &lt;/li&gt;
  &lt;li&gt;
    单元测试

    根据游戏类型定义资源游戏单元检测框架，主要是为了美术检测资源
  定期进行性能检测 兼容性检测 压力检测
  &lt;/li&gt;
  &lt;li&gt;
    场景优化

    Instance 技术
  显示对象的权限管理，隐藏应该是所有的逻辑也隐藏
  显示对象更新权限管理，统一由显示对象管理器控制
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;工具优化&quot;&gt;工具优化&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;日志系统&lt;/li&gt;
  &lt;li&gt;资源打包&lt;/li&gt;
  &lt;li&gt;资源规格检查工具&lt;/li&gt;
  &lt;li&gt;整理目前游戏存在工具（常见网格编辑，动画编辑，界面导出，配置生成等）。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;规范优化&quot;&gt;规范优化&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;程序编写上规范（编码，注释，结构，注意事项等）。&lt;/li&gt;
  &lt;li&gt;美术资源制作上规范 （制作方式，目录结构，命名规范等）。&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Fri, 22 Mar 2019 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2019/03/22/unity-optimization/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2019/03/22/unity-optimization/</guid>
        
        <category>unity</category>
        
        
        <category>unity</category>
        
      </item>
    
      <item>
        <title>android service生命周期</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#介绍&quot; id=&quot;markdown-toc-介绍&quot;&gt;介绍&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;介绍&quot;&gt;介绍&lt;/h2&gt;

android service生命周期理解对于正确使用service非常重要，这里主要记录一下
启动service和绑定service生命周期资料。

</description>
        <pubDate>Wed, 13 Jun 2018 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2018/06/13/android-service/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2018/06/13/android-service/</guid>
        
        <category>android</category>
        
        
        <category>android</category>
        
      </item>
    
      <item>
        <title>unity 项目基本结构介绍</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#engine框架介绍&quot; id=&quot;markdown-toc-engine框架介绍&quot;&gt;Engine框架介绍&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#开发条件&quot; id=&quot;markdown-toc-开发条件&quot;&gt;开发条件&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#框架模块&quot; id=&quot;markdown-toc-框架模块&quot;&gt;框架模块&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#bin输出目录&quot; id=&quot;markdown-toc-bin输出目录&quot;&gt;Bin（输出目录）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#example案例工程&quot; id=&quot;markdown-toc-example案例工程&quot;&gt;Example（案例工程）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#framework框架工程&quot; id=&quot;markdown-toc-framework框架工程&quot;&gt;Framework（框架工程）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#frameworkeditor编辑器框架工程&quot; id=&quot;markdown-toc-frameworkeditor编辑器框架工程&quot;&gt;Framework.editor（编辑器框架工程）&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#程序工程结构案例&quot; id=&quot;markdown-toc-程序工程结构案例&quot;&gt;程序工程结构案例&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#客户端总体目录&quot; id=&quot;markdown-toc-客户端总体目录&quot;&gt;客户端总体目录&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;engine框架介绍&quot;&gt;Engine框架介绍&lt;/h1&gt;

&lt;h2 id=&quot;开发条件&quot;&gt;开发条件&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Unity 2017.2.0&lt;/li&gt;
  &lt;li&gt;Visual Studio 2015以上版本&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;框架模块&quot;&gt;框架模块&lt;/h2&gt;

&lt;h3 id=&quot;bin输出目录&quot;&gt;Bin（输出目录）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;框架开发依赖引擎DLL库（注意版本号统一）&lt;/li&gt;
  &lt;li&gt;框架编译输出DLL，编写脚本自动拷贝到引擎里&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example案例工程&quot;&gt;Example（案例工程）&lt;/h3&gt;

框架模块测试Unity工程

&lt;h3 id=&quot;framework框架工程&quot;&gt;Framework（框架工程）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;3rd（框架依赖第三方插件）&lt;/li&gt;
  &lt;li&gt;Base（基础模块）
    &lt;ul&gt;
      &lt;li&gt;Attribute（属性定义）&lt;/li&gt;
      &lt;li&gt;Buffer（流操作）&lt;/li&gt;
      &lt;li&gt;Component（常用组件）&lt;/li&gt;
      &lt;li&gt;Const（常量定义）&lt;/li&gt;
      &lt;li&gt;Entry（框架入口）&lt;/li&gt;
      &lt;li&gt;EventPool（事件池）&lt;/li&gt;
      &lt;li&gt;Entension（类工程扩展模块）&lt;/li&gt;
      &lt;li&gt;Helper（常用辅助模块）&lt;/li&gt;
      &lt;li&gt;Object（常用对象定义）&lt;/li&gt;
      &lt;li&gt;Singleton（单利模块）&lt;/li&gt;
      &lt;li&gt;Protocol （消息协议）&lt;/li&gt;
      &lt;li&gt;TaskPool（任务池模块）&lt;/li&gt;
      &lt;li&gt;Variable（默认类型包裹）&lt;/li&gt;
      &lt;li&gt;Utils（辅助函数）&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Config（配置模块）&lt;/li&gt;
  &lt;li&gt;Debugger（调试模块）&lt;/li&gt;
  &lt;li&gt;Download（资源下载模块）&lt;/li&gt;
  &lt;li&gt;Event（事件模块）&lt;/li&gt;
  &lt;li&gt;Http（Http模块）&lt;/li&gt;
  &lt;li&gt;Localization（国际化模块）&lt;/li&gt;
  &lt;li&gt;Log（日志输出模块）&lt;/li&gt;
  &lt;li&gt;Network（网络层TCP模块）&lt;/li&gt;
  &lt;li&gt;Pool（对象池模块）&lt;/li&gt;
  &lt;li&gt;Procedure（流程管理模块）&lt;/li&gt;
  &lt;li&gt;PureMVC（MVC模块）&lt;/li&gt;
  &lt;li&gt;Resource（资源加载模块）&lt;/li&gt;
  &lt;li&gt;Scene（场景模块）&lt;/li&gt;
  &lt;li&gt;Setting（客户端设置模块）&lt;/li&gt;
  &lt;li&gt;Sound（音频模块）&lt;/li&gt;
  &lt;li&gt;UI（界面模块）&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;frameworkeditor编辑器框架工程&quot;&gt;Framework.editor（编辑器框架工程）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Base （编辑器基础代码）
    &lt;ul&gt;
      &lt;li&gt;Config （编辑器配置）&lt;/li&gt;
      &lt;li&gt;Coroutine （编辑器模式下的携程）&lt;/li&gt;
      &lt;li&gt;Helper （编辑器一些辅助函数，结构不太合理）&lt;/li&gt;
      &lt;li&gt;Utils （常见的编辑器辅助代码）&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Base（基础模块）
    &lt;ul&gt;
      &lt;li&gt;Application （应用构建-正在优化）&lt;/li&gt;
      &lt;li&gt;AssetBundle （资源打包功能）&lt;/li&gt;
      &lt;li&gt;AssetBundleBrowser （资源预览工具）&lt;/li&gt;
      &lt;li&gt;Folder （目录辅助代码）&lt;/li&gt;
      &lt;li&gt;Inspector （脚本空间面显示）&lt;/li&gt;
      &lt;li&gt;LineArgs （启动Unity命令参数解析工具）&lt;/li&gt;
      &lt;li&gt;Scene （场景相关编辑器工具）&lt;/li&gt;
      &lt;li&gt;Standard （项目标准定义或检查工具）&lt;/li&gt;
      &lt;li&gt;UGUI （ugui扩展脚本）&lt;/li&gt;
      &lt;li&gt;Unity （引擎相关编辑器代码）&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;程序工程结构案例&quot;&gt;程序工程结构案例&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;3rd-第三方插件目录
    &lt;ul&gt;
      &lt;li&gt;Demigiant 缓动插件&lt;/li&gt;
      &lt;li&gt;MeshAnimator  Mesh动画插件&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Arts-美术资源目录
    &lt;ul&gt;
      &lt;li&gt;Atlas 游戏图集目录，主要为UI图集&lt;/li&gt;
      &lt;li&gt;Audio 音频资源目录&lt;/li&gt;
      &lt;li&gt;Config 项目配置文件目录
        &lt;ul&gt;
          &lt;li&gt;Editor 编辑器插件（位置应该动态指定）&lt;/li&gt;
          &lt;li&gt;Load 预加载配置&lt;/li&gt;
          &lt;li&gt;Localization 国际化配置&lt;/li&gt;
          &lt;li&gt;Tab 配表Tab&lt;/li&gt;
          &lt;li&gt;Unit 角色配置&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Effect 项目特效资源目录&lt;/li&gt;
      &lt;li&gt;Font 字体资源目录&lt;/li&gt;
      &lt;li&gt;Level 关卡或场景资源目录
        &lt;ul&gt;
          &lt;li&gt;Battle_NameXXX 战斗场景&lt;/li&gt;
          &lt;li&gt;Login 登陆初始化场景&lt;/li&gt;
          &lt;li&gt;CreatePlayer 创建角色场景&lt;/li&gt;
          &lt;li&gt;Map 瓦片大世界初始化场景&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Shader 渲染脚本资源&lt;/li&gt;
      &lt;li&gt;Unit 显示单元目录
        &lt;ul&gt;
          &lt;li&gt;Hero 英雄角色资源目录&lt;/li&gt;
          &lt;li&gt;Map 大地图相关资源&lt;/li&gt;
          &lt;li&gt;Soldier 士兵角色资源目录&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Window 界面资源目录&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Editor Default Resources -默认标记器资源目录
  &lt;/li&gt;
  &lt;li&gt;Plugins-插件资源目录
    &lt;ul&gt;
      &lt;li&gt;Framework 项目框架插件&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Launcher 游戏初始化场景，启动游戏
  &lt;/li&gt;
  &lt;li&gt;Scripts
    &lt;ul&gt;
      &lt;li&gt;Auto 自动生成代码目录（目前包括消息和表格）&lt;/li&gt;
      &lt;li&gt;Base 项目基础代码目录（辅助代码，辅助模块）&lt;/li&gt;
      &lt;li&gt;Editor 项目编辑器代码目录&lt;/li&gt;
      &lt;li&gt;Logic 项目逻辑代码目录（请勿将基础代码放在该目录）
        &lt;ul&gt;
          &lt;li&gt;Map 大地图核模块&lt;/li&gt;
          &lt;li&gt;CreatePlayer 创建角色模块&lt;/li&gt;
          &lt;li&gt;Building 建筑模块&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Standard Assets-标准资源目录
  &lt;/li&gt;
  &lt;li&gt;StreamingAssets-流资源包输出目录&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;客户端总体目录&quot;&gt;客户端总体目录&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Engine 客户端框架
    &lt;ul&gt;
      &lt;li&gt;Framework 客户端框架&lt;/li&gt;
      &lt;li&gt;Framework.Editor 客户端框架编辑器部分&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Project 客户端工程
    &lt;ul&gt;
      &lt;li&gt;ProjectName 程序客户端工程&lt;/li&gt;
      &lt;li&gt;ProjectNameArt 美术客户端工程&lt;/li&gt;
      &lt;li&gt;Shared 共享对象
        &lt;ul&gt;
          &lt;li&gt;Build 构建版本和资源
            &lt;ul&gt;
              &lt;li&gt;Client 客户端构建生成目录&lt;/li&gt;
              &lt;li&gt;Publish 发布资源和版本脚本（Python 3.6.2）&lt;/li&gt;
              &lt;li&gt;Server 服务器构建生成目录&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;Conf 项目配置资源
            &lt;ul&gt;
              &lt;li&gt;message 协议文件&lt;/li&gt;
              &lt;li&gt;config 配配置文件（表等）&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;Tool 基本工程（生成协议和配表运行文件）&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Tool 工具工程&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;doc 文档目录&lt;/li&gt;
  &lt;li&gt;personal 个人目录&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 11 Mar 2018 00:00:00 +0000</pubDate>
        <link>https://abaojin.github.io/2018/03/11/unity-project/</link>
        <guid isPermaLink="true">https://abaojin.github.io/2018/03/11/unity-project/</guid>
        
        <category>unity</category>
        
        
        <category>unity</category>
        
      </item>
    
  </channel>
</rss>
