性能优化:自定义Update层

我们知道尽量少在MonoBehaviour脚本的Update回调中处理逻辑,对游戏性能影响比较大。但是有些逻辑又必须写在Update中,比如游戏AI模块,或子系统更新等。更糟糕的是,如果又要求当前场景中有成千上万的NPC敌人在场时,如果每个NPC都有Update回调都执行的话,而且它们极有可能在同一帧触发,这会产生一个巨大的CPU使用峰值。Unity调用Update时,会经历本机-托管桥接,而这是非常耗性能的。换句话来讲,执行10000个Update()回调要比在执行1个Update回调但执行10000个“OnUpdate”函数成本要高很多。后者另外也为我们能更好地控制更新提供了操作空间。比如:如果发现即将达到当前帧的CPU预算时,可以把某些低优先级的任务暂停下来。
脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 更新接口
 */
public class IUpdate: MonoBehaviour{

	public virtual void OnUpdate() { }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 活动系统
 */
public class ActivitySystem : IUpdate {

    public override void OnUpdate() { }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 任务成就系统
 */
public class AcheiveSystem :IUpdate {

    public override void OnUpdate() { }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 游戏更新检测控制脚本
 */
public class Game : MonoBehaviour {

	private List<IUpdate> systemList = new List<IUpdate>();

	/// <summary>
	/// 注册
	/// </summary>
	/// <param name="updateSys"></param>
	public void RegisterSystem(IUpdate updateSys)
	{
		if (!systemList.Contains(updateSys))
			systemList.Add(updateSys);
	}

	/// <summary>
	/// 注销
	/// </summary>
	/// <param name="updateSys"></param>
	public void UnRegisterSystem(IUpdate updateSys)
	{
		if (systemList.Contains(updateSys))
			systemList.Remove(updateSys);
	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		for (int i = 0; i < systemList.Count; i++)
		{
			systemList[i].OnUpdate();
		}
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值