UnityFileUtil

Static Class in ChivalrousGames.Common

Description

A collection of static methods to help handling various Unity files. Can be used to manage meta files. Usage of some of these methods can break your project. Use this class wisely and backup your project often.

Static Methods

ComputeTypeHash
public static int ComputeTypeHash(Type t);
Parameters
type Type Type for which hash must be calculated
Return
int Computed hash using the MD4 hash algorithm
Description

Computes the type hash. Mostly used to compute the GUID of a Type in a DLL.

GenerateMetaFileGuid
public static string GenerateMetaFileGuid();
Return
string A random guid to use in a meta file
Description

Generates a random GUID to be used in unity meta files

CollectFiles
public static List CollectFiles(string directoryPath, bool includeSubDir);
Parameters
directoryPath string Path toward the directory from which files must be collected
includeSubDir bool true to collect files from sub-directories as well; otherwise, false
Return
List A List of files info
Description

Collects the files from a directory.

CollectFiles
public static List CollectFiles(DirectoryInfo directoryInfo, bool includeSubDir);
Parameters
directoryInfo DirectoryInfo DirectoryInfo for the directory from which files must be collected
includeSubDir bool true to collect files from sub-directories as well; otherwise, false
Return
List A List of files info
Description

Collects the files from a directory.

CollectMetaFiles
public static List CollectMetaFiles(string directoryPath, bool includeSubDir);
Parameters
directoryPath string Path toward the directory from which meta files must be collected
includeSubDir bool true to collect meta files from sub-directories as well; otherwise, false
Return
List A List of meta files info
Description

Collects the meta files from a directory.

CollectMetaFiles
public static List CollectMetaFiles(DirectoryInfo directoryInfo, bool includeSubDir);
Parameters
directoryInfo DirectoryInfo DirectoryInfo for the directory from which meta files must be collected
includeSubDir bool true to collect meta files from sub-directories as well; otherwise, false
Return
List A List of meta files info
Description

Collects the meta files from a directory.

CollectAssetFiles
public static List CollectAssetFiles(string directoryPath, bool includeSubDir);
Parameters
directoryPath string Path toward the directory from which asset files must be collected
includeSubDir bool true to collect asset files from sub-directories as well; otherwise, false
Return
List A List of asset files info
Description

Collects the asset files from a directory.

CollectAssetFiles
public static List CollectAssetFiles(DirectoryInfo directoryInfo, bool includeSubDir);
Parameters
directoryInfo DirectoryInfo DirectoryInfo for the directory from which asset files must be collected
includeSubDir bool true to collect asset files from sub-directories as well; otherwise, false
Return
List A List of asset files info
Description

Collects the asset files from a directory.

GetGuidFromMetaFile
public static string GetGuidFromMetaFile(FileInfo metaFileInfo);
Parameters
metaFileInfo FileInfo FileInfo for the meta file from which to exract the GUID
Return
string GUID of the meta file
Description

Gets the GUID from meta file.

ReplaceFileContent
public static bool ReplaceFileContent(FileInfo fileInfo, string before, string after);
Parameters
fileInfo FileInfo FileInfo of the file to modify
before string Current string content of the file that must be replaced
after string String content that must be used to replace to current content
Return
bool true file content was replaced; otherwise, false
Description

Replaces a specified content from a file to another specified content.

UpdateScriptReferences
public static bool UpdateScriptReferences(FileInfo fileInfo, string oldGuid, string newGuid, string oldFileId, string newFileId);
Parameters
fileInfo FileInfo FileInfo of the file in whcih to modify references. Should be an asset file.
oldGuid string Current guid reference in the file
newGuid string Guid reference that must now be used in the file
oldFileId string Current file id reference in the file
newFileId string File id reference that must now be used in the file
Return
bool true file content was replaced; otherwise, false
Description

This should be used to on an asset file to replace the asset referenced in it. Example: you could have a prefab that is using a model X. You could change this reference toward model X for a model Y through usage of this method rather than doing it manually.

MetaGuidRegen
public static GuidRegenResult MetaGuidRegen(string directoryPath);
Parameters
directoryPath string Path toward the directory in which meta guid regeneration must be done
Return
GuidRegenResult The results of the regen operation
Description

Regenerates the guids from all meta files and fixes references in the specified directory and all sub directories. This can be used to create copies of assets.

MetaGuidRegen
public static GuidRegenResult MetaGuidRegen(DirectoryInfo directoryInfo);
Parameters
directoryInfo DirectoryInfo DirectoryInfo the directory in which meta guid regeneration must be done
Return
GuidRegenResult The results of the regen operation
Description

Regenerates the guids from all meta files and fixes references in the specified directory and all sub directories. This can be used to create copies of assets.