Unity资源引用问题

Prefab上面会有很多多余的引用记录在文件上,打包的时候获取依赖会一起打包进去,可以通过EditorUtility.SetDirty(Object)这个接口去刷

ParticleSystemRender的话会有另外的问题,如果使用过Mesh Mode的话,他会保存模型引用,也是非常让人费解。。需要如下清除:

public static bool CheckParticleSystemRenderer(GameObject gameObj)

        {

            bool isChange = false;

            ParticleSystemRenderer[] renders = gameObj.GetComponentsInChildren<ParticleSystemRenderer>(true);

            foreach (var renderItem in renders)

            {

                if (renderItem.renderMode != ParticleSystemRenderMode.Mesh)

                {

                    renderItem.mesh = null;

                    isChange = true;

                }

            }

            return isChange;

        }


材质球的话,也会有非常多的乱七八糟引用。setdirty也是不能解决,只能直接改序列化文件了

public static void CleanMaterial(Material mat)

    {

        if (mat && mat.shader != null)

        {

            SerializedObject psSource = new SerializedObject(mat);

            SerializedProperty emissionProperty = psSource.FindProperty("m_SavedProperties");

            SerializedProperty texEnvs = emissionProperty.FindPropertyRelative("m_TexEnvs");

            SerializedProperty floats = emissionProperty.FindPropertyRelative("m_Floats");

            SerializedProperty colos = emissionProperty.FindPropertyRelative("m_Colors");

            bool rebuild1 = CleanMaterialSerializedProperty(texEnvs, mat);

            bool rebuild2 = CleanMaterialSerializedProperty(floats, mat);

            bool rebuild3 = CleanMaterialSerializedProperty(colos, mat);

            if (rebuild1 || rebuild2 || rebuild3)

            {

                psSource.ApplyModifiedProperties();

                EditorUtility.SetDirty(mat);

            }

        }

    }

private static bool CleanMaterialSerializedProperty(SerializedProperty property, Material mat)

    {

        bool rebuild = false;

        for (int j = property.arraySize - 1; j >= 0; j--)

        {

            string propertyName = property.GetArrayElementAtIndex(j).displayName;

            if (!mat.HasProperty(propertyName))

            {

                property.DeleteArrayElementAtIndex(j);

                rebuild = true;

            }

        }

        return rebuild;

    }

FBX材质球导入的时候,也会存在历史记录的问题,但是这个一般比较难以发现,比如下图中,之前美术导入的FBX材质球并不是如下命名,这样在meta文件中就会残留之前的材质球命名,如果材质球刚好还引用了不正确的材质球被保存下来。那样打包跟加载的时候就会加载多余的材质球。下图的svn就是清理前后的对比。最后老规矩,贴上代码。


public static void CleanFbxDependenciesMatiral(string assetPath)

        {

            GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);

            Renderer[] renderList = obj.GetComponentsInChildren<Renderer>();

            List<Material> materialList = new List<Material>();

            foreach (Renderer render in renderList)

            {

                materialList.AddRange(render.sharedMaterials);

            }

            ModelImporter mi = AssetImporter.GetAtPath(assetPath) as ModelImporter;

            SerializedObject psSource = new SerializedObject(mi);

            SerializedProperty property = psSource.FindProperty("m_ExternalObjects");

            if (property == null)

                return;

            for (int j = property.arraySize - 1; j >= 0; j--)

            {

                SerializedProperty typeStr = property.GetArrayElementAtIndex(j).FindPropertyRelative("first").FindPropertyRelative("type");

                if (typeStr.stringValue != "UnityEngine:Material")

                {

                    continue;

                }

                SerializedProperty firstProperty = property.GetArrayElementAtIndex(j).FindPropertyRelative("second");

                if (firstProperty.objectReferenceValue == null)

                {

                    property.DeleteArrayElementAtIndex(j);

                }

                else if (firstProperty.objectReferenceValue is Material)

                {

                    Material m = firstProperty.objectReferenceValue as Material;

                    if (!materialList.Contains(m))

                        property.DeleteArrayElementAtIndex(j);

                    //Debug.LogFormat("{0},{1},{2}", firstProperty.objectReferenceInstanceIDValue, firstProperty.objectReferenceValue, materialList.Contains(m));

                }

            }

            if (psSource.hasModifiedProperties)

            {

                psSource.ApplyModifiedProperties();

                Debug.Log("hasModifiedProperties:" + assetPath);

                AssetDatabase.WriteImportSettingsIfDirty(assetPath);

            }

        }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,963评论 0 2
  • ## 可重入函数 ### 可重入性的理解 若一个程序或子程序可以安全的被并行执行,则称其为可重入的;即当该子程序正...
    夏至亦韵阅读 747评论 0 0
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,296评论 0 16
  • 一. Java基础部分.................................................
    wy_sure阅读 3,854评论 0 11
  • 自我感觉最糟糕的工作状态,肯定要随心走,到一个让自己开心的地方
    DrCYW阅读 170评论 0 0