Skip to content

Commit d1983c0

Browse files
committed
fixed folders with '.' in names being ignored in regenerating ot applying guids. now also supporting to only regen guids of folder meta assets.
1 parent 427c66b commit d1983c0

7 files changed

Lines changed: 154 additions & 21 deletions

File tree

Assets/NikosAssets/U3DHelperTools/Samples/Resources/RegenerateGUIDFolder/Folder.With.Dots.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!21 &2100000
4+
Material:
5+
serializedVersion: 6
6+
m_ObjectHideFlags: 0
7+
m_CorrespondingSourceObject: {fileID: 0}
8+
m_PrefabInstance: {fileID: 0}
9+
m_PrefabAsset: {fileID: 0}
10+
m_Name: File.With.Dots
11+
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
12+
m_ShaderKeywords:
13+
m_LightmapFlags: 4
14+
m_EnableInstancingVariants: 0
15+
m_DoubleSidedGI: 0
16+
m_CustomRenderQueue: -1
17+
stringTagMap: {}
18+
disabledShaderPasses: []
19+
m_SavedProperties:
20+
serializedVersion: 3
21+
m_TexEnvs:
22+
- _BumpMap:
23+
m_Texture: {fileID: 0}
24+
m_Scale: {x: 1, y: 1}
25+
m_Offset: {x: 0, y: 0}
26+
- _DetailAlbedoMap:
27+
m_Texture: {fileID: 0}
28+
m_Scale: {x: 1, y: 1}
29+
m_Offset: {x: 0, y: 0}
30+
- _DetailMask:
31+
m_Texture: {fileID: 0}
32+
m_Scale: {x: 1, y: 1}
33+
m_Offset: {x: 0, y: 0}
34+
- _DetailNormalMap:
35+
m_Texture: {fileID: 0}
36+
m_Scale: {x: 1, y: 1}
37+
m_Offset: {x: 0, y: 0}
38+
- _EmissionMap:
39+
m_Texture: {fileID: 0}
40+
m_Scale: {x: 1, y: 1}
41+
m_Offset: {x: 0, y: 0}
42+
- _MainTex:
43+
m_Texture: {fileID: 0}
44+
m_Scale: {x: 1, y: 1}
45+
m_Offset: {x: 0, y: 0}
46+
- _MetallicGlossMap:
47+
m_Texture: {fileID: 0}
48+
m_Scale: {x: 1, y: 1}
49+
m_Offset: {x: 0, y: 0}
50+
- _OcclusionMap:
51+
m_Texture: {fileID: 0}
52+
m_Scale: {x: 1, y: 1}
53+
m_Offset: {x: 0, y: 0}
54+
- _ParallaxMap:
55+
m_Texture: {fileID: 0}
56+
m_Scale: {x: 1, y: 1}
57+
m_Offset: {x: 0, y: 0}
58+
m_Floats:
59+
- _BumpScale: 1
60+
- _Cutoff: 0.5
61+
- _DetailNormalMapScale: 1
62+
- _DstBlend: 0
63+
- _GlossMapScale: 1
64+
- _Glossiness: 0.5
65+
- _GlossyReflections: 1
66+
- _Metallic: 0
67+
- _Mode: 0
68+
- _OcclusionStrength: 1
69+
- _Parallax: 0.02
70+
- _SmoothnessTextureChannel: 0
71+
- _SpecularHighlights: 1
72+
- _SrcBlend: 1
73+
- _UVSec: 0
74+
- _ZWrite: 1
75+
m_Colors:
76+
- _Color: {r: 1, g: 1, b: 1, a: 1}
77+
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

Assets/NikosAssets/U3DHelperTools/Samples/Resources/RegenerateGUIDFolder/Folder.With.Dots/File.With.Dots.mat.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/NikosAssets/U3DHelperTools/Samples/Scripts/EditorUtilitiesSample.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using NaughtyAttributes;
3+
using NikosAssets.Helpers.Editor;
34
using UnityEngine;
45
using Object = UnityEngine.Object;
56

@@ -12,6 +13,12 @@ public class EditorUtilitiesSample : BaseNotesMono
1213

1314
[BoxGroup(HelperConstants.ATTRIBUTE_FIELD_BOXGROUP_SETTINGS)]
1415
public string desiredFileName = "_RENAME_TEST_2";
16+
17+
[BoxGroup(HelperConstants.ATTRIBUTE_FIELD_BOXGROUP_SETTINGS)]
18+
public bool regenGuidsRecursive = true;
19+
20+
[BoxGroup(HelperConstants.ATTRIBUTE_FIELD_BOXGROUP_SETTINGS)]
21+
public GUIDHelper.AcceptedMetaFiles acceptedMetaFilesForGuidRegen = GUIDHelper.AcceptedMetaFiles.Any;
1522

1623
[BoxGroup(HelperConstants.ATTRIBUTE_FIELD_BOXGROUP_DESCRIPTIONS)]
1724
public string pathChosen = "Assets/";
@@ -69,7 +76,7 @@ public void RenameFilesAtChosenPath()
6976
[Button("Regenerate GUIDs recursive and keep the file references at the chosen path")]
7077
public void RegenerateGUIDsAtChosenPathRecursive()
7178
{
72-
Editor.GUIDHelper.RegenerateGuids(pathChosen, true);
79+
Editor.GUIDHelper.RegenerateGuids(pathChosen, regenGuidsRecursive, acceptedMetaFilesForGuidRegen);
7380
}
7481

7582
#endif

Assets/NikosAssets/U3DHelperTools/Samples/Scripts/GUIDRegenScriptableObjectSample.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using NaughtyAttributes;
3+
using NikosAssets.Helpers.Editor;
34
using UnityEngine;
45

56
namespace NikosAssets.Helpers.Samples
@@ -16,7 +17,7 @@ public void RegenerateOnlyScriptableObjectsRecursive()
1617
#if UNITY_EDITOR
1718
Editor.GUIDHelper.RegenerateGuids(Editor.EditorUtilitiesHelper.PickFolderInsideProject("Regen GUIDS",
1819
"Assets/", "Assets/"), true,
19-
true, true, new []{".asset"});
20+
GUIDHelper.AcceptedMetaFiles.Any, true, new []{".asset"});
2021
#endif
2122
}
2223
}

Assets/NikosAssets/U3DHelperTools/Scripts/Editor/EditorUtilitiesHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ public static Object CreateScript(string localFilePath, string templateFileName,
129129
/// <param name="recursive">
130130
/// Go into sub folders as well?
131131
/// </param>
132-
public static void PickFolderAndRegenerateGUIDs(bool recursive)
132+
/// <param name="acceptedMetaFiles">
133+
/// Only Regen GUIDs for files or folders or both?
134+
/// </param>
135+
public static void PickFolderAndRegenerateGUIDs(bool recursive, GUIDHelper.AcceptedMetaFiles acceptedMetaFiles)
133136
{
134137
string path = PickFolderInsideProject("Regenerate GUIDs in folder", "Assets/", null);
135138

@@ -139,7 +142,7 @@ public static void PickFolderAndRegenerateGUIDs(bool recursive)
139142
return;
140143
}
141144

142-
GUIDHelper.RegenerateGuids(path, recursive);
145+
GUIDHelper.RegenerateGuids(path, recursive, acceptedMetaFiles);
143146
}
144147

145148
/// <summary>
@@ -313,7 +316,7 @@ private static void RegenerateGUIDsRecursive()
313316
"\n\nMake a backup of your project beforehand!",
314317
"Regenerate GUIDs recursive", "Cancel"))
315318
{
316-
PickFolderAndRegenerateGUIDs(true);
319+
PickFolderAndRegenerateGUIDs(true, GUIDHelper.AcceptedMetaFiles.Any);
317320
}
318321
}
319322

@@ -325,7 +328,7 @@ private static void RegenerateGUIDsNonRecursive()
325328
"\n\nMake a backup of your project beforehand!",
326329
"Regenerate GUIDs non-recursive", "Cancel"))
327330
{
328-
PickFolderAndRegenerateGUIDs(false);
331+
PickFolderAndRegenerateGUIDs(false, GUIDHelper.AcceptedMetaFiles.Any);
329332
}
330333
}
331334

Assets/NikosAssets/U3DHelperTools/Scripts/Editor/GUIDHelper.cs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ namespace NikosAssets.Helpers.Editor
1414
/// </summary>
1515
public static class GUIDHelper
1616
{
17+
public enum AcceptedMetaFiles
18+
{
19+
/// <summary>
20+
/// Any file or folder asset
21+
/// </summary>
22+
Any = 0,
23+
/// <summary>
24+
/// Only search for files and ignore folder meta assets
25+
/// </summary>
26+
IgnoreFolders = 1,
27+
/// <summary>
28+
/// Only search for folder meta assets and ignore files
29+
/// </summary>
30+
IgnoreFiles = 2
31+
}
32+
1733
/// <summary>
1834
/// Unity files that contain GUID (references), feel free to add unlisted ones
1935
/// </summary>
@@ -64,8 +80,8 @@ public static class GUIDHelper
6480
/// <param name="recursive">
6581
/// Can the asset be in a sub directory of <paramref name="isInLocalDirectory"/>?
6682
/// </param>
67-
/// <param name="ignoreFolderAssets">
68-
/// Can the owner GUID be a folder asset (folder + .meta)?
83+
/// <param name="acceptedMetaFiles">
84+
/// Should we accept only folders or only file assets or both?
6985
/// </param>
7086
/// <param name="whiteListExtensions">
7187
/// Must include extensions like (.mat, .cs) or accept all file types.
@@ -74,15 +90,28 @@ public static class GUIDHelper
7490
/// <returns>
7591
/// If the given file at <paramref name="filePath"/> is a GUID owner (meta) file (true or false)
7692
/// </returns>
77-
private static bool IsAcceptedMetaFile(string isInLocalDirectory, string filePath, bool recursive, bool ignoreFolderAssets, string[] whiteListExtensions)
93+
private static bool IsAcceptedMetaFile(string isInLocalDirectory, string filePath, bool recursive,
94+
AcceptedMetaFiles acceptedMetaFiles, string[] whiteListExtensions)
7895
{
7996
string directory = Path.GetDirectoryName(filePath).Replace(@"\", "/") + "/";
8097

81-
// First GUID in .meta file is always the GUID of the asset itself
82-
// We must only replace GUIDs for Resources present in Assets.
98+
if (!Path.GetExtension(filePath).Equals(".meta")) return false;
99+
100+
//any meta file
101+
bool isAccepted = true;
102+
string fileOrFolder = filePath.Substring(0, filePath.Length - 5);
103+
switch (acceptedMetaFiles)
104+
{
105+
case AcceptedMetaFiles.IgnoreFolders:
106+
isAccepted = File.Exists(fileOrFolder);
107+
break;
108+
case AcceptedMetaFiles.IgnoreFiles:
109+
isAccepted = Directory.Exists(fileOrFolder);
110+
break;
111+
}
112+
83113
return
84-
(Path.GetExtension(filePath) == ".meta"
85-
&& (!ignoreFolderAssets || filePath.Count(fp => fp == '.') > 1)
114+
(isAccepted
86115
&&
87116
(
88117
//not recursive means that the directory ends with the localAssetPath
@@ -206,8 +235,8 @@ public static void SetGuids(
206235
/// <param name="recursive">
207236
/// Should we search for files inside other found folders of the <paramref name="localPathToApply"/>?
208237
/// </param>
209-
/// <param name="ignoreFolderAssets">
210-
/// Should we apply GUIDs to folder .meta files?
238+
/// <param name="acceptedMetaFiles">
239+
/// Should we accept only folders or only file assets or both?
211240
/// </param>
212241
/// <param name="logChangedAssets">
213242
/// Should we log the changed assets?
@@ -217,7 +246,7 @@ public static void SetGuids(
217246
/// </param>
218247
public static void ApplyGUIDsFrom(string localPathToApply, string globalPathToRead,
219248
bool recursive = true,
220-
bool ignoreFolderAssets = true,
249+
AcceptedMetaFiles acceptedMetaFiles = AcceptedMetaFiles.IgnoreFolders,
221250
bool logChangedAssets = false,
222251
string[] whiteListExtensions = null)
223252
{
@@ -251,7 +280,7 @@ public static void ApplyGUIDsFrom(string localPathToApply, string globalPathToRe
251280
List<string> guids = GetGuidsFromFileContents(contents);
252281

253282
//can we add this file to the guidOldToNewMap?
254-
if (IsAcceptedMetaFile(localPathToApply, filePath, recursive, ignoreFolderAssets,
283+
if (IsAcceptedMetaFile(localPathToApply, filePath, recursive, acceptedMetaFiles,
255284
whiteListExtensions))
256285
{
257286
int matchingGlobalFilePathIndex =
@@ -309,8 +338,8 @@ public static void ApplyGUIDsFrom(string localPathToApply, string globalPathToRe
309338
/// <param name="recursive">
310339
/// Should we search for files inside other found folders of the <paramref name="localPath"/>?
311340
/// </param>
312-
/// <param name="ignoreFolderAssets">
313-
/// Should we generate new GUIDs for folder .meta files?
341+
/// <param name="acceptedMetaFiles">
342+
/// Should we accept only folders or only file assets or both?
314343
/// </param>
315344
/// <param name="logChangedAssets">
316345
/// Should we log the changed assets?
@@ -321,7 +350,7 @@ public static void ApplyGUIDsFrom(string localPathToApply, string globalPathToRe
321350
public static void RegenerateGuids(
322351
string localPath,
323352
bool recursive = true,
324-
bool ignoreFolderAssets = true,
353+
AcceptedMetaFiles acceptedMetaFiles = AcceptedMetaFiles.Any,
325354
bool logChangedAssets = false,
326355
string[] whiteListExtensions = null)
327356
{
@@ -345,7 +374,7 @@ public static void RegenerateGuids(
345374
List<string> guids = GetGuidsFromFileContents(contents);
346375

347376
//can we add this file to the guidOldToNewMap?
348-
if (IsAcceptedMetaFile(localPath, filePath, recursive, ignoreFolderAssets, whiteListExtensions))
377+
if (IsAcceptedMetaFile(localPath, filePath, recursive, acceptedMetaFiles, whiteListExtensions))
349378
{
350379
string oldGuid = guids.First();
351380
// Generate and save new GUID if we haven't added it before

0 commit comments

Comments
 (0)