前面几个月公司,项目进行大版本更新,事比较多,就耽搁了,最近闲下来了就开始整理下,之前写的东西。
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 文件,这样可以减少碎片化文件的加载时间,从而减少冷启动时间,加快游戏启动速度。