From 34a9a7fc67bac321ac9aed819de677d663d93ee0 Mon Sep 17 00:00:00 2001 From: Ryan Measel Date: Tue, 4 Aug 2015 22:40:32 -0700 Subject: [PATCH] Added support for RectTransform - RectTransforms (introduced in Unity 4.6 as part of the new UI framework) throw a warning when you set their parent via the transform property: > "Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues." - To circumvent this warning, we check if the parent has a RectTransform component and set the boolean `worldPositionStays` accordingly. - Before calling `GetComponent` on the parent, the parent must be checked for existence. --- Assets/ObjectPool/Scripts/ObjectPool.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index fba6c17..a67f6b0 100755 --- a/Assets/ObjectPool/Scripts/ObjectPool.cs +++ b/Assets/ObjectPool/Scripts/ObjectPool.cs @@ -118,7 +118,11 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi if (obj != null) { trans = obj.transform; - trans.parent = parent; + if (parent) + { + bool worldPositionStays = (parent.GetComponent () == null) ? true : false; + trans.SetParent (parent, worldPositionStays); + } trans.localPosition = position; trans.localRotation = rotation; obj.SetActive(true); @@ -128,7 +132,11 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi } obj = (GameObject)Object.Instantiate(prefab); trans = obj.transform; - trans.parent = parent; + if (parent) + { + bool worldPositionStays = (parent.GetComponent () == null) ? true : false; + trans.SetParent (parent, worldPositionStays); + } trans.localPosition = position; trans.localRotation = rotation; instance.spawnedObjects.Add(obj, prefab); @@ -138,7 +146,11 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi { obj = (GameObject)Object.Instantiate(prefab); trans = obj.GetComponent(); - trans.parent = parent; + if (parent) + { + bool worldPositionStays = (parent.GetComponent () == null) ? true : false; + trans.SetParent (parent, worldPositionStays); + } trans.localPosition = position; trans.localRotation = rotation; return obj;