资源简介

随着项目的研发进度,Asset数量也会爆发式地增长,资源管理面临很大的挑战。如何查找并区分有效和无效的资源,是很多项目管理者非常重视的事情。本文介绍的这个工具可以自动分析查找项目中未使用到的资源,在选择删除的时候还可以将这些文件打包备份,确保资源的安全性、提高项目管理的效率。

资源截图

代码片段和文件信息

/**
asset cleaner
Copyright (c) 2015 Tatsuhiko Yamamura

    This software is released under the MIT License.
    http://opensource.org/licenses/mit-license.php
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using System.IO;
using System.Text.Regularexpressions;

namespace AssetClean
{
public class AssetCollector
{
public List deleteFileList = new List ();
ClassReferenceCollection classCollection = new ClassReferenceCollection ();
ShaderReferenceCollection shaderCollection = new ShaderReferenceCollection ();

public bool useCodeStrip = true;
public bool saveEditorExtensions = true;

public void Collection ()
{
try {
deleteFileList.Clear ();

if( useCodeStrip ){
classCollection.Collection ();
}
shaderCollection.Collection ();

// Find assets
var files = Directory.GetFiles (“Assets“ “*.*“ SearchOption.AllDirectories)
.Where (item => Path.GetExtension (item) != “.meta“)
.Where (item => Path.GetExtension (item) != “.js“)
.Where (item => Path.GetExtension (item) != “.dll“)
.Where (item => Regex.IsMatch (item “[\\/\\\\]Gizmos[\\/\\\\]“) == false)
.Where (item => Regex.IsMatch (item “[\\/\\\\]Plugins[\\/\\\\]Android[\\/\\\\]“) == false)
.Where (item => Regex.IsMatch (item “[\\/\\\\]Plugins[\\/\\\\]iOS[\\/\\\\]“) == false)
.Where (item => Regex.IsMatch (item “[\\/\\\\]Resources[\\/\\\\]“) == false);

if( useCodeStrip == false ){
files = files.Where( item => Path.GetExtension(item) != “.cs“);
}

foreach (var path in files) {
var guid = AssetDatabase.AssetPathToGUID (path);
deleteFileList.Add (guid);
}
EditorUtility.DisplayProgressBar (“checking“ “collection all files“ 0.2f);
UnregistReferenceFromResources();

EditorUtility.DisplayProgressBar (“checking“ “check reference from resources“ 0.4f);
UnregistReferenceFromScenes();

EditorUtility.DisplayProgressBar (“checking“ “check reference from scenes“ 0.6f);
if( saveEditorExtensions ){
UnregistEditorCodes();
}
} finally {
EditorUtility.ClearProgressBar ();
}
}
void UnregistReferenceFromResources()
{
var resourcesFiles = Directory.GetFiles (“Assets“ “*.*“ SearchOption.AllDirectories)
.Where (item => Regex.IsMatch (item “[\\/\\\\]Resources[\\/\\\\]“) == true)
.Where (item => Path.GetExtension (item) != “.meta“)
.ToArray ();
foreach (var path in AssetDatabase.GetDependencies (resourcesFiles)) {
UnregistFromDelteList (AssetDatabase.AssetPathToGUID(path));
}
}

void UnregistReferenceFromScenes()
{
// Exclude objects that reference from scenes.
var scenes = EditorBuildSettings.scenes
.Where (item => item.enabled == true)
.Select (item => item.path)
.ToArray ();
foreach (var path in AssetDatabase.GetDependencies (scenes)) {
if( saveEditorExtensions == false ){
Debug.Log(path);
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-07-26 14:42  Tool-UnityAssetCleaner-master\
     文件         263  2015-07-26 14:42  Tool-UnityAssetCleaner-master\.gitignore
     目录           0  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\
     文件         192  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner.meta
     目录           0  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\
     文件         192  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor.meta
     目录           0  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\
     文件        5013  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\AssetCollector.cs
     文件         262  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\AssetCollector.cs.meta
     文件        6320  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\ClassReferenceCollection.cs
     文件         262  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\ClassReferenceCollection.cs.meta
     文件        4751  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\FindUnusedAssets.cs
     文件         262  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\FindUnusedAssets.cs.meta
     文件        2307  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\ShaderReferenceCollection.cs
     文件         262  2015-07-26 14:42  Tool-UnityAssetCleaner-master\Assets\Cleaner\Editor\ShaderReferenceCollection.cs.meta
     文件        1086  2015-07-26 14:42  Tool-UnityAssetCleaner-master\LICENSE
     目录           0  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\
     文件        4136  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\AudioManager.asset
     文件        4276  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\DynamicsManager.asset
     文件        4104  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\EditorBuildSettings.asset
     文件        4188  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\EditorSettings.asset
     文件        4228  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\GraphicsSettings.asset
     文件        5520  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\InputManager.asset
     文件        4384  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\NavMeshAreas.asset
     文件        4112  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\NetworkManager.asset
     文件        4308  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\Physics2DSettings.asset
     文件       35901  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\ProjectSettings.asset
     文件          52  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\ProjectVersion.txt
     文件        4944  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\QualitySettings.asset
     文件        4236  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\TagManager.asset
     文件        4112  2015-07-26 14:42  Tool-UnityAssetCleaner-master\ProjectSettings\TimeManager.asset
............此处省略1个文件信息

评论

共有 条评论