Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
ryanmeasel committed Aug 5, 2015
commit 34a9a7fc67bac321ac9aed819de677d663d93ee0
18 changes: 15 additions & 3 deletions Assets/ObjectPool/Scripts/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RectTransform> () == null) ? true : false;
trans.SetParent (parent, worldPositionStays);
}
trans.localPosition = position;
trans.localRotation = rotation;
obj.SetActive(true);
Expand All @@ -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<RectTransform> () == null) ? true : false;
trans.SetParent (parent, worldPositionStays);
}
trans.localPosition = position;
trans.localRotation = rotation;
instance.spawnedObjects.Add(obj, prefab);
Expand All @@ -138,7 +146,11 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi
{
obj = (GameObject)Object.Instantiate(prefab);
trans = obj.GetComponent<Transform>();
trans.parent = parent;
if (parent)
{
bool worldPositionStays = (parent.GetComponent<RectTransform> () == null) ? true : false;
trans.SetParent (parent, worldPositionStays);
}
trans.localPosition = position;
trans.localRotation = rotation;
return obj;
Expand Down