Skip to content

Commit 9ef96dc

Browse files
authored
Merge pull request #265 from Unity-Technologies/Uni-33830-recttransform-unity-2017-3
Uni-33830 update usage of RectTransform to Unity 2017.3
2 parents f04f76a + a2d9c09 commit 9ef96dc

File tree

2 files changed

+73
-54
lines changed

2 files changed

+73
-54
lines changed

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using UnityEngine;
45
using UnityEditor;
56
using System.Linq;
@@ -1017,6 +1018,16 @@ public HashSet<GameObject> ImplementUpdates(FbxPrefab prefabInstance)
10171018
rectTransform.localRotation = tempTransform.localRotation;
10181019
rectTransform.localPosition = tempTransform.localPosition;
10191020
rectTransform.localScale = tempTransform.localScale;
1021+
1022+
#region force update of rect transform 2017.3 or newer
1023+
// using reflection so we can continue to compile against versions 2017.1
1024+
// Retrieve the method you are looking for
1025+
System.Reflection.MethodInfo methodInfo =
1026+
rectTransform.GetType().GetMethod("ForceUpdateRectTransforms");
1027+
// Invoke the method on the instance
1028+
if (methodInfo!=null)
1029+
methodInfo.Invoke(rectTransform, null);
1030+
#endregion
10201031
}
10211032
finally
10221033
{

Assets/FbxExporters/Editor/UnitTests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,91 +21,99 @@ public class FbxPrefabAutoUpdaterTest : ExporterTestBase
2121
[SetUp]
2222
public void Init ()
2323
{
24-
var capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
25-
m_fbx = ExportSelection(capsule);
26-
m_fbxPath = AssetDatabase.GetAssetPath(m_fbx);
24+
var capsule = GameObject.CreatePrimitive (PrimitiveType.Capsule);
25+
m_fbx = ExportSelection (capsule);
26+
m_fbxPath = AssetDatabase.GetAssetPath (m_fbx);
2727

2828
// Instantiate the fbx and create a prefab from it.
2929
// Delete the object right away (don't even wait for term).
30-
var fbxInstance = PrefabUtility.InstantiatePrefab(m_fbx) as GameObject;
31-
new FbxPrefabAutoUpdater.FbxPrefabUtility(fbxInstance.AddComponent<FbxPrefab>()).SetSourceModel(m_fbx);
32-
m_prefabPath = GetRandomPrefabAssetPath();
33-
m_prefab = PrefabUtility.CreatePrefab(m_prefabPath, fbxInstance);
30+
var fbxInstance = PrefabUtility.InstantiatePrefab (m_fbx) as GameObject;
31+
new FbxPrefabAutoUpdater.FbxPrefabUtility (fbxInstance.AddComponent<FbxPrefab> ()).SetSourceModel (m_fbx);
32+
m_prefabPath = GetRandomPrefabAssetPath ();
33+
m_prefab = PrefabUtility.CreatePrefab (m_prefabPath, fbxInstance);
3434
AssetDatabase.Refresh ();
35-
Assert.AreEqual(m_prefabPath, AssetDatabase.GetAssetPath(m_prefab));
36-
GameObject.DestroyImmediate(fbxInstance);
35+
Assert.AreEqual (m_prefabPath, AssetDatabase.GetAssetPath (m_prefab));
36+
GameObject.DestroyImmediate (fbxInstance);
3737
}
3838

3939
[Test]
4040
public void BasicTest ()
4141
{
42-
var fbxPrefabPath = FbxPrefabAutoUpdater.FindFbxPrefabAssetPath();
43-
Assert.IsFalse(string.IsNullOrEmpty(fbxPrefabPath));
44-
Assert.IsTrue(fbxPrefabPath.EndsWith(FbxPrefabAutoUpdater.FBX_PREFAB_FILE));
42+
var fbxPrefabPath = FbxPrefabAutoUpdater.FindFbxPrefabAssetPath ();
43+
Assert.IsFalse (string.IsNullOrEmpty (fbxPrefabPath));
44+
Assert.IsTrue (fbxPrefabPath.EndsWith (FbxPrefabAutoUpdater.FBX_PREFAB_FILE));
4545

46-
Assert.IsTrue(FbxPrefabAutoUpdater.IsFbxAsset("Assets/path/to/foo.fbx"));
47-
Assert.IsFalse(FbxPrefabAutoUpdater.IsFbxAsset("Assets/path/to/foo.png"));
46+
Assert.IsTrue (FbxPrefabAutoUpdater.IsFbxAsset ("Assets/path/to/foo.fbx"));
47+
Assert.IsFalse (FbxPrefabAutoUpdater.IsFbxAsset ("Assets/path/to/foo.png"));
4848

49-
Assert.IsTrue(FbxPrefabAutoUpdater.IsPrefabAsset("Assets/path/to/foo.prefab"));
50-
Assert.IsFalse(FbxPrefabAutoUpdater.IsPrefabAsset("Assets/path/to/foo.fbx"));
51-
Assert.IsFalse(FbxPrefabAutoUpdater.IsPrefabAsset("Assets/path/to/foo.png"));
49+
Assert.IsTrue (FbxPrefabAutoUpdater.IsPrefabAsset ("Assets/path/to/foo.prefab"));
50+
Assert.IsFalse (FbxPrefabAutoUpdater.IsPrefabAsset ("Assets/path/to/foo.fbx"));
51+
Assert.IsFalse (FbxPrefabAutoUpdater.IsPrefabAsset ("Assets/path/to/foo.png"));
5252

53-
var imported = new HashSet<string>( new string [] { "Assets/path/to/foo.fbx", m_fbxPath } );
54-
Assert.IsTrue(FbxPrefabAutoUpdater.MayHaveFbxPrefabToFbxAsset(m_prefabPath, fbxPrefabPath,
53+
var imported = new HashSet<string> (new string [] { "Assets/path/to/foo.fbx", m_fbxPath });
54+
Assert.IsTrue (FbxPrefabAutoUpdater.MayHaveFbxPrefabToFbxAsset (m_prefabPath, fbxPrefabPath,
5555
imported));
5656
}
5757

5858
[Test]
5959
public void RectTransformTest ()
6060
{
61-
Vector3 scaleForward = new Vector3(1,2,3);
62-
Vector3 positionForward = new Vector3(100, 200, 300);
63-
Vector3 rotationForward = new Vector3(1,2,3);
61+
Vector3 scaleForward = new Vector3 (1, 2, 3);
62+
Vector3 positionForward = new Vector3 (100, 200, 300);
63+
Vector3 rotationForward = new Vector3 (1, 2, 3);
6464

65-
Vector3 scaleBackward = new Vector3(3, 2, 1);
66-
Vector3 positionBackward = new Vector3(300, 200, 100);
67-
Vector3 rotationBackward = new Vector3(3,2,1);
65+
Vector3 scaleBackward = new Vector3 (3, 2, 1);
66+
Vector3 positionBackward = new Vector3 (300, 200, 100);
67+
Vector3 rotationBackward = new Vector3 (3, 2, 1);
6868

6969
//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);
70+
var cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
71+
var capsule = GameObject.CreatePrimitive (PrimitiveType.Capsule);
72+
capsule.transform.SetParent (cube.transform);
7773

78-
capsule.transform.parent = cube.transform;
74+
var rectTransform = capsule.AddComponent<RectTransform> ();
7975

80-
string filePath = GetRandomFbxFilePath();
76+
rectTransform.localScale = scaleForward;
77+
rectTransform.localPosition = positionForward;
78+
rectTransform.localRotation = Quaternion.Euler (rotationForward);
79+
#if UNITY_2017_3_OR_NEWER
80+
rectTransform.ForceUpdateRectTransforms ();
81+
#endif
8182

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));
83+
string filePath1 = GetRandomFbxFilePath ();
8984

90-
//Create an "updated" hierarchy
91-
var cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
92-
var capsule2 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
93-
capsule2.AddComponent<RectTransform>();
85+
//instantiate our hierarchy as a prefab
86+
var oldInstance = ConvertToModel.Convert (cube, fbxFullPath: filePath1);
87+
Assert.IsTrue (oldInstance);
9488

95-
capsule2.GetComponent<RectTransform>().localScale = scaleBackward;
96-
capsule2.GetComponent<RectTransform>().localPosition = positionBackward;
97-
capsule2.GetComponent<RectTransform>().localRotation = Quaternion.Euler(rotationBackward);
89+
rectTransform = oldInstance.transform.GetChild (0).GetComponent<RectTransform> ();
9890

99-
capsule2.transform.parent = cube2.transform;
91+
Assert.IsTrue (rectTransform.localScale == scaleForward);
92+
Assert.IsTrue (rectTransform.localPosition == positionForward);
93+
Assert.IsTrue (rectTransform.localRotation == Quaternion.Euler (rotationForward));
10094

95+
//Create an "updated" hierarchy
96+
var cube2 = GameObject.CreatePrimitive (PrimitiveType.Cube);
97+
var capsule2 = GameObject.CreatePrimitive (PrimitiveType.Capsule);
98+
capsule2.transform.SetParent (cube2.transform);
99+
100+
rectTransform = capsule2.AddComponent<RectTransform> ();
101+
102+
rectTransform.localScale = scaleBackward;
103+
rectTransform.localPosition = positionBackward;
104+
rectTransform.localRotation = Quaternion.Euler (rotationBackward);
105+
#if UNITY_2017_3_OR_NEWER
106+
rectTransform.ForceUpdateRectTransforms ();
107+
#endif
101108
//export our updated hierarchy to the same file path as the original
102109
SleepForFileTimestamp();
103-
FbxExporters.Editor.ModelExporter.ExportObject(filePath, cube2);
110+
FbxExporters.Editor.ModelExporter.ExportObject(filePath1, cube2);
104111
AssetDatabase.Refresh();
105112

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));
113+
rectTransform = oldInstance.transform.GetChild (0).GetComponent<RectTransform> ();
114+
Assert.IsTrue(rectTransform.localScale == scaleBackward);
115+
Assert.IsTrue(rectTransform.localPosition == positionBackward);
116+
Assert.IsTrue(rectTransform.localRotation == Quaternion.Euler(rotationBackward));
109117

110118
GameObject.DestroyImmediate(cube);
111119
GameObject.DestroyImmediate(cube2);

0 commit comments

Comments
 (0)