Last active
January 8, 2022 10:04
-
-
Save ProGM/18111dfef2477407c1c1 to your computer and use it in GitHub Desktop.
A useful script to identify Unity resource that are not referenced in the project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
GREEN='\033[0;32m' | |
RED='\033[0;31m' | |
YELLOW='\033[0;33m' | |
NC='\033[0m' | |
rm unused_files.log &> /dev/null | |
grep -r . --include=\*.meta --exclude=*{Editor,Gizmos}* -e 'guid' | while read line; do | |
guuid=`echo $line | awk '{print $NF}'` | |
path=`echo $line | cut -f1 -d":"` | |
no_meta=`echo $path | sed "s/.meta$//"` | |
# Skip if is directory | |
if [[ -d $no_meta ]]; then | |
continue | |
fi | |
filename=$(basename "$path") | |
filename="${filename%.*}" | |
echo -e "${GREEN}Currently searching for $guuid in $filename${NC}" | |
grep -rn . --include=\*.{unity,anim,controller,prefab,mat} -e $guuid | |
if [[ $? != 0 ]]; then | |
echo $filename | grep .cs$ && is_script=$? || is_script=$? | |
echo $filename | grep .unity$ && is_map=$? || is_map=$? | |
if [[ $is_script == 0 ]]; then | |
echo -e "${YELLOW}FILE $path not used directly, searching...${NC}" | |
grep -rn . --include=\*.cs --exclude=$no_meta -e "${filename%.*}" | |
if [[ $? != 0 ]]; then | |
echo -e "${RED}FILE $path is unused!!!${NC}" | |
echo $path >> unused_files.log | |
fi | |
elif [[ $is_map == 0 ]]; then | |
echo -e "${YELLOW}FILE $path not used directly, searching...${NC}" | |
grep ProjectSettings/EditorBuildSettings.asset -e `echo $no_meta | sed "s/^\.\///"` | |
if [[ $? != 0 ]]; then | |
echo -e "${RED}FILE $path is unused!!!${NC}" | |
echo $path >> unused_files.log | |
fi | |
else | |
echo -e "${RED}FILE $path is unused!!!${NC}" | |
echo $path >> unused_files.log | |
fi | |
fi | |
done |
Thanks! Worked for me.
I experienced the same problem as @mybadstudios but for me there was a reason and an easy fix:
If the script is simply applied to the whole project it will find no/few unreferenced objects because there might not needed demo scenes using the assets. Demo scenes should be removed first. Then the script can fin unused assets. After removing these the script was able to find now unused materials. After removing these the script was able to find now unused textures.
A three step process, but works like a charm.
Thanks for sharing!
Thanks for the code!
I'm pretty new to Unity. How do I use this code?
Any help is appreciated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this.
It sounded awesome but unfortunately the only output I got from this was that every single .meta file in my project was not being used :(