zoukankan      html  css  js  c++  java
  • Unity内存优化技术测试案例

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。

    CSDN视频网址:http://edu.csdn.net/lecturer/144

    Unity引擎优化技术,无非涉及到三点:CPU优化,GPU优化,内存优化。

    先谈谈内存优化:大概可以分成三大部分:

       1、 Unity3D内部的内存

       2、 Mono的托管内存

       3、 引入的DLL或者第三方DLL所需要的内存。

    其中unity3d内部的内存包括如下:

    • 资源:纹理、网格、音频等等
    • GameObject和各种组件。
    • 引擎内部逻辑需要的内存:渲染器,物理系统,粒子系统等等
       我们接下来通过代码执行的时间方式给读者介绍一下关于组件的使用效率问题,代码如下所示:
    [csharp] view plain copy
     
    1. using UnityEngine;  
    2.   
    3. public class CachedMB : MonoBehaviour  
    4. {  
    5.   
    6.   
    7.     Transform _transform;  
    8.     public Transform transform  
    9.     {  
    10.         get { return _transform ?? (_transform = base.transform); }  
    11.     }  
    12.   
    13.   
    14.     //for testing  
    15.     public Transform uncachedTransform  
    16.     {  
    17.         get { return base.transform; }  
    18.     }  
    19.   
    20.     Rigidbody _rigidbody;  
    21.     public Rigidbody rigidbody  
    22.     {  
    23.         get { return _rigidbody ?? (_rigidbody = base.GetComponent<Rigidbody>()); }  
    24.     }  
    25.   
    26.     Camera _camera;  
    27.     public Camera camera  
    28.     {  
    29.         get { return _camera ?? (_camera = base.GetComponent<Camera>()); }  
    30.     }  
    31.   
    32.     Light _light;  
    33.     public Light light  
    34.     {  
    35.         get { return _light ?? (_light = base.GetComponent<Light>()); }  
    36.     }  
    37.   
    38.     private Animation _animation;  
    39.     public Animation animation  
    40.     {  
    41.         get { return _animation ?? (_animation = base.GetComponent<Animation>()); }  
    42.     }  
    43.   
    44.   
    45.     private ConstantForce _constantForce;  
    46.     public ConstantForce constantForce  
    47.     {  
    48.         get { return _constantForce ?? (_constantForce = base.GetComponent<ConstantForce>()); }  
    49.     }  
    50.   
    51.     private Renderer _renderer;  
    52.     public Renderer renderer  
    53.     {  
    54.         get { return _renderer ?? (_renderer = base.GetComponent<Renderer>()); }  
    55.     }  
    56.   
    57.     private AudioSource _audio;  
    58.     public AudioSource audio  
    59.     {  
    60.         get { return _audio ?? (_audio = base.GetComponent<AudioSource>()); }  
    61.     }  
    62.   
    63.   
    64.     private GUIText _guiText;  
    65.     public GUIText guiText  
    66.     {  
    67.         get { return _guiText ?? (_guiText = base.GetComponent<GUIText>()); }  
    68.     }  
    69.   
    70.   
    71.     private GUITexture _guiTexture;  
    72.     public GUITexture guiTexture  
    73.     {  
    74.         get { return _guiTexture ?? (_guiTexture = base.GetComponent<GUITexture>()); }  
    75.     }  
    76.   
    77.   
    78.     private NetworkView _networkView;  
    79.     public NetworkView networkView  
    80.     {  
    81.         get { return _networkView ?? (_networkView = base.GetComponent<NetworkView>()); }  
    82.     }  
    83.   
    84.   
    85.     private Collider _collider;  
    86.     public Collider collider  
    87.     {  
    88.         get { return _collider ?? (_collider = base.GetComponent<Collider>()); }  
    89.     }  
    90.   
    91.   
    92.     private HingeJoint _hingeJoint;  
    93.     public HingeJoint hingeJoint  
    94.     {  
    95.         get { return _hingeJoint ?? (_hingeJoint = base.GetComponent<HingeJoint>()); }  
    96.     }  
    97.   
    98.   
    99.     private ParticleEmitter _particleEmitter;  
    100.     public ParticleEmitter particleEmitter  
    101.     {  
    102.         get { return _particleEmitter ?? (_particleEmitter = base.GetComponent<ParticleEmitter>()); }  
    103.     }  
    104.   
    105.   
    106.     private ParticleSystem _particleSystem;  
    107.     public ParticleSystem particleSystem  
    108.     {  
    109.         get { return _particleSystem ?? (_particleSystem = base.GetComponent<ParticleSystem>()); }  
    110.     }  
    111.   
    112.   
    113.     private GameObject _gameObject;  
    114.     public GameObject gameObject  
    115.     {  
    116.         get { return _gameObject ?? (_gameObject = base.gameObject); }  
    117.     }  
    118.   
    119.   
    120.     private string _tag;  
    121.     public string tag  
    122.     {  
    123.         get { return _tag ?? (_tag = base.tag); }  
    124.         set { _tag = value; base.tag = value; }  
    125.     }  
    126.   
    127.   
    128.     private string _name;  
    129.     public string name  
    130.     {  
    131.         get { return _name ?? (_name = base.name); }  
    132.         set { _tag = value; base.tag = value; }  
    133.     }  
    134.   
    135. }  
    接下来开始写测试代码案例:
    [csharp] view plain copy
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3. using System.Diagnostics;  
    4. using System.IO;  
    5. using System.Collections.Generic;  
    6.   
    7. public class CacheTest : CachedMB  
    8. {  
    9.     const int ITERATIONS = 1000000;  
    10.     // Use this for initialization  
    11.   
    12.     Transform cached;  
    13.   
    14.     IEnumerator Start()  
    15.     {  
    16.         cached = uncachedTransform;  
    17.   
    18.         for (; ; )  
    19.         {  
    20.             yield return null;  
    21.             if (!Input.GetKeyDown(KeyCode.T)) continue;  
    22.   
    23.             var sw1 = Stopwatch.StartNew();  
    24.             {  
    25.                 Transform trans1;  
    26.                 for (int i = 0; i < ITERATIONS; i++)  
    27.                     trans1 = GetComponent<Transform>();  
    28.             } sw1.Stop();  
    29.   
    30.   
    31.             var sw2 = Stopwatch.StartNew();  
    32.             {  
    33.                 Transform trans2;  
    34.                 for (int i = 0; i < ITERATIONS; i++)  
    35.                     trans2 = transform;  
    36.             } sw2.Stop();  
    37.   
    38.   
    39.             var sw3 = Stopwatch.StartNew();  
    40.             {  
    41.                 Transform trans3;  
    42.                 for (int i = 0; i < ITERATIONS; i++)  
    43.                     trans3 = cached;  
    44.             } sw3.Stop();  
    45.   
    46.             var sw4 = Stopwatch.StartNew();  
    47.             {  
    48.                 Transform trans4;  
    49.                 for (int i = 0; i < ITERATIONS; i++)  
    50.                     trans4 = uncachedTransform;  
    51.             } sw4.Stop();  
    52.   
    53.             UnityEngine.Debug.LogWarning(ITERATIONS + " iterations");  
    54.             UnityEngine.Debug.LogWarning("GetComponent " + sw1.ElapsedMilliseconds + "ms");  
    55.             UnityEngine.Debug.LogWarning("MonoBehaviour " + sw4.ElapsedMilliseconds + "ms");  
    56.             UnityEngine.Debug.LogWarning("CachedMB " + sw2.ElapsedMilliseconds + "ms");  
    57.             UnityEngine.Debug.LogWarning("Manual Cache " + sw3.ElapsedMilliseconds + "ms");  
    58.         }  
    59.     }  
    60. }  

    http://www.woaipu.com/shops/zuzhuan/61406
    http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117777
    http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117890
    http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117994
    http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=118376

  • 相关阅读:
    JAVA联调接口跨域解决办法
    Quartz集成springMVC 的方案二(持久化任务、集群和分布式)
    Quartz集成springMVC 的方案一
    Java 常调用的Webservice接口的方法
    集成Dubbo服务(Spring)
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    事务的隔离级别 数据库
    Spring 之注解事务 @Transactional
    Request To JavaBean(请求对象转换为JavaBean对象)
    springmvc+mongodb+maven 项目搭建配置
  • 原文地址:https://www.cnblogs.com/sy646et/p/7203115.html
Copyright © 2011-2022 走看看