今天在搞东西的时候发现一个问题,我动态实例化预制的东西无法改变位置,之前是可以的,这次只是移到UI上面而已,就失效了。查了很久,没找到原因,后来有人说用NGUITOOLS.AddChild就行了,试了下,发现确实可以了,但是想不明白什么原因,就看了下这个函数的实现,如下:
static public GameObject AddChild (GameObject parent, GameObject prefab)
{
GameObject go = GameObject.Instantiate(prefab) as GameObject;
#if UNITY_EDITOR
UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Create Object");
#endif
if (go != null && parent != null)
{
Transform t = go.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
go.layer = parent.layer;
}
return go;
}
发现和我写的代码唯一不同的就是没有设parent,怀疑是这个问题,但是没懂为什么不设置parent会导致设置position无效,如果有知道的朋友,请留言告知我下,谢谢