Unity3D中实现控制Update方法中帧率执行方法(降低Update方法中代码执行频率)节约资源

一、比如我要实现在Update方法中每5帧执行一次、或每2帧执行一次
①搭建场景

②编写脚本Test_UpdateMethod
/***
*	Title:"测试" 项目
*		主题:实现在Update方法里降低执行效率
*	Description:
*		功能:XXX
*	Date:2017
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Test
{
	public class Test_UpdateMethod : MonoBehaviour
	{
        public GameObject cube;                                                 //控制移动的物体
        public GameObject cube1;
        public float moveSpeed = 0.5F;                                          //移动速度



		void Start()
		{
			
		}

        private void Update()
        {

            if (Time.frameCount % 5 == 0)
            {
                print("每5帧执行一次");
                //cube.transform.localPosition=;
                cube1.transform.Translate(cube1.transform.right * Time.deltaTime);
            }


            if (Time.frameCount % 2 == 0)
            {
                print("每2帧执行一次");
                //cube.transform.localPosition=;
                cube.transform.localPosition = Vector3.MoveTowards(cube.transform.localPosition, new Vector3(2, 0.5F, 3), Time.deltaTime);
            }


            print("每1帧都执行");


        }
    }
}

③将脚本挂载到场景的_ScriptsMgr物体上,然后给该脚本指定对应的物体
④运行场景

猜你喜欢

转载自blog.csdn.net/xiaochenXIHUA/article/details/80204453
今日推荐