Example #1
0
    public static List <ShaderUsage> GetShaderUsages(out int totalGameObjects)
    {
        totalGameObjects = 0;
        List <ShaderUsage> usages = new List <ShaderUsage>();

        string[] guids = AssetDatabase.FindAssets("t: GameObject");
        for (int i = 0; i < guids.Length; i++)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
            var    importer  = AssetImporter.GetAtPath(assetPath);
            if (importer == null || !(importer is ModelImporter))
            {
                continue;
            }
            ModelImporter modelImporter = importer as ModelImporter;
            foreach (var kvp in modelImporter.GetExternalObjectMap())
            {
                if (kvp.Value is Material)
                {
                    Material mat        = (kvp.Value as Material);
                    string   shaderName = mat.shader.name;
                    if (shaderName.Contains("Manual") && !shaderName.EndsWith("Baked") && !shaderName.Contains("ManualSmall"))
                    {
                        usages.Add(new ShaderUsage(assetPath, shaderName, mat.name));
                    }
                }
            }
            totalGameObjects++;
        }
        return(usages);
    }
Example #2
0
    public static void Remove()
    {
        ModKitHelper.ClearUnityConsole();

        int count = 0;
        int total = 0;

        string[] guids = AssetDatabase.FindAssets("t: GameObject");
        for (int i = 0; i < guids.Length; i++)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
            var    importer  = AssetImporter.GetAtPath(assetPath);
            if (importer == null || !(importer is ModelImporter))
            {
                continue;
            }
            ModelImporter modelImporter = importer as ModelImporter;
            List <AssetImporter.SourceAssetIdentifier> remapsToRemove = new List <AssetImporter.SourceAssetIdentifier>();
            foreach (var kvp in modelImporter.GetExternalObjectMap())
            {
                if (kvp.Value is Material)
                {
                    Material mat        = (kvp.Value as Material);
                    string   shaderName = mat.shader.name;
                    if (shaderName.Contains("Manual") && !shaderName.EndsWith("Baked") && !shaderName.Contains("ManualSmall"))
                    {
                        MonoBehaviour.print("Invalid Shader used by " + assetPath + ": " + shaderName + " for material " + mat.name);
                        remapsToRemove.Add(kvp.Key);
                        count++;
                    }
                }
            }
            foreach (var remap in remapsToRemove)
            {
                modelImporter.RemoveRemap(remap);
                AssetDatabase.WriteImportSettingsIfDirty(assetPath);
                AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
            }
            total++;
        }

        MonoBehaviour.print(count + " out of " + total + " GameObjects used the manual shader.");
    }
Example #3
0
        /// <summary>
        /// Clear all externally remapped assets
        /// </summary>
        /// <remarks>
        /// After extracting textures and materials the model importer may maintain a remapping.
        /// The model will attempt to reference these assets during reimport, even if they
        /// have been deleted.
        /// ClearRemappedAssets will remove all map entries for assets that do not exist.
        /// </remarks>
        /// <param name="force">Remove remap even if external assets exist</param>
        public static void ClearRemappedAssets(ModelImporter modelImporter, bool force = false)
        {
            var externalObjectMap = modelImporter.GetExternalObjectMap();

            foreach (var map in externalObjectMap)
            {
                var identifier = map.Key;
                var exists     = false;
                // PROBLEM: path & guid will be defined even if asset no longer exists.
                // SOLUTION: Use filesystem to check asset exists.
                var path = AssetDatabase.GetAssetPath(map.Value);
                if (!force && path != null && path.StartsWith("Assets/"))
                {
                    path   = Application.dataPath + "/" + path.Substring("Assets/".Length);
                    exists = File.Exists(path);
                }
                if (!exists)
                {
                    modelImporter.RemoveRemap(identifier);
                }
            }
        }
Example #4
0
        public override void InvokeDeleteAction(string assetPath)
        {
            if (Path.GetExtension(assetPath) != ".mat")
            {
                return;
            }

            string materialName = Path.GetFileNameWithoutExtension(assetPath).ToLower();

            foreach (string modelAssetGUID in AssetDatabase.FindAssets("t:model"))
            {
                string        modelAssetPath = AssetDatabase.GUIDToAssetPath(modelAssetGUID);
                ModelImporter modelImporter  = (ModelImporter)AssetImporter.GetAtPath(modelAssetPath);

                // Check for any meshes that were referencing this material.
                Dictionary <AssetImporter.SourceAssetIdentifier, UnityEngine.Object> remappedMaterials = modelImporter.GetExternalObjectMap();
                foreach (KeyValuePair <AssetImporter.SourceAssetIdentifier, UnityEngine.Object> entry in remappedMaterials)
                {
                    if (entry.Key.name.ToLower() == materialName)
                    {
                        AssetDatabase.ImportAsset(modelAssetPath);
                    }
                }
            }
        }