Unity 3D 根据子节点实现LayOut Group

Unity 3D 手动实现LayOut Group

Unity 3D提供的Layout Group有三种,分别为Grid Layout Group、Vertical Layout Group和Horizontal Layout Group。这些要么垂直,要么水平的,无法根据自己的子节点为进行变换,如:需要一个弧形的Layout效果,这三个Layout就不能满足了。下面介绍一种根据子节点的位置进行Layout。
将下面代码绑定到父节点

   #region  Attributes and Properties


        public RectTransform[] m_childRectTrList;
        private List m_childPosition;
        private int m_cout = 0;
        #endregion


        #region Engine Methods
        protected void Awake()
        {
            m_childPosition = new List();
            foreach (RectTransform childRectTr in m_childRectTrList)
            {
                m_childPosition.Add(childRectTr.position);
            }
        }
        #endregion


        #region Public Methods
        protected void FixedUpdate()
        {
            m_cout = 0;
            for(int i=0; i< m_childRectTrList.Length; i++)
            {
                if(m_childRectTrList[i].gameObject.activeSelf)
                {
                    m_childRectTrList[i].position = m_childPosition[m_cout];
                    m_cout++;
                }
            }
        }
        #endregion
  • 首先将子节点存入m_childRectTrList中;
  • 将m_childRectTrList中的Position位置存入一个List中m_childPosition;

你可能感兴趣的:(Unity3D)