Unity游戏启动时都干了些啥

前面几个月公司,项目进行大版本更新,事比较多,就耽搁了,最近闲下来了就开始整理下,之前写的东西。

Unity游戏在启动的时候会加载一些默认的资源,这些资源包括Texture、Shader、Mesh等等。下面分为两部分细说:

那些资源会打包进游戏包

既然要测试,就得有工程是吧。好,我们用Unity创建一个空工程,工程中就一个场景如图:

这个场景中的所有对象都删除(包括MainCamera哦)。然后导出APK包(不会的自行百度)。

我用Unity5.6.1打包的,导出的APK文件是20.5M。AndroidStudio创建的空工程,导出的APK只有1.8M,差距还是蛮大的。

导出的apk包直接用压缩工具打开查看。如图:

我们可以看到 assets/bin/Data/ 目录下面有Unity3D引擎的 .dll 文件,以及 lib/ 目录下的 .so 文件,这些都是Unity3D引擎的代码文件,剩下的那些就是Unity3D引擎引入的资源文件。

unity_builtin_extra 和 unity default resources 这两个文件,它们在文件大小上是资源文件中最大的。
这些文件是类似于AssetBundle的文件格式,用工具
http://en.unity3d.netobf.com/DevXUnityUnPack提取。

用法如下图:

打开apk之后如图:

注意:你会看到 unity_builtin_extra 里面都是Shader文件,而 unity default resources 里面则包含纹理、默认的Arial字体、材质、Shader等。

游戏启动时那些东西加载了
分析完了unity那些资源打包进APK了,下面再来看游戏那些资源被加载了呢?

脚本:

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using UnityEngine;

    public class Test : MonoBehaviour {

        // Use this for initialization
        void Start () {
            StringBuilder str = new StringBuilder();
            var objs = Resources.FindObjectsOfTypeAll<UnityEngine.Object>();

            str.AppendLine("total objs count :  " + objs.Length );
            Debug.Log("total objs count :  " + objs.Length);
            foreach(var obj in objs)
            {
                var type = obj.GetType();
                str.AppendLine("name= " + obj.name + " ,  Type= " + type);
                Debug.Log("name= " + obj.name + " ,  Type= " + type);
            }

            //Application.persistentDataPath:   C:\Users\username\AppData\LocalLow\company name\product name
            string path = Path.Combine(Application.dataPath, "logs.txt");
            File.WriteAllText(path, str.ToString());
        }

    }

场景中创建一个空物体,把这个脚本挂上去,打包。这里测试 我就直接打成exe了,在Editor下运行 看到有Editor资源。

输入日志如下:

        total objs count :  223
        name= NavMeshSettings ,  Type= UnityEngine.Object
        name= OcclusionCullingSettings ,  Type= UnityEngine.Object
        name= Hidden/BlitCopy ,  Type= UnityEngine.Material
        name=  ,  Type= UnityEngine.Texture
        name= Hidden/Internal-GUITexture ,  Type= UnityEngine.Material
        name=  ,  Type= UnityEngine.Material
        name= UnityNHxRoughness ,  Type= UnityEngine.Texture2D
        name= UnityDitherMask3D ,  Type= UnityEngine.Texture3D
        name= UnityDitherMask2D ,  Type= UnityEngine.Texture2D
        name= UnityDefaultCube ,  Type= UnityEngine.Cubemap
        name= UnityDefaultCubeArray ,  Type= UnityEngine.CubemapArray
        name= UnityDefault2DArray ,  Type= UnityEngine.Texture2DArray
        name= UnityDefault3D ,  Type= UnityEngine.Texture3D
        name= UnityDefault2D ,  Type= UnityEngine.Texture2D
        name= UnityBlackCube ,  Type= UnityEngine.Cubemap
        name= UnityAttenuation ,  Type= UnityEngine.Texture2D
        name= UnityHalo ,  Type= UnityEngine.Texture2D
        name= UnityGrayscaleRamp ,  Type= UnityEngine.Texture2D
        name= UnityGrey ,  Type= UnityEngine.Texture2D
        name= UnityRed ,  Type= UnityEngine.Texture2D
        name= UnityRandomRotation ,  Type= UnityEngine.Texture2D
        name= UnityBlack ,  Type= UnityEngine.Texture2D
        name= UnityWhite ,  Type= UnityEngine.Texture2D
        name= UnityNormalMap ,  Type= UnityEngine.Texture2D
        name= InternalIdentityTransform ,  Type= UnityEngine.Transform
        name= InternalIdentityTransform ,  Type= UnityEngine.GameObject
        name= PlayerSettings ,  Type= UnityEngine.Object
        name= InputManager ,  Type= UnityEngine.Object
        name= BuildSettings ,  Type= UnityEngine.Object
        name= GraphicsSettings ,  Type= UnityEngine.Rendering.GraphicsSettings
        name= Hidden/Internal-DeferredShading ,  Type= UnityEngine.Shader
        name= Hidden/Internal-DeferredReflections ,  Type= UnityEngine.Shader
        name= Hidden/Internal-ScreenSpaceShadows ,  Type= UnityEngine.Shader
        name= Hidden/Internal-PrePassLighting ,  Type= UnityEngine.Shader
        name= Hidden/Internal-DepthNormalsTexture ,  Type= UnityEngine.Shader
        name= Hidden/Internal-MotionVectors ,  Type= UnityEngine.Shader
        name= Hidden/Internal-Halo ,  Type= UnityEngine.Shader
        name= Hidden/Internal-Flare ,  Type= UnityEngine.Shader
        name= Legacy Shaders/Diffuse ,  Type= UnityEngine.Shader
        name= Hidden/CubeBlur ,  Type= UnityEngine.Shader
        name= Hidden/CubeCopy ,  Type= UnityEngine.Shader
        name= Hidden/CubeBlend ,  Type= UnityEngine.Shader
        name= Sprites/Default ,  Type= UnityEngine.Shader
        name= UI/Default ,  Type= UnityEngine.Shader
        name= Hidden/VideoDecode ,  Type= UnityEngine.Shader
        name= Hidden/Internal-StencilWrite ,  Type= UnityEngine.Shader
        name= Hidden/Internal-CombineDepthNormals ,  Type= UnityEngine.Shader
        name= Hidden/BlitCopy ,  Type= UnityEngine.Shader
        name= Hidden/BlitCopyDepth ,  Type= UnityEngine.Shader
        name= Hidden/Internal-GUITextureClip ,  Type= UnityEngine.Shader
        name= Hidden/Internal-GUITextureClipText ,  Type= UnityEngine.Shader
        name= Hidden/Internal-GUITexture ,  Type= UnityEngine.Shader
        name= Hidden/Internal-GUITextureBlit ,  Type= UnityEngine.Shader
        name= Hidden/ConvertTexture ,  Type= UnityEngine.Shader
        name= Hidden/VR/BlitCopyFromTexArray ,  Type= UnityEngine.Shader
        name= Sprites-Default ,  Type= UnityEngine.Material
        name= QualitySettings ,  Type= UnityEngine.QualitySettings
        name= RuntimeInitializeOnLoadManager ,  Type= UnityEngine.Object
        name= Hidden/InternalErrorShader ,  Type= UnityEngine.Shader
        name= Hidden/InternalClear ,  Type= UnityEngine.Shader
        name= Soft ,  Type= UnityEngine.Texture2D
        name= UnitySplash-cube ,  Type= UnityEngine.Texture2D
        name= UnitySplash-cube ,  Type= UnityEngine.Sprite
        name= DeveloperConsole ,  Type= UnityEngine.TextAsset
        name= GUISkin ,  Type= UnityEngine.TextAsset
        name= TagManager ,  Type= UnityEngine.Object
        name= AudioManager ,  Type= UnityEngine.Object
        name= ScriptMapper ,  Type= UnityEngine.Object
        name= Legacy Shaders/VertexLit ,  Type= UnityEngine.Shader
        name= Skybox/Procedural ,  Type= UnityEngine.Shader
        name= MonoManager ,  Type= UnityEngine.Object
        name= AudioSpatializerMicrosoft ,  Type= UnityEngine.TextAsset
        name= SpatialMappingBase ,  Type= UnityEngine.TextAsset
        name= SpatialMappingCollider ,  Type= UnityEngine.TextAsset
        name= SpatialMappingRenderer ,  Type= UnityEngine.TextAsset
        name= HoloLensInput ,  Type= UnityEngine.TextAsset
        name= HoloLensInputModule ,  Type= UnityEngine.TextAsset
        name= PlaymodeTestsController ,  Type= UnityEngine.TextAsset
        name= PlayModeRunnerCallback ,  Type= UnityEngine.TextAsset
        name= CallbackExecutor ,  Type= UnityEngine.TextAsset
        name= Placeholder ,  Type= UnityEngine.TextAsset
        name= AnalyticsTracker ,  Type= UnityEngine.TextAsset
        name= NetworkAnimator ,  Type= UnityEngine.TextAsset
        name= NetworkBehaviour ,  Type= UnityEngine.TextAsset
        name= NetworkDiscovery ,  Type= UnityEngine.TextAsset
        name= NetworkIdentity ,  Type= UnityEngine.TextAsset
        name= NetworkLobbyManager ,  Type= UnityEngine.TextAsset
        name= NetworkLobbyPlayer ,  Type= UnityEngine.TextAsset
        name= NetworkManager ,  Type= UnityEngine.TextAsset
        name= NetworkManagerHUD ,  Type= UnityEngine.TextAsset
        name= NetworkMigrationManager ,  Type= UnityEngine.TextAsset
        name= NetworkProximityChecker ,  Type= UnityEngine.TextAsset
        name= NetworkStartPosition ,  Type= UnityEngine.TextAsset
        name= NetworkTransformChild ,  Type= UnityEngine.TextAsset
        name= NetworkTransform ,  Type= UnityEngine.TextAsset
        name= NetworkTransformVisualizer ,  Type= UnityEngine.TextAsset
        name= AsyncUtil ,  Type= UnityEngine.TextAsset
        name= NetworkAnimator ,  Type= UnityEngine.TextAsset
        name= NetworkBehaviour ,  Type= UnityEngine.TextAsset
        name= NetworkDiscovery ,  Type= UnityEngine.TextAsset
        name= NetworkIdentity ,  Type= UnityEngine.TextAsset
        name= NetworkLobbyManager ,  Type= UnityEngine.TextAsset
        name= NetworkLobbyPlayer ,  Type= UnityEngine.TextAsset
        name= NetworkManager ,  Type= UnityEngine.TextAsset
        name= NetworkManagerHUD ,  Type= UnityEngine.TextAsset
        name= NetworkMigrationManager ,  Type= UnityEngine.TextAsset
        name= NetworkProximityChecker ,  Type= UnityEngine.TextAsset
        name= NetworkStartPosition ,  Type= UnityEngine.TextAsset
        name= NetworkTransformChild ,  Type= UnityEngine.TextAsset
        name= NetworkTransform ,  Type= UnityEngine.TextAsset
        name= NetworkTransformVisualizer ,  Type= UnityEngine.TextAsset
        name= EventSystem ,  Type= UnityEngine.TextAsset
        name= EventTrigger ,  Type= UnityEngine.TextAsset
        name= UIBehaviour ,  Type= UnityEngine.TextAsset
        name= BaseInput ,  Type= UnityEngine.TextAsset
        name= BaseInputModule ,  Type= UnityEngine.TextAsset
        name= PointerInputModule ,  Type= UnityEngine.TextAsset
        name= StandaloneInputModule ,  Type= UnityEngine.TextAsset
        name= TouchInputModule ,  Type= UnityEngine.TextAsset
        name= BaseRaycaster ,  Type= UnityEngine.TextAsset
        name= Physics2DRaycaster ,  Type= UnityEngine.TextAsset
        name= PhysicsRaycaster ,  Type= UnityEngine.TextAsset
        name= Button ,  Type= UnityEngine.TextAsset
        name= Dropdown ,  Type= UnityEngine.TextAsset
        name= Graphic ,  Type= UnityEngine.TextAsset
        name= GraphicRaycaster ,  Type= UnityEngine.TextAsset
        name= Image ,  Type= UnityEngine.TextAsset
        name= InputField ,  Type= UnityEngine.TextAsset
        name= Mask ,  Type= UnityEngine.TextAsset
        name= MaskableGraphic ,  Type= UnityEngine.TextAsset
        name= RawImage ,  Type= UnityEngine.TextAsset
        name= RectMask2D ,  Type= UnityEngine.TextAsset
        name= Scrollbar ,  Type= UnityEngine.TextAsset
        name= ScrollRect ,  Type= UnityEngine.TextAsset
        name= Selectable ,  Type= UnityEngine.TextAsset
        name= Slider ,  Type= UnityEngine.TextAsset
        name= Text ,  Type= UnityEngine.TextAsset
        name= Toggle ,  Type= UnityEngine.TextAsset
        name= ToggleGroup ,  Type= UnityEngine.TextAsset
        name= AspectRatioFitter ,  Type= UnityEngine.TextAsset
        name= CanvasScaler ,  Type= UnityEngine.TextAsset
        name= ContentSizeFitter ,  Type= UnityEngine.TextAsset
        name= GridLayoutGroup ,  Type= UnityEngine.TextAsset
        name= HorizontalLayoutGroup ,  Type= UnityEngine.TextAsset
        name= HorizontalOrVerticalLayoutGroup ,  Type= UnityEngine.TextAsset
        name= LayoutElement ,  Type= UnityEngine.TextAsset
        name= LayoutGroup ,  Type= UnityEngine.TextAsset
        name= VerticalLayoutGroup ,  Type= UnityEngine.TextAsset
        name= BaseMeshEffect ,  Type= UnityEngine.TextAsset
        name= Outline ,  Type= UnityEngine.TextAsset
        name= PositionAsUV1 ,  Type= UnityEngine.TextAsset
        name= Shadow ,  Type= UnityEngine.TextAsset
        name= EventSystem ,  Type= UnityEngine.TextAsset
        name= EventTrigger ,  Type= UnityEngine.TextAsset
        name= UIBehaviour ,  Type= UnityEngine.TextAsset
        name= BaseInput ,  Type= UnityEngine.TextAsset
        name= BaseInputModule ,  Type= UnityEngine.TextAsset
        name= PointerInputModule ,  Type= UnityEngine.TextAsset
        name= StandaloneInputModule ,  Type= UnityEngine.TextAsset
        name= TouchInputModule ,  Type= UnityEngine.TextAsset
        name= BaseRaycaster ,  Type= UnityEngine.TextAsset
        name= Physics2DRaycaster ,  Type= UnityEngine.TextAsset
        name= PhysicsRaycaster ,  Type= UnityEngine.TextAsset
        name= Button ,  Type= UnityEngine.TextAsset
        name= Dropdown ,  Type= UnityEngine.TextAsset
        name= Graphic ,  Type= UnityEngine.TextAsset
        name= GraphicRaycaster ,  Type= UnityEngine.TextAsset
        name= Image ,  Type= UnityEngine.TextAsset
        name= InputField ,  Type= UnityEngine.TextAsset
        name= Mask ,  Type= UnityEngine.TextAsset
        name= MaskableGraphic ,  Type= UnityEngine.TextAsset
        name= RawImage ,  Type= UnityEngine.TextAsset
        name= RectMask2D ,  Type= UnityEngine.TextAsset
        name= Scrollbar ,  Type= UnityEngine.TextAsset
        name= ScrollRect ,  Type= UnityEngine.TextAsset
        name= Selectable ,  Type= UnityEngine.TextAsset
        name= Slider ,  Type= UnityEngine.TextAsset
        name= Text ,  Type= UnityEngine.TextAsset
        name= Toggle ,  Type= UnityEngine.TextAsset
        name= ToggleGroup ,  Type= UnityEngine.TextAsset
        name= AspectRatioFitter ,  Type= UnityEngine.TextAsset
        name= CanvasScaler ,  Type= UnityEngine.TextAsset
        name= ContentSizeFitter ,  Type= UnityEngine.TextAsset
        name= GridLayoutGroup ,  Type= UnityEngine.TextAsset
        name= HorizontalLayoutGroup ,  Type= UnityEngine.TextAsset
        name= HorizontalOrVerticalLayoutGroup ,  Type= UnityEngine.TextAsset
        name= LayoutElement ,  Type= UnityEngine.TextAsset
        name= LayoutGroup ,  Type= UnityEngine.TextAsset
        name= VerticalLayoutGroup ,  Type= UnityEngine.TextAsset
        name= BaseMeshEffect ,  Type= UnityEngine.TextAsset
        name= Outline ,  Type= UnityEngine.TextAsset
        name= PositionAsUV1 ,  Type= UnityEngine.TextAsset
        name= Shadow ,  Type= UnityEngine.TextAsset
        name= AudioSpatializerMicrosoft ,  Type= UnityEngine.TextAsset
        name= SpatialMappingBase ,  Type= UnityEngine.TextAsset
        name= SpatialMappingCollider ,  Type= UnityEngine.TextAsset
        name= SpatialMappingRenderer ,  Type= UnityEngine.TextAsset
        name= HoloLensInput ,  Type= UnityEngine.TextAsset
        name= HoloLensInputModule ,  Type= UnityEngine.TextAsset
        name= Test ,  Type= UnityEngine.TextAsset //这是我们写的脚本
        name= TimeManager ,  Type= UnityEngine.Object
        name= DelayedCallManager ,  Type= UnityEngine.Object
        name= PhysicsManager ,  Type= UnityEngine.Object
        name= ResourceManager ,  Type= UnityEngine.Object
        name= NetworkManager ,  Type= UnityEngine.Object
        name= MasterServerInterface ,  Type= UnityEngine.Object
        name= NavMeshProjectSettings ,  Type= UnityEngine.Object
        name= Physics2DSettings ,  Type= UnityEngine.Object
        name= ClusterInputManager ,  Type= UnityEngine.Object
        name= CloudWebServicesManager ,  Type= UnityEngine.Object
        name= UnityAnalyticsManager ,  Type= UnityEngine.Object
        name= UnityConnectSettings ,  Type= UnityEngine.Object
        name= PerformanceReportingManager ,  Type= UnityEngine.Object
        name=  ,  Type= UnityEngine.Object
        name=  ,  Type= UnityEngine.Object
        name= Default-Skybox ,  Type= UnityEngine.Material
        name=  ,  Type= UnityEngine.Cubemap
        name=  ,  Type= UnityEngine.LightProbes
        name= GameObject ,  Type= UnityEngine.GameObject
        name= GameObject ,  Type= UnityEngine.Transform
        name= RenderSettings ,  Type= UnityEngine.RenderSettings
        name= LightmapSettings ,  Type= UnityEngine.LightmapSettings
        name= GameObject ,  Type= Test

Test.cs 代码本身就是一个 TextAsset,需要在启动时进行加载。 obj1 是场景里面的 GameObject 的名字,它所挂载的 Transform 组件也需要被加载。

注意:
即使代码脚本没有被使用,它也会在冷启动时加载,所以,如果一个大型项目里面有很多的代码文件,那么这些代码文件都需要被加载,这将会影响游戏的冷启动时间。所以,如果有很多的代码文件,可以考虑把这些代码文件打包成一个 .dll 文件,这样可以减少碎片化文件的加载时间,从而减少冷启动时间,加快游戏启动速度。

Unity游戏启动流程包括加载默认资源和执行各个模块的操作。首先,在启动时Unity加载一些默认的资源,包括Texture、Shader、Mesh等等。接下来,根据给出的代码示例,游戏启动流程可以分为以下几个步骤: 1. 初始化:在初始化阶段,游戏会创建并初始化一些模块,如资源包解压模块(UnPackAssetBundle)和热更新模块(HotUpdate)。这些模块会接收一些回调函数用于显示登录进度和在流程结束时执行特定操作。 2. 注册流程:在注册流程阶段,将各个模块连接起来,以确保它们按照正确的顺序执行。在给出的示例中,资源包解压模块(UnPackAssetBundle)和热更新模块(HotUpdate)通过注册流程的方式进行连接。 3. 启动流程:在启动流程阶段,调用资源包解压模块(UnPackAssetBundle)的DoStart方法,该方法会触发资源包解压模块的开始操作。 以上是Unity游戏启动流程的一般概述,具体的流程可能会根据具体游戏的需求而有所不同。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity游戏启动时了些啥](https://blog.csdn.net/a958832776/article/details/80105563)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Unity游戏开发-游戏热更新以及登录流程](https://blog.csdn.net/FlyToCloud/article/details/115075246)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值