예제 #1
0
    protected void BindMaterialDepend(List<MaterialAsset> _materialAssetList)
    {
        foreach (var materialAsset in _materialAssetList)
        {
            string matName = materialAsset.matName.ToLower();
            if (!LoadCache.HasLoaded(matName, AssetType.material))
            {
                Debug.Log(matName + "找不到");
                continue;
            }
            MaterialRelationship rl = materialAsset.materialRelationship;

            Material mat = LoadCache.GetMaterial(matName);//materialAssetBundleRefDic[materialAsset.matName].mainAsset as Material;
            if (rl != null && mat.name.ToLower().Equals(rl.matName.ToLower()))
            {
                mat.shader = Shader.Find(rl.sdName);//GetShader(rl.sdName);//shaderAssetBundleRefDic[rl.sdName].mainAsset as Shader;
                foreach (var item in rl.propertyPairList)
                {
                    ShaderPropertyType type = (ShaderPropertyType)item.type;
                    switch (type)
                    {
                        case ShaderPropertyType.Color:
                            mat.SetColor(item.propertyName, item.color);
                            break;
                        case ShaderPropertyType.Vector:
                            mat.SetVector(item.propertyName, item.v4);
                            break;
                        case ShaderPropertyType.Float:
                        case ShaderPropertyType.Range:
                            mat.SetFloat(item.propertyName, item.value);
                            break;
                        case ShaderPropertyType.TexEnv:
                            if (LoadCache.HasLoaded(item.texName, AssetType.texture))
                            {
                                mat.SetTexture(item.propertyName, LoadCache.GetTexture(item.texName));//textureAssetBundleRefDic[item.texName].mainAsset as Texture);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
예제 #2
0
 /// <summary>
 /// 根据关联性配置组装物体
 /// </summary>
 /// <param name="_prefab"></param>
 /// <param name="_relationship"></param>
 public void ProcessGameObjectRelation(GameObject _prefab, GameObjectRelationship _relationship)
 {
     if (_prefab == null || _relationship == null) return;//如果物体为空或关联性配置为空,返回
     if (_relationship.matNames.Count > 0 && _prefab.GetComponent<Renderer>() != null)//如果关联性配置中数据为0或者该物体没有渲染组件,返回
     {
         int count = _relationship.matNames.Count;
         Material[] matArray = _prefab.GetComponent<Renderer>().sharedMaterials;
         for (int i = 0; i < count; i++)//遍历关联性配置数据
         {
             if (i >= matArray.Length)
             {
                 Debug.LogError(_prefab.name + "的配置文件记录的材质数量与实际材质数量不符合,请美术检查资源!");
                 break;
             }
             if (LoadCache.HasLoaded(_relationship.matNames[i], AssetType.material))//如果材质资源缓存中有关联性配置中需求的材质
             {
                 matArray[i] = LoadCache.GetMaterial(_relationship.matNames[i]);//materialAssetBundleRefDic[_relationship.matNames[i]].mainAsset as Material;
                 if (matArray[i] != null && matArray[i].shader != null)
                 {
                     Shader temp = Shader.Find(matArray[i].shader.name);
                     if (temp != null)
                         matArray[i].shader = temp;
                 }
             }
             else
             {
                 Debug.LogError(_prefab.name + " , can't find material that names " + _relationship.matNames[i] + " , please check it ! " + LoadCache.materialRefDic.Count);
             }
         }
         _prefab.GetComponent<Renderer>().sharedMaterials = matArray;
         //_prefab.SetActive(true);
     }
     //对子物体进行递归
     if (_prefab.transform.childCount > 0 && _relationship.childRelationList.Count > 0)
     {
         int fixedCount = Mathf.Min(_prefab.transform.childCount, _relationship.childRelationList.Count);
         for (int i = 0; i < fixedCount; i++)
         {
             ProcessGameObjectRelation(_prefab.transform.GetChild(i).gameObject, _relationship.childRelationList[i]);
         }
     }
 }