Unity-ScrollRect下实例化的物体与原有的物体移动幅度不一致

项目中有个需求要实现地图滚动,所以使用的ScrollRect方法,然后把新增的物体实例化进去,结果发现实例化的物体与原物体跟随背景移动的幅度不一致。

 研究后发现原来是实例化物体时设置的属性有问题:

错误示范1:

      GameObject g = Instantiate(gObj);
        go.transform.parent = parent;
        go.transform.localPosition = parent.localPosition;
        go.transform.localScale = parent.localScale;

错误示范2:

      GameObject g = Instantiate(gObj,Vector3.zero,Quaternion.identity);
        go.transform.parent = parent;
        go.transform.localScale = parent.localScale;

正确示范:

GameObject g = Instantiate(gObj);
g.transform.SetParent(gParent.transform);
g.transform.localPosition = Vector3.zero;
g.transform.localScale = Vector3.one;

 

你可能感兴趣的:(Unity)