如果我想运行的时候在AB两个图之间插一个图该怎么办?脚本如下所示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using System.Collections;
public class UIMain : MonoBehaviour {
void Start ()
{
GameObject button = GameObject.Instantiate(Resources.Load("button"))as GameObject;
button.transform.parent = transform;
button.transform.localPosition = Vector3.zero;
button.transform.localScale = Vector3.one;
GameObject AObj = transform.Find("A").gameObject;
GameObject BObj = transform.Find("B").gameObject;
button.transform.SetSiblingIndex( AObj.transform.GetSiblingIndex() );
}
}
transform.SetSiblingIndex 和 GetSiblingIndex就是设置与获取 GameObject在兄弟节点的位置。。
设置生成物体在父节点的顺序 GameObject Line = Instantiate( Resources.Load("Prefabs/Pos1"), InsOBJ.transform );
Line.transform.SetSiblingIndex(0); InsOBJ.transform 就是父节点 如果父节点下有很多东西 但是想要将 动态创建的物体放到指定的位置 用到 GmaeObject.transform.SetSiblingIndex(int); 【int是几就为第几个 默认0是第一个】
1.SiblingIndex 从0开始依次向后增加1
2.设置 SiblingIndex 时,如果设置的索引>当前索引 ,原有索引上的元素至当前元素之下的元素一律向上移动(索引-1),就是把下面的通通上移使得目标索引位置空出,然后放入自己。
3.设置 SiblingIndex 时,如果设置的索引