Пример #1
0
        /// <summary>
        /// Fit screen size base on Unity Engine architecture.
        /// </summary>
        /// <param name="xRatio"></param>
        /// <param name="yRatio"></param>
        public void FitPerfectSize(float xRatio, float yRatio)
        {
            /*
             * NOTE(jenchieh):
             * Cool, `sizeDelta' will actually change the `localPosition'
             * now since version 2017.4.
             *
             * So we set the `sizeDelta' (width and height) first, then
             * set the `localPosition'.
             */

            /* Do the scale. */
            {
                List <Transform> childs = null;
                if (!mIsUnityDefinedUI)
                {
                    // NOTE: If not the Unity define UI, we need to
                    // dettach all the child transform before we can
                    // resize it. If we resize it without dettach all
                    // child transforms, the children transform will
                    // also be scaled/changed.
                    //
                    // 這個有點暴力解法... 不知道為什麼Unity沒有辦法
                    // 在初始化階段一次清乾淨.
                    childs = JCS_Utility.ForceDetachChildren(this.mRectTransform);
                }

                Vector3 newScale = mRectTransform.localScale;
                newScale.x /= xRatio;
                newScale.y /= yRatio;
                mRectTransform.localScale = newScale;

                if (!mIsUnityDefinedUI)
                {
                    // NOTE: Reattach all the previous child.
                    JCS_Utility.AttachChildren(this.mRectTransform, childs);
                }
            }

            /* Do the position. */
            {
                Vector3 newPosition = mRectTransform.localPosition;
                newPosition.x /= xRatio;
                newPosition.y /= yRatio;

                // set to the new position
                mRectTransform.localPosition = newPosition;
            }
        }