Skip to content

Commit 51b605a

Browse files
authored
Merge pull request #255 from Unity-Technologies/UNI-33630-update-UX-for-missing-script-fix
UNI-33630 update UX for missing script fix
2 parents aab851b + 4a810dd commit 51b605a

File tree

2 files changed

+89
-26
lines changed

2 files changed

+89
-26
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,40 @@ public override void OnInspectorGUI() {
180180
}
181181
EditorGUI.EndDisabledGroup ();
182182

183-
if (!HideRepairMissingScripts ()) {
184-
EditorGUILayout.Space ();
185-
186-
EditorGUI.indentLevel--;
187-
EditorGUILayout.LabelField ("Repair Missing Scripts", EditorStyles.boldLabel);
188-
EditorGUI.indentLevel++;
183+
EditorGUILayout.Space ();
189184

190-
EditorGUILayout.Space ();
185+
EditorGUI.indentLevel--;
186+
EditorGUILayout.LabelField ("Run Component Updater", EditorStyles.boldLabel);
187+
EditorGUI.indentLevel++;
191188

192-
var repairMissingScripts = new GUIContent (
193-
"Repair Missing Scripts",
194-
"Repair missing FbxPrefab scripts in text assets");
195-
if (GUILayout.Button (repairMissingScripts)) {
196-
bool result = FbxExporters.Editor.RepairMissingScripts.ReplaceGUIDInTextAssets ();
189+
EditorGUILayout.Space ();
190+
191+
var repairMissingScripts = new GUIContent (
192+
"Run Component Updater",
193+
"If the forum package 1.1.0b1 was previously installed, then links to the FbxPrefab component " +
194+
"in assets created with the FBX exporter will need updating.\n" +
195+
"Run this button to update all FbxPrefab references in text serialized prefabs and scene files.");
196+
197+
if (GUILayout.Button (repairMissingScripts)) {
198+
var componentUpdater = new FbxExporters.Editor.RepairMissingScripts ();
199+
var filesToRepairCount = componentUpdater.GetAssetsToRepairCount ();
200+
var dialogTitle = "FBX Exporter Component Updater";
201+
if (filesToRepairCount > 0) {
202+
bool result = UnityEditor.EditorUtility.DisplayDialog (dialogTitle,
203+
string.Format("Found {0} prefab and/or scene file(s) with components requiring update.\n\n" +
204+
"If you choose 'Go Ahead', the FbxPrefab components in these assets " +
205+
"will be automatically updated to work with the latest FBX exporter.\n" +
206+
"You should make a backup before proceeding.", filesToRepairCount),
207+
"I Made a Backup. Go Ahead!", "No Thanks");
197208
if (result) {
198-
UnityEditor.EditorUtility.DisplayDialog ("Finished Repairing Scripts",
199-
"Repaired missing scripts in text serialized assets", "Ok");
209+
componentUpdater.ReplaceGUIDInTextAssets ();
200210
} else {
201-
UnityEditor.EditorUtility.DisplayDialog ("Finished Repairing Scripts",
202-
"Couldn't find any assets needing repair", "Ok");
211+
var assetsToRepair = componentUpdater.GetAssetsToRepair ();
212+
Debug.LogFormat ("Failed to update the FbxPrefab components in the following files:\n{0}", string.Join ("\n", assetsToRepair));
203213
}
214+
} else {
215+
UnityEditor.EditorUtility.DisplayDialog (dialogTitle,
216+
"Couldn't find any text assets that require updating", "Ok");
204217
}
205218
}
206219

@@ -214,11 +227,6 @@ public override void OnInspectorGUI() {
214227
}
215228
}
216229

217-
private static bool HideRepairMissingScripts(){
218-
var docPath = Application.dataPath + "/FbxExporters/FBX_Exporter_User_Guide_v1.1.0b1.pdf";
219-
return File.Exists(docPath)? false : true;
220-
}
221-
222230
private static string TryFindDCC(string dccPath, string ext, ExportSettings.DCCType dccType){
223231
string dccName = "";
224232
switch (dccType) {

Assets/FbxExporters/Editor/FbxExporterRepairMissingScripts.cs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class RepairMissingScripts
1515

1616
private const string m_idFormat = "{{fileID: {0}, guid: {1}, type:";
1717

18-
1918
private static string m_forumPackageSearchID;
2019

2120
private static string ForumPackageSearchID {
@@ -38,24 +37,78 @@ private static string CurrentPackageSearchID {
3837
}
3938
}
4039

41-
public static bool ReplaceGUIDInTextAssets ()
40+
private string[] m_assetsToRepair;
41+
private string[] AssetsToRepair{
42+
get{
43+
if (m_assetsToRepair == null) {
44+
m_assetsToRepair = FindAssetsToRepair ();
45+
}
46+
return m_assetsToRepair;
47+
}
48+
}
49+
50+
public int GetAssetsToRepairCount(){
51+
return AssetsToRepair.Length;
52+
}
53+
54+
public string[] GetAssetsToRepair(){
55+
return AssetsToRepair;
56+
}
57+
58+
public static string[] FindAssetsToRepair()
4259
{
4360
// search project for assets containing old GUID
4461

4562
// ignore if forced binary
4663
if (UnityEditor.EditorSettings.serializationMode == SerializationMode.ForceBinary) {
47-
return false;
64+
return new string[]{};
4865
}
4966

5067
// check all scenes and prefabs
5168
string[] searchFilePatterns = new string[]{ "*.prefab", "*.unity" };
5269

53-
bool replacedGUID = false;
70+
List<string> assetsToRepair = new List<string> ();
5471
foreach (string searchPattern in searchFilePatterns) {
5572
foreach (string file in Directory.GetFiles(Application.dataPath, searchPattern, SearchOption.AllDirectories)) {
56-
replacedGUID |= ReplaceGUIDInFile (file);
73+
if (AssetNeedsRepair (file)) {
74+
assetsToRepair.Add (file);
75+
}
5776
}
5877
}
78+
return assetsToRepair.ToArray ();
79+
}
80+
81+
private static bool AssetNeedsRepair(string filePath)
82+
{
83+
try{
84+
using(var sr = new StreamReader (filePath)){
85+
if(sr.Peek() > -1){
86+
var firstLine = sr.ReadLine();
87+
if(!firstLine.StartsWith("%YAML")){
88+
sr.Close();
89+
return false;
90+
}
91+
}
92+
93+
var contents = sr.ReadToEnd();
94+
if(contents.Contains(ForumPackageSearchID)){
95+
sr.Close();
96+
return true;
97+
}
98+
}
99+
}
100+
catch(IOException e){
101+
Debug.LogError (string.Format ("Failed to check file for component update: {0} (error={1})", filePath, e));
102+
}
103+
return false;
104+
}
105+
106+
public bool ReplaceGUIDInTextAssets ()
107+
{
108+
bool replacedGUID = false;
109+
foreach (string file in AssetsToRepair) {
110+
replacedGUID |= ReplaceGUIDInFile (file);
111+
}
59112
if (replacedGUID) {
60113
AssetDatabase.Refresh ();
61114
}
@@ -105,6 +158,8 @@ private static bool ReplaceGUIDInFile (string path)
105158
if (modified) {
106159
File.Delete (path);
107160
File.Move (tmpFile, path);
161+
162+
Debug.LogFormat("Updated FbxPrefab components in file {0}", path);
108163
return true;
109164
} else {
110165
File.Delete (tmpFile);

0 commit comments

Comments
 (0)