Unity AB包问题总结

Unity版本 2019.4.9f1
1、ab包加载安卓端StreamingAssets中的文件,出现打包的文本(json文本)无法加载的问题,其他资源(预制体,音乐,图片)可正常加载
使用的路径为Application.streamingAssetsPath + “/” +相应的包路径
Unity AB包问题总结_第1张图片
安卓手机上显示的日志如上图
解决方式:暂时使用Resources文件夹下的文本文件

2、在编辑器中加载AB包资源出现材质丢失,贴图变为紫色等问题
解决方式:再资源加载完毕后,重新赋上材质

	private List<Material> thisMaterial;
    private List<string> shaders;

    private void Awake()
    {
        ReallyFixShader();
    }
    public void ReallyFixShader()
    {
#if UNITY_EDITOR
        //Debug.Log("Unity Editor");

        thisMaterial = new List<Material>(6);
        shaders = new List<string>(6);


        MeshRenderer[] meshRenderer = GetComponentsInChildren<MeshRenderer>();
        int length = meshRenderer.Length;
        for (int i = 0; i < length; i++)
        {
            int count = meshRenderer[i].materials.Length;
            for (int j = 0; j < count; j++)
            {
                Material _mater = meshRenderer[i].materials[j];
                thisMaterial.Add(_mater);
                shaders.Add(_mater.shader.name);
            }
        }
        thisMaterial.Clear();

        SkinnedMeshRenderer[] meshSkinRenderer = GetComponentsInChildren<SkinnedMeshRenderer>();
        length = meshSkinRenderer.Length;
        for (int i = 0; i < length; i++)
        {
            int count = meshSkinRenderer[i].materials.Length;
            for (int j = 0; j < count; j++)
            {
                Material _mater = meshSkinRenderer[i].materials[j];
                thisMaterial.Add(_mater);
                shaders.Add(_mater.shader.name);
            }
        }
        thisMaterial.Clear();

        Image[] images = GetComponentsInChildren<Image>();
        length = images.Length;
        for (int i = 0; i < length; i++)
        {
            var m = images[i].material;
            thisMaterial.Add(m);
            shaders.Add(m.shader.name);
        }
        for (int i = 0; i < thisMaterial.Count; i++)
        {
            thisMaterial[i].shader = Shader.Find(shaders[i]);
        }
        thisMaterial.Clear();


        SpriteRenderer[] srs = GetComponentsInChildren<SpriteRenderer>();
        length = srs.Length;

        for (int i = 0; i < length; i++)
        {
            //var m = srs[i].material;
            var m = srs[i].sharedMaterial;
            thisMaterial.Add(m);
            shaders.Add(m.shader.name);
        }
        for (int i = 0; i < thisMaterial.Count; i++)
        {
            thisMaterial[i].shader = Shader.Find(shaders[i]);
        }
        thisMaterial.Clear();


        Renderer[] rs = GetComponentsInChildren<Renderer>();
        length = rs.Length;
        for (int i = 0; i < length; i++)
        {
            //var m = rs[i].material;
            //Debug.Log("材质"+i+"长度"+length);
            var m = rs[i].sharedMaterial;
            thisMaterial.Add(m);
            shaders.Add(m.shader.name);
        }
        for (int i = 0; i < thisMaterial.Count; i++)
        {
            thisMaterial[i].shader = Shader.Find(shaders[i]);
        }
        thisMaterial.Clear();
#endif

    }

3、在这里插入图片描述
错误提示:The Animator Controller (AtkReadyIcon) you have used is not valid. Animations will not play
原因:在编辑器失去焦点后加载AB包中的Animator
解决方法:
在Editor->Preference->General->Script Changes While Playing 修改为Recompile After Finished Playing
(注意是Finished

你可能感兴趣的:(unity)