protected void LoadTexture(string texPath, Action <Texture> exCallback = null)
    {
        TexturesWaitLoadCount++;
        var texLoader = KTextureLoader.Load(texPath, (isOk, tex) =>
        {
            if (!isOk)
            {
                Log.LogError("无法加载依赖图片: {0}", texPath);
            }

            if (exCallback != null)
            {
                exCallback(tex);
            }

            TexturesWaitLoadCount--;
            if (TexturesWaitLoadCount <= 0)
            {
                foreach (var c in TexturesLoadedCallback)
                {
                    c();
                }
                TexturesLoadedCallback.Clear();
            }
        });

        ResourceLoaders.Add(texLoader);
    }
Esempio n. 2
0
    // 加载材质的图片, 协程等待
    private IEnumerator CoGenerateMaterial(string matPath, KSerializeMaterial sMat)
    {
        // 纹理全部加载完成后到这里
        //if (!CachedMaterials.TryGetValue(matPath, out mat))
        {
            var shaderLoader = KShaderLoader.Load(sMat.ShaderPath);
            while (!shaderLoader.IsCompleted)
            {
                yield return(null);
            }

            var shader = shaderLoader.ShaderAsset;
            if (shader == null)
            {
                shader = KTool.FindShader(sMat.ShaderName);
                Logger.LogWarning("无法加载Shader资源: {0}, 使用Shaders.Find代替", sMat.ShaderName);
                if (shader == null)
                {
                    Logger.LogWarning("找不到Shader: {0}, 使用Diffuse临时代替", sMat.ShaderName);
                    shader = KTool.FindShader("Diffuse");
                }
            }
            Debuger.Assert(shader);

            Mat      = new Material(shader);
            Mat.name = sMat.MaterialName;

            //CachedMaterials[matPath] = mat;

            foreach (KSerializeMaterialProperty shaderProp in sMat.Props)
            {
                switch (shaderProp.Type)
                {
                case KSerializeMaterialProperty.ShaderType.Texture:
                    Vector2 tiling;
                    Vector2 offset;
                    var     texturePath = ParseMaterialStr(shaderProp.PropValue, out tiling, out offset);
                    if (TextureLoaders == null)
                    {
                        TextureLoaders = new List <KTextureLoader>();
                    }

                    var texLoader = KTextureLoader.Load(texturePath);
                    TextureLoaders.Add(texLoader);
                    while (!texLoader.IsCompleted)
                    {
                        yield return(null);
                    }

                    var tex = texLoader.Asset;
                    if (tex == null)
                    {
                        Logger.LogError("找不到纹理: {0}", texturePath);
                    }
                    else
                    {
                        _SetMatTex(Mat, shaderProp.PropName, tex, tiling, offset);
                    }
                    break;

                case KSerializeMaterialProperty.ShaderType.Color:
                    _SetMatColor(Mat, shaderProp.PropName, shaderProp.PropValue);
                    break;

                case KSerializeMaterialProperty.ShaderType.Range:
                    _SetMatRange(Mat, shaderProp.PropName, shaderProp.PropValue);
                    break;

                case KSerializeMaterialProperty.ShaderType.Vector:
                    _SetMatVector(Mat, shaderProp.PropName, shaderProp.PropValue);
                    break;

                case KSerializeMaterialProperty.ShaderType.RenderTexture:
                    // RenderTextures, 不处理, 一般用在水,Water脚本会自动生成
                    break;
                }
            }
        }
    }