Unity性能优化

首先声明,以下内容是我看的某老师的视频后整理下来的,记录下来以备用
性能优化从两个方面来,首先静态优化,针对于静态物体,可以打勾static,引擎会自动优化内存。还有就是关于贴图方面,针对于移动端,对于不同的平台,贴图的压缩能够有效的提供帧率,达到性能的优化。Texture Type优先选择Advanced,对于压缩方式,苹果采用PVRTC,安卓采用ETC。

 下面说一下动态优化,对于相同的物体(同材质骨骼模型等等)可以共用一个SkinnedMeshRenderer。下面贴出源码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CombineOpMesh : MonoBehaviour
{

    void Start()
    {
        CombineToMesh(this.gameObject);
    }

    public void CombineToMesh(GameObject go)
    {
        SkinnedMeshRenderer[] smr = go.GetComponentsInChildren<SkinnedMeshRenderer>();

        //Struct used to describe meshes to be combined (mesh, submeshindex, transform)
        List<CombineInstance> lcbi = new List<CombineInstance>();

        List<Material> lm = new List<Material>();
        List<Transform> lt = new List<Transform>();

        for (int i = 0; i < smr.Length; i++)
        {
            lm.AddRange(smr[i].materials);
            lt.AddRange(smr[i].bones);

            for (int sub = 0; sub < smr[i].sharedMesh.subMeshCount; sub++)
            {
                CombineInstance cbi = new CombineInstance();
                cbi.mesh = smr[i].sharedMesh;
                cbi.subMeshIndex = sub;
                lcbi.Add(cbi);
            }
            Destroy(smr[i].gameObject);
        }

        SkinnedMeshRenderer _smr = go.GetComponent<SkinnedMeshRenderer>();
        if (_smr == null)
        {
            _smr = go.AddComponent<SkinnedMeshRenderer>();
        }
     
            _smr.sharedMesh = new Mesh();
            _smr.bones = lt.ToArray();
            _smr.materials = new Material[] { lm[0] };
            _smr.rootBone = go.transform;
            _smr.sharedMesh.CombineMeshes(lcbi.ToArray(),true,false);
        
    }

}

下面是没添加脚本和添加了脚本之后的对比图:

Unity性能优化_第1张图片



能够很明显得看到变化。就先写这么多吧~~~

你可能感兴趣的:(unity,性能优化)