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

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

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

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

1、 Unity3D内部的内存

2、 Mono的托管内存

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

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

  • 资源:纹理、网格、音频等等
  • GameObject和各种组件。
  • 引擎内部逻辑需要的内存:渲染器,物理系统,粒子系统等等
   我们接下来通过代码执行的时间方式给读者介绍一下关于组件的使用效率问题,代码如下所示:
using UnityEngine;

public class CachedMB : MonoBehaviour
{

	Transform _transform;
	public Transform transform
	{
		get { return _transform ?? (_transform = base.transform); }
	}

	//for testing
	public Transform uncachedTransform
	{
		get { return base.transform; }
	}

	Rigidbody _rigidbody;
	public Rigidbody rigidbody
	{
		get { return _rigidbody ?? (_rigidbody = base.GetComponent<Rigidbody>()); }
	}

	Camera _camera;
	public Camera camera
	{
		get { return _camera ?? (_camera = base.GetComponent<Camera>()); }
	}

	Light _light;
	public Light light
	{
		get { return _light ?? (_light = base.GetComponent<Light>()); }
	}

	private Animation _animation;
	public Animation animation
	{
		get { return _animation ?? (_animation = base.GetComponent<Animation>()); }
	}

	private ConstantForce _constantForce;
	public ConstantForce constantForce
	{
		get { return _constantForce ?? (_constantForce = base.GetComponent<ConstantForce>()); }
	}

	private Renderer _renderer;
	public Renderer renderer
	{
		get { return _renderer ?? (_renderer = base.GetComponent<Renderer>()); }
	}

	private AudioSource _audio;
	public AudioSource audio
	{
		get { return _audio ?? (_audio = base.GetComponent<AudioSource>()); }
	}

	private GUIText _guiText;
	public GUIText guiText
	{
		get { return _guiText ?? (_guiText = base.GetComponent<GUIText>()); }
	}

	private GUITexture _guiTexture;
	public GUITexture guiTexture
	{
		get { return _guiTexture ?? (_guiTexture = base.GetComponent<GUITexture>()); }
	}

	private NetworkView _networkView;
	public NetworkView networkView
	{
		get { return _networkView ?? (_networkView = base.GetComponent<NetworkView>()); }
	}

	private Collider _collider;
	public Collider collider
	{
		get { return _collider ?? (_collider = base.GetComponent<Collider>()); }
	}

	private HingeJoint _hingeJoint;
	public HingeJoint hingeJoint
	{
		get { return _hingeJoint ?? (_hingeJoint = base.GetComponent<HingeJoint>()); }
	}

	private ParticleEmitter _particleEmitter;
	public ParticleEmitter particleEmitter
	{
		get { return _particleEmitter ?? (_particleEmitter = base.GetComponent<ParticleEmitter>()); }
	}

	private ParticleSystem _particleSystem;
	public ParticleSystem particleSystem
	{
		get { return _particleSystem ?? (_particleSystem = base.GetComponent<ParticleSystem>()); }
	}

	private GameObject _gameObject;
	public GameObject gameObject
	{
		get { return _gameObject ?? (_gameObject = base.gameObject); }
	}

	private string _tag;
	public string tag
	{
		get { return _tag ?? (_tag = base.tag); }
		set { _tag = value; base.tag = value; }
	}

	private string _name;
	public string name
	{
		get { return _name ?? (_name = base.name); }
		set { _tag = value; base.tag = value; }
	}

}

接下来开始写测试代码案例:

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

public class CacheTest : CachedMB
{
	const int ITERATIONS = 1000000;
	// Use this for initialization

	Transform cached;

	IEnumerator Start()
	{
		cached = uncachedTransform;

		for (; ; )
		{
			yield return null;
			if (!Input.GetKeyDown(KeyCode.T)) continue;

			var sw1 = Stopwatch.StartNew();
			{
				Transform trans1;
				for (int i = 0; i < ITERATIONS; i++)
					trans1 = GetComponent<Transform>();
			} sw1.Stop();

			var sw2 = Stopwatch.StartNew();
			{
				Transform trans2;
				for (int i = 0; i < ITERATIONS; i++)
					trans2 = transform;
			} sw2.Stop();

			var sw3 = Stopwatch.StartNew();
			{
				Transform trans3;
				for (int i = 0; i < ITERATIONS; i++)
					trans3 = cached;
			} sw3.Stop();

			var sw4 = Stopwatch.StartNew();
			{
				Transform trans4;
				for (int i = 0; i < ITERATIONS; i++)
					trans4 = uncachedTransform;
			} sw4.Stop();

			UnityEngine.Debug.LogWarning(ITERATIONS + " iterations");
			UnityEngine.Debug.LogWarning("GetComponent " + sw1.ElapsedMilliseconds + "ms");
			UnityEngine.Debug.LogWarning("MonoBehaviour " + sw4.ElapsedMilliseconds + "ms");
			UnityEngine.Debug.LogWarning("CachedMB " + sw2.ElapsedMilliseconds + "ms");
			UnityEngine.Debug.LogWarning("Manual Cache " + sw3.ElapsedMilliseconds + "ms");
		}
	}
}

执行结果如下所示:



通过显示的结果,我们知道,GetComponent是比较耗费时间的,在游戏开发中尤其是在每帧调用中少用。Manual Cache是耗费时间最少的,应该在游戏开发中尽量多用。
  


Unity内存优化技术测试案例的更多相关文章

  1. Unity内存优化

    [Unity内存优化] 1.在Update方法或循环中,少用string类,因为string类的每次操作都会调用new生成新字符串对象.用StringBuilder代替string,StringBui ...

  2. Unity内存优化(贴图层面)

    聊聊近况: 距离上一篇文章已经过了好久,主要原因是我懒了.公司项目也到了开始优化的阶段,上网找的资料,看过了就忘.还是想把它整理一下,写出来.其实我说的东西,网上都有,我只是搬运工而已. 贴图压缩: ...

  3. Unity内存优化之视频讲解

    视频为中文讲解,mp4格式,大小3.05GB 目录   扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主

  4. Unity Shader入门精要学习笔记 - 第16章 Unity中的渲染优化技术

    转自冯乐乐的 <Unity Shader 入门精要> 移动平台的特点 为了尽可能一处那些隐藏的表面,减少overdraw(即一个像素被绘制多次),PowerVR芯片(通常用于ios设备和某 ...

  5. Unity教程之再谈Unity中的优化技术

    这是从 Unity教程之再谈Unity中的优化技术 这篇文章里提取出来的一部分,这篇文章让我学到了挺多可能我应该知道却还没知道的知识,写的挺好的 优化几何体   这一步主要是为了针对性能瓶颈中的”顶点 ...

  6. 【Unity技巧】Unity中的优化技术

    http://blog.csdn.net/candycat1992/article/details/42127811 写在前面 这一篇是在Digital Tutors的一个系列教程的基础上总结扩展而得 ...

  7. Unity堆内存优化

    unity中减少堆内存分配以减少垃圾回收处理:只有局部变量且为值类值的变量是从stack栈中分配内存,其它所有情况都是从heap堆中分配内在.* 缓存获取到的数据.* 频繁被调用的函数中尽量少的分配空 ...

  8. Unity中的优化技术

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/candycat1992/article/ ...

  9. 【MDCC技术大咖秀】Android内存优化之OOM

    大神分析的很全面,所以就转过来保存一份,转自:http://www.csdn.net/article/2015-09-18/2825737/1 以下为正文: Android的内存优化是性能优化中很重要 ...

随机推荐

  1. pxe基于虚拟机的自启动

    环境系统:centos6.4 min版 虚拟机实现:提供的服务器ip为192.168.0.105,桥接 安装dhcp服务: yum -y install dhcp 配置dhcp服务,使能够成功启动: ...

  2. Live Score FAQ

    Q: Why doesn't the selected game go to top? A: The game which include your favorite team will be alw ...

  3. 什么是浮动IP

    源地址:https://www.1and1.com/digitalguide/server/know-how/what-is-a-floating-ip/ What is a floating IP? ...

  4. C# 删除文件以及文件夹

    代码如下: /// <summary> /// 删除文件夹以及文件 /// </summary> /// <param name="directoryPath& ...

  5. Python学习札记(十七) 高级特性3 列表生成式

    参考:列表生成式 Note 1.List Comprehensions,即列表生成式,是Python中内置的非常强大的list生成式. eg.生成一个列表:[1*1, 2*2, ..., 10*10] ...

  6. LA 5135 井下矿工(点—双连通分量模板题)

    https://vjudge.net/problem/UVALive-5135 题意:在一个无向图上选择尽量少的点涂黑,使得任意删除一个点后,每个连通分量至少有一个黑点. 思路: 首先dfs遍历求出割 ...

  7. Extjs前端框架解决了什么问题

    Extjs 作为一套企业级富客户端前端开发框架,主要解决了以下问题: 1.DOM Ext.Element: Ext.Element.get()快捷方式Ext.get(),只能以dom的id作为参数去获 ...

  8. maven--spring-boot-starter-parent 小结

    Maven中的dependency的scope作用域详解 https://blog.csdn.net/itchiang/article/details/45009057 https://blog.cs ...

  9. 前端打印功能实现及css设置

    首先是使用下边代码,实现js局部打印功能.参数dom为需要打印的节点,为了保证页面功能的单一性,最好弹出一个新的预览页面完成打印功能. function print(dom){ var body = ...

  10. Mercurial的使用心得

    本文发表地址:http://www.xiabingbao.com/mercurial/2015/01/22/mercurial-understanding/ 本人接触版本控制的历史 在很久很久以前,我 ...