Skip to content

Commit 4bb2ab4

Browse files
authored
Merge pull request #242 from Unity-Technologies/UNI-24636-1.2.0b1-forum-release
UNI-24636 1.2.0b1 forum release
2 parents 73c2c32 + 42037b2 commit 4bb2ab4

40 files changed

+733
-33
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
**/.idea/
2121

2222
## Unity ##
23-
*.meta
23+
[Aa]ssets/**/*.meta
24+
![Aa]ssets/FbxExporters/**/*.meta
2425
[Pp]rojectSettings/*.asset
2526
[Pp]rojectSettings/*.txt
2627

Assets/FbxExporters/Editor.meta

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

Assets/FbxExporters/Editor/ConvertToModel.cs.meta

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

Assets/FbxExporters/Editor/EditorRotate.cs.meta

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

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,43 @@ public override void OnInspectorGUI() {
180180
}
181181
EditorGUI.EndDisabledGroup ();
182182

183+
EditorGUILayout.Space ();
184+
185+
EditorGUI.indentLevel--;
186+
EditorGUILayout.LabelField ("FBX Prefab Component Updater", EditorStyles.boldLabel);
187+
EditorGUI.indentLevel++;
188+
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 Prefab Component Updater";
201+
if (filesToRepairCount > 0) {
202+
bool result = UnityEditor.EditorUtility.DisplayDialog (dialogTitle,
203+
string.Format("Found {0} prefab(s) and/or scene(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");
208+
if (result) {
209+
componentUpdater.ReplaceGUIDInTextAssets ();
210+
} else {
211+
var assetsToRepair = componentUpdater.GetAssetsToRepair ();
212+
Debug.LogFormat ("Failed to update the FbxPrefab components in the following files:\n{0}", string.Join ("\n", assetsToRepair));
213+
}
214+
} else {
215+
UnityEditor.EditorUtility.DisplayDialog (dialogTitle,
216+
"Couldn't find any prefabs or scenes that require updating", "Ok");
217+
}
218+
}
219+
183220
GUILayout.FlexibleSpace ();
184221
GUILayout.EndScrollView ();
185222
GUILayout.EndVertical();

Assets/FbxExporters/Editor/FbxExportSettings.cs.meta

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

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private bool ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] un
199199
// Set the normals on Layer 0.
200200
FbxLayer fbxLayer = GetOrCreateLayer(fbxMesh);
201201

202-
if (mesh.HasValidNormals(unmergedTriangles.Length)) {
202+
if (mesh.HasValidNormals()) {
203203
using (var fbxLayerElement = FbxLayerElementNormal.Create (fbxMesh, "Normals")) {
204204
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
205205
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eDirect);
@@ -218,7 +218,7 @@ private bool ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] un
218218
}
219219

220220
/// Set the binormals on Layer 0.
221-
if (mesh.HasValidBinormals(unmergedTriangles.Length)) {
221+
if (mesh.HasValidBinormals()) {
222222
using (var fbxLayerElement = FbxLayerElementBinormal.Create (fbxMesh, "Binormals")) {
223223
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
224224
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eDirect);
@@ -236,7 +236,7 @@ private bool ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] un
236236
}
237237

238238
/// Set the tangents on Layer 0.
239-
if (mesh.HasValidTangents(unmergedTriangles.Length)) {
239+
if (mesh.HasValidTangents()) {
240240
using (var fbxLayerElement = FbxLayerElementTangent.Create (fbxMesh, "Tangents")) {
241241
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
242242
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eDirect);
@@ -260,7 +260,7 @@ mesh.Tangents [unityTriangle] [2]
260260

261261
exportedAttribute |= ExportUVs (fbxMesh, mesh, unmergedTriangles);
262262

263-
if (mesh.HasValidVertexColors(unmergedTriangles.Length)) {
263+
if (mesh.HasValidVertexColors()) {
264264
using (var fbxLayerElement = FbxLayerElementVertexColor.Create (fbxMesh, "VertexColors")) {
265265
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
266266
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eIndexToDirect);
@@ -1463,9 +1463,7 @@ public Vector3 [] Binormals {
14631463
var normals = Normals;
14641464
var tangents = Tangents;
14651465

1466-
if (IsValidArray<Vector3>(normals) &&
1467-
IsValidArray<Vector4>(tangents, normals.Length)
1468-
) {
1466+
if (HasValidNormals() && HasValidTangents()) {
14691467
m_Binormals = new Vector3 [normals.Length];
14701468

14711469
for (int i = 0; i < normals.Length; i++)
@@ -1546,34 +1544,22 @@ public MeshInfo (Mesh mesh, Material[] materials)
15461544
}
15471545
}
15481546

1549-
/// <summary>
1550-
/// Determines whether this instance is a valid array with the specified length.
1551-
/// </summary>
1552-
/// <returns><c>true</c> if this instance is valid array; otherwise, <c>false</c>.</returns>
1553-
/// <param name="array">Array.</param>
1554-
/// <param name="expectedLength">Expected length.</param>
1555-
/// <typeparam name="T">The 1st type parameter.</typeparam>
1556-
public static bool IsValidArray<T>(T[] array, int expectedLength = -1)
1557-
{
1558-
return array != null &&
1559-
array.Length > 0 &&
1560-
(expectedLength >= 0 ? array.Length == expectedLength : true);
1561-
}
1562-
1563-
public bool HasValidNormals(int expectedLength){
1564-
return IsValidArray<Vector3> (Normals, expectedLength);
1547+
public bool HasValidNormals(){
1548+
return Normals != null && Normals.Length > 0;
15651549
}
15661550

1567-
public bool HasValidBinormals(int expectedLength){
1568-
return IsValidArray<Vector3> (Binormals, expectedLength);
1551+
public bool HasValidBinormals(){
1552+
return HasValidNormals () &&
1553+
HasValidTangents () &&
1554+
Binormals != null;
15691555
}
15701556

1571-
public bool HasValidTangents(int expectedLength){
1572-
return IsValidArray<Vector4> (Tangents, expectedLength);
1557+
public bool HasValidTangents(){
1558+
return Tangents != null && Tangents.Length > 0;
15731559
}
15741560

1575-
public bool HasValidVertexColors(int expectedLength){
1576-
return IsValidArray<Color32> (VertexColors, expectedLength);
1561+
public bool HasValidVertexColors(){
1562+
return VertexColors != null && VertexColors.Length > 0;
15771563
}
15781564
}
15791565

Assets/FbxExporters/Editor/FbxExporter.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
using System.IO;
6+
7+
namespace FbxExporters.Editor
8+
{
9+
public class RepairMissingScripts
10+
{
11+
private const string m_forumPackageGUID = "2d81c55c4d9d85146b1d2de96e084b63";
12+
private const string m_currentPackageGUID = "628ffbda3fdf4df4588770785d91a698";
13+
14+
private const string m_fbxPrefabDLLFileId = "69888640";
15+
16+
private const string m_idFormat = "{{fileID: {0}, guid: {1}, type:";
17+
18+
private static string m_forumPackageSearchID;
19+
20+
private static string ForumPackageSearchID {
21+
get {
22+
if (string.IsNullOrEmpty (m_forumPackageSearchID)) {
23+
m_forumPackageSearchID = string.Format (m_idFormat, m_fbxPrefabDLLFileId, m_forumPackageGUID);
24+
}
25+
return m_forumPackageSearchID;
26+
}
27+
}
28+
29+
private static string m_currentPackageSearchID;
30+
31+
private static string CurrentPackageSearchID {
32+
get {
33+
if (string.IsNullOrEmpty (m_currentPackageSearchID)) {
34+
m_currentPackageSearchID = string.Format (m_idFormat, m_fbxPrefabDLLFileId, m_currentPackageGUID);
35+
}
36+
return m_currentPackageSearchID;
37+
}
38+
}
39+
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()
59+
{
60+
// search project for assets containing old GUID
61+
62+
// ignore if forced binary
63+
if (UnityEditor.EditorSettings.serializationMode == SerializationMode.ForceBinary) {
64+
return new string[]{};
65+
}
66+
67+
// check all scenes and prefabs
68+
string[] searchFilePatterns = new string[]{ "*.prefab", "*.unity" };
69+
70+
List<string> assetsToRepair = new List<string> ();
71+
foreach (string searchPattern in searchFilePatterns) {
72+
foreach (string file in Directory.GetFiles(Application.dataPath, searchPattern, SearchOption.AllDirectories)) {
73+
if (AssetNeedsRepair (file)) {
74+
assetsToRepair.Add (file);
75+
}
76+
}
77+
}
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+
}
112+
if (replacedGUID) {
113+
AssetDatabase.Refresh ();
114+
}
115+
return replacedGUID;
116+
}
117+
118+
private static bool ReplaceGUIDInFile (string path)
119+
{
120+
// try to read file, assume it's a text file for now
121+
bool modified = false;
122+
123+
try {
124+
var tmpFile = Path.GetTempFileName();
125+
if(string.IsNullOrEmpty(tmpFile)){
126+
return false;
127+
}
128+
129+
using(var sr = new StreamReader (path)){
130+
// verify that this is a text file
131+
var firstLine = "";
132+
if (sr.Peek () > -1) {
133+
firstLine = sr.ReadLine ();
134+
if (!firstLine.StartsWith ("%YAML")) {
135+
sr.Close ();
136+
return false;
137+
}
138+
}
139+
140+
using(var sw = new StreamWriter (tmpFile, false)){
141+
if (!string.IsNullOrEmpty (firstLine)) {
142+
sw.WriteLine (firstLine);
143+
}
144+
145+
while (sr.Peek () > -1) {
146+
var line = sr.ReadLine ();
147+
148+
if (line.Contains (ForumPackageSearchID)) {
149+
line = line.Replace (ForumPackageSearchID, CurrentPackageSearchID);
150+
modified = true;
151+
}
152+
153+
sw.WriteLine (line);
154+
}
155+
}
156+
}
157+
158+
if (modified) {
159+
File.Delete (path);
160+
File.Move (tmpFile, path);
161+
162+
Debug.LogFormat("Updated FbxPrefab components in file {0}", path);
163+
return true;
164+
} else {
165+
File.Delete (tmpFile);
166+
}
167+
} catch (IOException e) {
168+
Debug.LogError (string.Format ("Failed to replace GUID in file {0} (error={1})", path, e));
169+
}
170+
171+
return false;
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)