コード例 #1
0
        public void UpdateShaderOnPropertyNodes(ref Shader shader)
        {
            if (m_propertyNodes.Count == 0)
            {
                return;
            }

            try
            {
                bool hasContents = false;
                //string metaNewcontents = IOUtils.LINE_TERMINATOR.ToString();
                TextureDefaultsDataColector defaultCol = new TextureDefaultsDataColector();
                foreach (KeyValuePair <int, PropertyNode> kvp in m_propertyNodes)
                {
                    hasContents = kvp.Value.UpdateShaderDefaults(ref shader, ref defaultCol) || hasContents;
                }

                if (hasContents)
                {
                    ShaderImporter importer = ( ShaderImporter )ShaderImporter.GetAtPath(AssetDatabase.GetAssetPath(shader));
                    importer.SetDefaultTextures(defaultCol.NamesArr, defaultCol.ValuesArr);
                    importer.SaveAndReimport();

                    defaultCol.Destroy();
                    defaultCol = null;
                    //string metaFilepath = AssetDatabase.GetTextMetaFilePathFromAssetPath( AssetDatabase.GetAssetPath( shader ) );
                    //string metaContents = IOUtils.LoadTextFileFromDisk( metaFilepath );

                    //int startIndex = metaContents.IndexOf( IOUtils.MetaBegin );
                    //int endIndex = metaContents.IndexOf( IOUtils.MetaEnd );

                    //if ( startIndex > 0 && endIndex > 0 )
                    //{
                    //	startIndex += IOUtils.MetaBegin.Length;
                    //	string replace = metaContents.Substring( startIndex, ( endIndex - startIndex ) );
                    //	if ( hasContents )
                    //	{
                    //		metaContents = metaContents.Replace( replace, metaNewcontents );
                    //	}
                    //}
                    //IOUtils.SaveTextfileToDisk( metaContents, metaFilepath, false );
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
        }
コード例 #2
0
    // Create a material instance of a given shader and configure textures and such
    void CreateMaterial(Shader shader)
    {
        string shaderPath       = AssetDatabase.GetAssetPath(shader);
        string shaderDirectory  = Path.GetDirectoryName(shaderPath);
        string shaderParentPath = Path.GetDirectoryName(Path.GetDirectoryName(shaderPath));
        string shaderName       = shader.name.Substring("Shade/".Length);
        string materialPath     = Path.Combine(shaderParentPath, shaderName);

        Material material = AssetDatabase.LoadAssetAtPath <Material>(materialPath + ".mat");

        if (material == null)
        {
            material = new Material(shader);
            AssetDatabase.CreateAsset(material, materialPath + ".mat");
        }

        material.shader = shader;

        // Find the Graph.json file for this shader, find any Texture nodes and properly configure the associated material properties for them
        TextAsset      shaderGraphAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(Path.Combine(shaderDirectory, "Graph.json"));
        ShaderGraph    graph            = JsonUtility.FromJson <ShaderGraph>(shaderGraphAsset.text);
        ShaderImporter shaderImporter   = ShaderImporter.GetAtPath(shaderPath) as ShaderImporter;

        string[] textureTypes =
        {
            "Texture",
            "Gradient",
            "Bake",
            "Tiler"
        };

        foreach (ShaderNode n in graph.nodes)
        {
            if (n.options.userLabel != null)
            {
                string lowercaseName = Char.ToLowerInvariant(n.options.userLabel[0]) + n.options.userLabel.Substring(1);
                string propertyName  = "_" + lowercaseName.Replace(" ", "");

                if (Array.Exists(textureTypes, element => element == n.name))
                {
                    string    textureName = n.options.value != null ? n.options.value : n.options.userLabel;
                    Texture2D tex         = AssetDatabase.LoadAssetAtPath <Texture2D>(Path.Combine(shaderDirectory, textureName + ".png"));
                    if (tex != null)
                    {
                        material.SetTexture(propertyName, tex);
                        shaderImporter.SetDefaultTextures(new[] { propertyName }, new[] { tex });

                        string          texturePath = AssetDatabase.GetAssetPath(tex);
                        TextureImporter importer    = (TextureImporter)AssetImporter.GetAtPath(texturePath);

                        if (n.options.wrapMode != null)
                        {
                            switch (n.options.wrapMode)
                            {
                            case "repeat":
                                importer.wrapMode = TextureWrapMode.Repeat;
                                break;

                            case "clamp":
                                importer.wrapMode = TextureWrapMode.Clamp;
                                break;

                            case "mirror":
                                importer.wrapMode = TextureWrapMode.Mirror;
                                break;
                            }
                        }
                        else
                        {
                            importer.wrapMode = TextureWrapMode.Clamp;
                        }

                        if (n.options.filterMode != null)
                        {
                            switch (n.options.filterMode)
                            {
                            case "point":
                                importer.filterMode = FilterMode.Point;
                                break;

                            case "linear":
                                importer.filterMode = n.options.generateMipmaps ? FilterMode.Trilinear : FilterMode.Bilinear;
                                break;
                            }
                        }

                        importer.textureType = n.options.isNormalMap ? TextureImporterType.NormalMap : TextureImporterType.Default;

                        importer.SaveAndReimport();
                    }
                }
            }
        }
    }