示例#1
0
    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;
    }
示例#2
0
    public Material         GetMaterial(SsShaderType t)
    {
        if (materials == null)
        {
            return(null);
        }
        int index = SsShaderManager.ToSerial(t);

        if (index >= materials.Length)
        {
            return(null);
        }
        return(materials[index]);
    }
示例#3
0
    IndividualizeMaterial(bool mulMatColor)
    {
        if (_material == null)
        {
            return(null);
        }
#if true
        // do nothing if individualized already
        if (_individualMaterial != null)
        {
            return(_individualMaterial);
        }
#else
        // delete old before
        if (_individualMaterial != null)
        {
            //GameObject.DestroyImmediate(_individualMaterial, false);
            GameObject.Destroy(_individualMaterial);
        }
#endif
        // instantiate the original material assigned statically then replace to it
        _individualMaterial = (Material)UnityEngine.Object.Instantiate(_material);
        // save original material
        if (_originalMaterial == null)
        {
            _originalMaterial = _material;
        }
        _material = _individualMaterial;
        if (mulMatColor)
        {
            // replace shader blends material color
            var shaderType = SsShaderManager.EnumToType(_colorBlendType, _res.AlphaBlendType, SsMaterialColorBlendOperation.Mul);
            _material.shader = SsShaderManager.Get(shaderType, false);
        }
        _mgr._materials[_subMeshIndex] = _material;
        // to update
        _mgr._matChanged = true;
        return(_material);
    }