AddMaterials(SsPartRes part, SsColorBlendOperation colorBlendType) { SsImageFile img = part.imageFile; SsShaderType shaderType = SsShaderManager.EnumToType(colorBlendType, part.AlphaBlendType, SsMaterialColorBlendOperation.Non); Material material = img.GetMaterial(shaderType); if (material) { return; // already added } // create material #if _BUILD_UNIFIED_SHADERS img.useUnifiedShader = _anmRes.UseCgProgram; Shader shader = SsShaderManager.Get(shaderType, _anmRes.UseCgProgram); #else Shader shader = SsShaderManager.Get(shaderType, false); #endif // get material asset path string assetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(img.texture)) + "/assets/"; string shaderName = shader.name; shaderName = shaderName.Replace("Ss/", ""); string assetName = assetPath + img.texture.name + "_Mat_" + shaderName + ".asset"; // try to load the exiting Material existedMat = (Material)AssetDatabase.LoadAssetAtPath(assetName, typeof(Material)); Material mat = (existedMat ?? new Material(shader)); mat.SetTexture("_MainTex", img.texture); if (existedMat == null) { // if none, create material as asset file newly // Debug.Log("create material: " + img.path + " shader: " + shaderType); if (!Directory.Exists(assetPath)) { string parentFolder = Path.GetDirectoryName(assetPath.TrimEnd('\\', '/')); AssetDatabase.CreateFolder(parentFolder, "assets"); } AssetDatabase.CreateAsset(mat, assetName); } else { // update the existing content mat.shader = shader; EditorUtility.SetDirty(mat); AssetDatabase.SaveAssets(); //same as EditorApplication.SaveAssets(); } // add to material list img.materials[SsShaderManager.ToSerial(shaderType)] = mat; }
public Material GetMaterial(SsShaderType t) { if (materials == null) { return(null); } int index = SsShaderManager.ToSerial(t); if (index >= materials.Length) { return(null); } return(materials[index]); }