Skip to content

Commit afdc084

Browse files
authored
Merge pull request #223 from Unity-Technologies/Uni-29179_Handle_RectTransforms_in_export
Uni-29179 Handle Rect Transforms import / export
2 parents 5529da0 + 3e9598c commit afdc084

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,13 +983,40 @@ public HashSet<GameObject> ImplementUpdates(FbxPrefab prefabInstance)
983983
prefabComponent = prefabXfo.gameObject.AddComponent(fbxComponent.t);
984984
Log("created component {0}:{1}", nodeName, fbxComponent.t);
985985
}
986-
// Check that the component exists before copying to it.
987-
// This happens for Rect Transform components as the fbx
988-
// contains a Transform which it tries to add/update on the
989-
// prefab, but fails because you cannot have both a Transform
990-
// and a Rect Transform
991-
// UNI-29179 TODO: better handling of Rect Transforms
986+
987+
//If the prefabComponent has not been assigned yet,
988+
//it means that we couldn't find it, and that we tried to add it but that it seems like it already exists.
992989
if (!prefabComponent) {
990+
//This is to confirm that it is a RectTransform
991+
index = prefabComponents.FindIndex(x => x.GetType() == typeof(RectTransform));
992+
993+
if (index < 0)
994+
{
995+
Log("The component could not be found or added, and was not a RectTransform");
996+
continue;
997+
}
998+
999+
prefabComponent = prefabComponents[index];
1000+
prefabComponents.RemoveAt(index);
1001+
1002+
GameObject tempGameObject = new GameObject();
1003+
try
1004+
{
1005+
Transform tempTransform = tempGameObject.transform;
1006+
1007+
UnityEditor.EditorJsonUtility.FromJsonOverwrite(fbxComponent.jsonValue, tempTransform);
1008+
1009+
var rectTransform = prefabComponent as RectTransform;
1010+
rectTransform.localRotation = tempTransform.localRotation;
1011+
rectTransform.localPosition = tempTransform.localPosition;
1012+
rectTransform.localScale = tempTransform.localScale;
1013+
}
1014+
finally
1015+
{
1016+
GameObject.DestroyImmediate(tempGameObject);
1017+
}
1018+
1019+
Log("updated component {0}:{1}", nodeName, typeof(RectTransform));
9931020
continue;
9941021
}
9951022
// Now set the values.

Assets/FbxExporters/Editor/UnitTests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using NUnit.Framework;
44
using System.IO;
55
using System.Collections.Generic;
6+
using FbxExporters.Editor;
67

78
namespace FbxExporters.UnitTests
89
{
@@ -54,6 +55,62 @@ public void BasicTest ()
5455
imported));
5556
}
5657

58+
[Test]
59+
public void RectTransformTest ()
60+
{
61+
Vector3 scaleForward = new Vector3(1,2,3);
62+
Vector3 positionForward = new Vector3(100, 200, 300);
63+
Vector3 rotationForward = new Vector3(1,2,3);
64+
65+
Vector3 scaleBackward = new Vector3(3, 2, 1);
66+
Vector3 positionBackward = new Vector3(300, 200, 100);
67+
Vector3 rotationBackward = new Vector3(3,2,1);
68+
69+
//Create a hierarchy with a RectTransform
70+
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
71+
var capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
72+
capsule.AddComponent<RectTransform>();
73+
74+
capsule.GetComponent<RectTransform>().localScale = scaleForward;
75+
capsule.GetComponent<RectTransform>().localPosition = positionForward;
76+
capsule.GetComponent<RectTransform>().localRotation = Quaternion.Euler(rotationForward);
77+
78+
capsule.transform.parent = cube.transform;
79+
80+
string filePath = GetRandomFbxFilePath();
81+
82+
//instantiate our hierarchy as a prefab
83+
var oldInstance = ConvertToModel.Convert(cube, fbxFullPath: filePath);
84+
Assert.IsTrue(oldInstance);
85+
86+
Assert.IsTrue(oldInstance.transform.GetChild(0).GetComponent<RectTransform>().localScale == scaleForward);
87+
Assert.IsTrue(oldInstance.transform.GetChild(0).GetComponent<RectTransform>().localPosition == positionForward);
88+
Assert.IsTrue(oldInstance.transform.GetChild(0).GetComponent<RectTransform>().localRotation == Quaternion.Euler(rotationForward));
89+
90+
//Create an "updated" hierarchy
91+
var cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
92+
var capsule2 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
93+
capsule2.AddComponent<RectTransform>();
94+
95+
capsule2.GetComponent<RectTransform>().localScale = scaleBackward;
96+
capsule2.GetComponent<RectTransform>().localPosition = positionBackward;
97+
capsule2.GetComponent<RectTransform>().localRotation = Quaternion.Euler(rotationBackward);
98+
99+
capsule2.transform.parent = cube2.transform;
100+
101+
//export our updated hierarchy to the same file path as the original
102+
SleepForFileTimestamp();
103+
FbxExporters.Editor.ModelExporter.ExportObject(filePath, cube2);
104+
AssetDatabase.Refresh();
105+
106+
Assert.IsTrue(oldInstance.transform.GetChild(0).GetComponent<RectTransform>().localScale == scaleBackward);
107+
Assert.IsTrue(oldInstance.transform.GetChild(0).GetComponent<RectTransform>().localPosition == positionBackward);
108+
Assert.IsTrue(oldInstance.transform.GetChild(0).GetComponent<RectTransform>().localRotation == Quaternion.Euler(rotationBackward));
109+
110+
GameObject.DestroyImmediate(cube);
111+
GameObject.DestroyImmediate(cube2);
112+
}
113+
57114
[Test]
58115
public void ReplaceTest ()
59116
{

0 commit comments

Comments
 (0)