//----------------------
        // Protected Functions

        /// <summary>
        /// Set the parent object base on the
        /// fit screen setting.
        /// </summary>
        protected void SetParentObjectByMode()
        {
            JCS_Canvas jcsCanvas = JCS_Canvas.instance;

            if (jcsCanvas == null)
            {
                JCS_Debug.LogReminders("Does not use JCS_Canvas Object...");
                return;
            }

            Transform parentObject = null;

            // if is Resize UI is enable than add Dialogue under
            // resize ui transform
            if (JCS_UISettings.instance.RESIZE_UI)
            {
                parentObject = jcsCanvas.GetResizeUI().transform;
            }
            // Else we add it directly under the Canvas
            else
            {
                parentObject = jcsCanvas.GetCanvas().transform;
            }

            // set it to parent
            this.gameObject.transform.SetParent(parentObject);
        }
Exemple #2
0
        /// <summary>
        /// Set the parent object base on the fit screen setting.
        /// </summary>
        protected void SetParentObjectByMode()
        {
            var canvas   = JCS_Canvas.GuessCanvas(this.transform);
            var resizeUI = canvas.ResizeUI;

            if (canvas == null)
            {
                JCS_Debug.LogReminder("Doesn't use JCS_Canvas object");
                return;
            }

            Transform parentObject;

            // if is Resize UI is enable than add Dialogue under resize ui transform
            if (JCS_UISettings.instance.RESIZE_UI)
            {
                parentObject = resizeUI.transform;
            }
            // Else we add it directly under the Canvas
            else
            {
                parentObject = canvas.canvas.transform;
            }

            // set it to parent
            this.gameObject.transform.SetParent(parentObject);
        }
Exemple #3
0
        /// <summary>
        /// Create the Game Object during the editing time and
        /// put under JCS_Canvas object.
        /// </summary>
        /// <param name="settingPath"> path to spawn </param>
        /// <returns> object just spawned. </returns>
        private static GameObject CreateHierarchyObjectUnderCanvas(string settingPath)
        {
            // since this will be in the editing time.
            // so we don't worry to much about the performance.
            JCS_Canvas jcsCanvas = (JCS_Canvas)FindObjectOfType(typeof(JCS_Canvas));

            return(CreateHierarchyObjectUnderCanvas(settingPath, jcsCanvas));
        }
Exemple #4
0
        /// <summary>
        /// Start sliding the screen out the scene.
        /// </summary>
        /// <param name="align"> align type </param>
        public void StartSlideOut(JCS_Align align, float time)
        {
            Vector2 sizeDelta = JCS_Canvas.GuessCanvas().AppRect.sizeDelta;

            float imageSize = 1200;
            float distanceX = sizeDelta.x + imageSize;
            float distanceY = sizeDelta.y + imageSize;

            // NOTE(jenchieh): this is just some tweeking.
            mTweener.DurationX = time;
            mTweener.DurationY = time;
            mTweener.DurationZ = time;

            Vector3 tweenTo = this.transform.localPosition;

            switch (align)
            {
            // going left showing from right
            // ============--------------
            case JCS_Align.ALIGN_RIGHT:
            {
                tweenTo.x = JCS_Mathf.ToNegative(distanceX);
            }
            break;

            // going right showing from left
            // -----------=============
            case JCS_Align.ALIGN_LEFT:
            {
                tweenTo.x = JCS_Mathf.ToPositive(distanceX);
            }
            break;

            case JCS_Align.ALIGN_BOTTOM:
            {
                tweenTo.y = JCS_Mathf.ToPositive(distanceY);
            }
            break;

            case JCS_Align.ALIGN_TOP:
            {
                tweenTo.y = JCS_Mathf.ToNegative(distanceY);
            }
            break;
            }

            // start sliding
            mTweener.DoTween(tweenTo);
        }
        public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
        {
            if (trans == null || callback == null)
                return;

            var canvas = JCS_Canvas.GuessCanvas();

            var parent = trans.parent;
            trans.SetParent(canvas.AppRect);

            if (callback != null)
                callback.Invoke(parent);

            trans.SetParent(parent);
        }
Exemple #6
0
        /// <summary>
        /// Transfer canvas space to world space.
        ///
        /// NOTE(jenchieh): this dont often use, cuz world space element
        /// dont often follow the ui element.
        /// </summary>
        /// <param name="targetCanvasPos"> canvas position to transfer. </param>
        /// <returns> world space position </returns>
        public Vector3 CanvasToWorldSpace(Vector2 targetCanvasPos)
        {
            Camera cam = GetCamera();

            //first you need the RectTransform component of your canvas
            RectTransform canvasRect = JCS_Canvas.GuessCanvas().AppRect;

            Vector2 canvasObject_WorldPosition = new Vector2(
                ((targetCanvasPos.x + (canvasRect.sizeDelta.x * JCS_Mathf.T_HALF)) / canvasRect.sizeDelta.x),
                ((targetCanvasPos.y + (canvasRect.sizeDelta.y * JCS_Mathf.T_HALF)) / canvasRect.sizeDelta.y));

            Vector3 worldPos = cam.ViewportToWorldPoint(canvasObject_WorldPosition);

            //now you can set the position to the world element
            return(worldPos);
        }
 private List <JCS_Canvas> SortCanvases_Insertion()
 {
     for (int i = 0; i < mCanvases.Count; ++i)
     {
         for (int j = i; j > 0; --j)
         {
             if (mCanvases[j].canvas.sortingOrder < mCanvases[j - 1].canvas.sortingOrder)
             {
                 JCS_Canvas temp = mCanvases[j];
                 mCanvases[j]     = mCanvases[j - 1];
                 mCanvases[j - 1] = temp;
             }
         }
     }
     return(mCanvases);
 }
Exemple #8
0
        private void Start()
        {
#if (UNITY_5_4_OR_NEWER)
            RectTransform appRect = JCS_Canvas.GuessCanvas().AppRect;

            // TODO(jenchieh): unknown reason that something changes this to
            // somewhere else. (since 5.4.0f3)
            Vector3 tempPos = appRect.localPosition;
            tempPos.z = 0;
            this.transform.localPosition = Vector3.zero;
#endif

            this.transform.localEulerAngles = Vector3.zero;

            this.transform.localScale = Vector3.one;
        }
Exemple #9
0
        private void Start()
        {
            // Set the canvas to be our root.
            this.transform.SetParent(JCS_Canvas.GuessCanvas(transform).transform);

            // Add panel root without losing the original size.
            {
                Vector2 originalSize = mRectTransform.sizeDelta;

                this.mPanelRoot = this.gameObject.AddComponent <JCS_PanelRoot>();

                mRectTransform.sizeDelta = originalSize;
            }

            SetToScreenEdge();
        }
Exemple #10
0
        /// <summary>
        /// Check weather the "type" in the screen space. (Sprite Renderer)
        /// </summary>
        /// <param name="checkTrans"> sprite renderer to check. (Sprite width & height) </param>
        /// <returns>
        /// true: in screen space,
        /// false: not in screen space
        /// </returns>
        public bool CheckInScreenSpace(SpriteRenderer checkTrans)
        {
            Vector2 objectRect = JCS_Util.GetSpriteRendererRect(checkTrans);

            Camera  cam    = main.GetCamera();
            Vector2 objPos = cam.WorldToViewportPoint(checkTrans.transform.position);
            Vector2 camPos = cam.WorldToViewportPoint(cam.transform.position);

            float objLeft  = objPos.x - (objectRect.x / JCS_Mathf.D_HALF);
            float objRight = objPos.x + (objectRect.x / JCS_Mathf.D_HALF);
            float objTop   = objPos.y + (objectRect.y / JCS_Mathf.D_HALF);
            float objBot   = objPos.y - (objectRect.y / JCS_Mathf.D_HALF);

            RectTransform appRect = JCS_Canvas.GuessCanvas().AppRect;

            float camWidth  = appRect.sizeDelta.x;
            float camHeight = appRect.sizeDelta.y;

            float camLeft  = camPos.x - (camWidth / JCS_Mathf.D_HALF);
            float camRight = camPos.x + (camWidth / JCS_Mathf.D_HALF);
            float camTop   = camPos.y + (camHeight / JCS_Mathf.D_HALF);
            float camBot   = camPos.y - (camHeight / JCS_Mathf.D_HALF);

#if (UNITY_EDITOR)
            Vector3 topLeft  = new Vector3(objLeft, objTop, 0.0f);
            Vector3 topRight = new Vector3(objRight, objTop, 0.0f);
            Vector3 botRight = new Vector3(objRight, objBot, 0.0f);
            Vector3 botLeft  = new Vector3(objLeft, objBot, 0.0f);

            Debug.DrawLine(topLeft, topRight);
            Debug.DrawLine(topLeft, botLeft);
            Debug.DrawLine(botRight, botLeft);
            Debug.DrawLine(topRight, botRight);
#endif

            // TODO(JenChieh): Not done.

            if ((objRight < camLeft || objLeft > camRight) &&
                (objTop < camBot || objBot > camTop))
            {
                // out of screen.
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Create a tween panel.
        /// </summary>
        private static GameObject CreateTweenPanel()
        {
            JCS_Canvas jcsCanvas = (JCS_Canvas)FindObjectOfType(typeof(JCS_Canvas));

            if (jcsCanvas == null)
            {
                JCS_Debug.Log(
                    "Cannot find the JCS_Canvas in the hierarchy. Plz create the canvas before create the base panel.");
                return(null);
            }

            string     setting_path = "JCSUnity_Resources/GUI/JCS_TweenPanel";
            GameObject tweenPanel   = CreateHierarchyObjectUnderCanvas(setting_path);

            Undo.RegisterCreatedObjectUndo(tweenPanel, "Create Tween Panel");

            return(tweenPanel);
        }
        /// <summary>
        /// Check to see if enable to spawn the dialogue or not
        /// </summary>
        /// <returns></returns>
        private static bool CheckIfOkayToSpawnDialogue(JCS_DialogueType type)
        {
            // Force Diaglogue have higher priority,
            // so it will block the lower priority dialogue type
            if (JCS_UIManager.instance.GetJCSDialogue(type) != null)
            {
                JCS_Debug.LogError("(" + type.ToString() + ")No able to spawn Dialogue cuz there are multiple dialogue in the scene...");
                return(false);
            }

            if (JCS_Canvas.GuessCanvas() == null)
            {
                JCS_Debug.LogError("No able to spawn Dialogue cuz Canvas are null...");
                return(false);
            }

            return(true);
        }
Exemple #13
0
        /// <summary>
        /// Create the clean base gui panel for JCSUnity and add in under to
        /// the canvas.
        ///
        /// Need:
        ///     1) JCS_Canvas
        /// in the scene before create base panel.
        /// </summary>
        private static GameObject CreateBasePanel()
        {
            JCS_Canvas canvas = (JCS_Canvas)FindObjectOfType(typeof(JCS_Canvas));

            if (canvas == null)
            {
                JCS_Debug.Log("Can't find JCS_Canvas in hierarchy. Plz create canvas before creating new panel.");
                return(null);
            }

            const string setting_path = "UI/JCS_BasePanel";
            GameObject   basePanel    = CreateHierarchyObjectUnderCanvas(setting_path);

            Undo.RegisterCreatedObjectUndo(basePanel, "Create Base Panel");

            basePanel.transform.localScale = Vector3.one;
            basePanel.name = "_BasePanel (Created)";

            return(basePanel);
        }
Exemple #14
0
        /// <summary>
        /// Create a tween panel.
        /// </summary>
        private static GameObject CreateTweenPanel()
        {
            JCS_Canvas jcsCanvas = (JCS_Canvas)FindObjectOfType(typeof(JCS_Canvas));

            if (jcsCanvas == null)
            {
                JCS_Debug.Log("Can't find JCS_Canvas in hierarchy. Plz create canvas before creating new panel.");
                return(null);
            }

            const string setting_path = "JCSUnity_Resources/GUI/JCS_TweenPanel";
            GameObject   tweenPanel   = CreateHierarchyObjectUnderCanvas(setting_path);

            Undo.RegisterCreatedObjectUndo(tweenPanel, "Create Tween Panel");

            tweenPanel.transform.localScale = Vector3.one;
            tweenPanel.name = "_TweenPanel (Created)";

            return(tweenPanel);
        }
        /// <summary>
        /// Create the clean base gui panel for JCSUnity
        /// and add in under to the canvas.
        ///
        /// Need:
        ///     1) JCS_Canvas
        /// in the scene before create base panel.
        /// </summary>
        private static GameObject CreateBaseGUIPanel()
        {
            JCS_Canvas jcsCanvas = (JCS_Canvas)FindObjectOfType(typeof(JCS_Canvas));

            if (jcsCanvas == null)
            {
                JCS_Debug.Log(
                    "Cannot find the JCS_Canvas in the hierarchy. Plz create the canvas before create the base panel.");

                return(null);
            }

            string     setting_path = "JCSUnity_Resources/GUI/JCS_BasePanel";
            GameObject basePanel    = CreateHierarchyObjectUnderCanvas(setting_path);

            Undo.RegisterCreatedObjectUndo(basePanel, "Create Base GUI Panel");

            basePanel.transform.localScale = Vector3.one;

            return(basePanel);
        }
        public static List<RectTransform> DetachChildren(RectTransform trans)
        {
            var childs = new List<RectTransform>();

            var canvas = JCS_Canvas.GuessCanvas();

            for (int index = 0; index < trans.childCount; ++index)
            {
                Transform child = trans.GetChild(index);
                var rect = child.GetComponent<RectTransform>();

                if (rect == null)
                    continue;

                childs.Add(rect);

                // Remove from parent.
                child.SetParent(canvas.AppRect);
            }

            return childs;
        }
Exemple #17
0
        /// <summary>
        /// Make a canvas space object to a world space position.
        ///
        /// NOTE(jenchieh): Make UI object (canvas space) on top of the
        /// world space game object.
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public Vector3 CastToScreen(Vector3 pos)
        {
            var camera = JCS_Camera.main;

            Vector3 positionOffset = mPositionOffset;

            if (mPanelRoot != null)
            {
                positionOffset.x *= mPanelRoot.PanelDeltaWidthRatio;
                positionOffset.y *= mPanelRoot.PanelDeltaHeightRatio;
            }

            switch (GetObjectType())
            {
            case JCS_UnityObjectType.TEXT:
            case JCS_UnityObjectType.UI:
            {
                Vector2 worldToCanvasSpace = camera.WorldToCanvasSpace(pos);

                var canvas   = JCS_Canvas.GuessCanvas();
                var resizeUI = canvas.ResizeUI;

                float targetScale = resizeUI.TargetScale;

                if (targetScale != 0.0f)
                {
                    worldToCanvasSpace.x /= resizeUI.TargetScale;
                    worldToCanvasSpace.y /= resizeUI.TargetScale;
                }

                this.LocalPosition = worldToCanvasSpace + (Vector2)positionOffset;
            }
            break;
            }

            return(this.LocalPosition);
        }
Exemple #18
0
        /// <summary>
        /// Check weather the "type" in the screen space. (RectTransform)
        /// </summary>
        /// <param name="checkTrans"> rect transform's size, etc. </param>
        /// <returns>
        /// true: in screen space,
        /// false: not in screen space
        /// </returns>
        public bool CheckInScreenSpace(RectTransform checkTrans)
        {
            Vector2 rectSize = checkTrans.sizeDelta;
            Vector3 panelPos = checkTrans.localPosition;

            float halfSlotWidth  = rectSize.x / JCS_Mathf.D_HALF * checkTrans.localScale.x;
            float halfSlotHeight = rectSize.y / JCS_Mathf.D_HALF * checkTrans.localScale.y;

            float panelLeftBorder   = panelPos.x - halfSlotWidth;
            float panelRightBorder  = panelPos.x + halfSlotWidth;
            float panelTopBorder    = panelPos.y + halfSlotHeight;
            float panelBottomBorder = panelPos.y - halfSlotHeight;

            Camera  cam    = main.GetCamera();
            Vector3 camPos = cam.transform.position;
            // Transfer 3D space to 2D space
            Vector2 camPosToScreen = cam.WorldToScreenPoint(camPos);

            // Get application rect
            RectTransform appRect    = JCS_Canvas.GuessCanvas().AppRect;
            Vector2       screenRect = appRect.sizeDelta;

            float camLeftBorder   = camPosToScreen.x - screenRect.x / JCS_Mathf.D_HALF;
            float camRightBorder  = camPosToScreen.x + screenRect.x / JCS_Mathf.D_HALF;
            float camTopBorder    = camPosToScreen.y + screenRect.y / JCS_Mathf.D_HALF;
            float camBottomBorder = camPosToScreen.y - screenRect.y / JCS_Mathf.D_HALF;

            if (panelRightBorder - rectSize.x > camRightBorder ||
                panelLeftBorder + rectSize.x < camLeftBorder ||
                panelTopBorder - rectSize.y > camTopBorder ||
                panelBottomBorder + rectSize.y < camBottomBorder)
            {
                return(false);
            }

            return(true);
        }
Exemple #19
0
        private Vector3 GameDepthRect(Vector3 depth)
        {
            // Next step: find camera 4 bounds.
            Camera cam = main.GetCamera();

            var canvas = JCS_Canvas.GuessCanvas();

            Vector3 camPos = cam.transform.position;
            // only need to know the depth.
            {
                camPos.x = 0.0f;
                camPos.y = 0.0f;
            }
            Vector3 canvasPos = canvas.transform.position;
            // only need to know the depth.
            {
                canvasPos.x = 0.0f;
                canvasPos.y = 0.0f;
            }
            float camToCanvasDistance = Vector3.Distance(camPos, canvasPos);

            float camToGameDepthDistance = Vector3.Distance(camPos, depth);

            RectTransform appRect    = canvas.AppRect;
            Vector2       canvasRect = appRect.sizeDelta;

            // transfer rect from screen space to world space
            {
                canvasRect.x *= appRect.localScale.x;
                canvasRect.y *= appRect.localScale.y;
            }

            return(new Vector3(
                       camToGameDepthDistance * canvasRect.x / camToCanvasDistance,
                       camToGameDepthDistance * canvasRect.y / camToCanvasDistance,
                       0));
        }
Exemple #20
0
        /// <summary>
        /// Transfer world space to canvas space.
        /// Source -> http://answers.unity3d.com/questions/799616/unity-46-beta-19-how-to-convert-from-world-space-t.html
        /// </summary>
        /// <param name="targetWorldPos"> world position to transfer. </param>
        /// <returns> Canvas space position </returns>
        public Vector2 WorldToCanvasSpace(Vector3 targetWorldPos)
        {
            Camera cam = GetCamera();

            //first you need the RectTransform component of your canvas
            RectTransform canvasRect = JCS_Canvas.GuessCanvas().AppRect;

            //then you calculate the position of the UI element

            // 0,0 for the canvas is at the center of the screen,
            // whereas WorldToViewPortPoint treats
            // the lower left corner as 0,0. Because of
            // this, you need to subtract the height / width of
            // the canvas * 0.5 to get the correct position.

            Vector3 viewportPos = cam.WorldToViewportPoint(targetWorldPos);

            Vector2 worldObject_ScreenPosition = new Vector2(
                ((viewportPos.x * canvasRect.sizeDelta.x) - (canvasRect.sizeDelta.x * JCS_Mathf.T_HALF)),
                ((viewportPos.y * canvasRect.sizeDelta.y) - (canvasRect.sizeDelta.y * JCS_Mathf.T_HALF)));

            //now you can set the position to the ui element
            return(worldObject_ScreenPosition);
        }
Exemple #21
0
        /// <summary>
        /// Check weather the "type" in the screen space. (Character Controller)
        /// </summary>
        /// <param name="cap"> character controller to check. (Collider) </param>
        /// <returns>
        /// true: in screen space,
        /// false: not in screen space
        /// </returns>
        public bool CheckInScreenSpace(CharacterController cap)
        {
            // First Step: Find CharacterController's 4 bounds.
            Vector3 capScale = cap.transform.localScale;

            capScale = JCS_Mathf.AbsoluteValue(capScale);

            Vector3 cCenter = new Vector3(
                cap.center.x * capScale.x,
                cap.center.y * capScale.y,
                cap.center.z * capScale.z);

            float cR = cap.radius * capScale.x;
            float cH = (cap.height - (cap.radius * 2.0f)) * capScale.y;

            if (cH < 0)
            {
                cH = 0;
            }

            float cTopBound   = cap.transform.position.y + cCenter.y + (cH / 2.0f) + cR;
            float cBotBound   = cap.transform.position.y + cCenter.y - (cH / 2.0f) - cR;
            float cRightBound = cap.transform.position.x + cCenter.x + cR;
            float cLeftBound  = cap.transform.position.x + cCenter.x - cR;

#if (UNITY_EDITOR)
            Vector3 cOrigin = cap.transform.position + cCenter;
            Debug.DrawLine(cOrigin,
                           new Vector3(cOrigin.x, cTopBound, cOrigin.z));
            Debug.DrawLine(cOrigin,
                           new Vector3(cOrigin.x, cBotBound, cOrigin.z));
            Debug.DrawLine(cOrigin,
                           new Vector3(cRightBound, cOrigin.y, cOrigin.z));
            Debug.DrawLine(cOrigin,
                           new Vector3(cLeftBound, cOrigin.y, cOrigin.z));
#endif

            // Next step: find camera 4 bounds.
            Camera cam = JCS_Camera.main.GetCamera();

            JCS_Canvas jcsCanvas = JCS_Canvas.instance;

            Vector3 camPos = cam.transform.position;
            // only need to know the depth.
            {
                camPos.x = 0;
                camPos.y = 0;
            }
            Vector3 canvasPos = jcsCanvas.transform.position;
            // only need to know the depth.
            {
                canvasPos.x = 0;
                canvasPos.y = 0;
            }
            float camToCanvasDistance = Vector3.Distance(camPos, canvasPos);

            Vector3 gameDepth = new Vector3(0, cap.transform.position.z, 0);
            float   camToGameDepthDistance = Vector3.Distance(camPos, gameDepth);

            //print("To Game depth Distance: " + camToGameDepthDistance);
            //print("To Cavas Distnace: " + camToCanvasDistance);

            Vector2 canvasRect = jcsCanvas.GetAppRect().sizeDelta;
            // transfer rect from screen space to world space
            {
                canvasRect.x *= jcsCanvas.GetAppRect().localScale.x;
                canvasRect.y *= jcsCanvas.GetAppRect().localScale.y;
            }

            Vector3 gameRect = new Vector3(
                camToGameDepthDistance * canvasRect.x / camToCanvasDistance,
                camToGameDepthDistance * canvasRect.y / camToCanvasDistance,
                0);

            // camPos name are named up there.
            // cannot name the same.
            Vector3 cCamPos = cam.transform.position;

            float camTopBound   = cCamPos.y + gameRect.y / 2;
            float camBotBound   = cCamPos.y - gameRect.y / 2;
            float camRightBound = cCamPos.x + gameRect.x / 2;
            float camLeftBound  = cCamPos.x - gameRect.x / 2;

#if (UNITY_EDITOR)
            Vector3 topLeft = cam.transform.position;
            topLeft.x -= gameRect.x / 2;
            topLeft.y += gameRect.y / 2;

            Vector3 topRight = cam.transform.position;
            topRight.x += gameRect.x / 2;
            topRight.y += gameRect.y / 2;

            Vector3 botLeft = cam.transform.position;
            botLeft.x -= gameRect.x / 2;
            botLeft.y -= gameRect.y / 2;

            Vector3 botRight = cam.transform.position;
            botRight.x += gameRect.x / 2;
            botRight.y -= gameRect.y / 2;

            // set depth to the same
            topLeft.z  = mGameDepth;
            topRight.z = mGameDepth;
            botLeft.z  = mGameDepth;
            botRight.z = mGameDepth;

            // Draw the box
            JCS_Debug.DrawRect(topLeft, topRight, botRight, botLeft);
#endif

            if (cRightBound < camLeftBound ||
                camRightBound < cLeftBound ||
                cTopBound < camBotBound ||
                camTopBound < cBotBound)
            {
                // no in the screen
                return(false);
            }

            // in screen
            return(true);
        }
Exemple #22
0
        /// <summary>
        /// Square inside the game editor screen. Display
        /// the screen width and height in current game depth.
        /// 2D Game probably need this. 3D Game is optional.
        /// </summary>
        private void DisplayGameDepthCamera()
        {
            if (!mDisplayGameDepthCamera)
            {
                return;
            }

            // Next step: find camera 4 bounds.
            Camera cam = JCS_Camera.main.GetCamera();

            JCS_Canvas jcsCanvas = JCS_Canvas.instance;

            Vector3 camPos = cam.transform.position;
            // only need to know the depth.
            {
                camPos.x = 0;
                camPos.y = 0;
            }
            Vector3 canvasPos = JCS_Canvas.instance.transform.position;
            // only need to know the depth.
            {
                canvasPos.x = 0;
                canvasPos.y = 0;
            }
            float camToCanvasDistance = Vector3.Distance(camPos, canvasPos);

            Vector3 gameDepth = new Vector3(0, mGameDepth, 0);
            float   camToGameDepthDistance = Vector3.Distance(camPos, gameDepth);

            //print("To Game depth Distance: " + camToGameDepthDistance);
            //print("To Cavas Distnace: " + camToCanvasDistance);

            Vector2 canvasRect = jcsCanvas.GetAppRect().sizeDelta;

            // transfer rect from screen space to world space
            {
                canvasRect.x *= jcsCanvas.GetAppRect().localScale.x;
                canvasRect.y *= jcsCanvas.GetAppRect().localScale.y;
            }

            mCamRectSize = new Vector3(
                camToGameDepthDistance * canvasRect.x / camToCanvasDistance,
                camToGameDepthDistance * canvasRect.y / camToCanvasDistance,
                0);

            // camPos name are named up there.
            // cannot name the same.
            Vector3 cCamPos = cam.transform.position;

            float camTopBound   = cCamPos.y + mCamRectSize.y / 2;
            float camBotBound   = cCamPos.y - mCamRectSize.y / 2;
            float camRightBound = cCamPos.x + mCamRectSize.x / 2;
            float camLeftBound  = cCamPos.x - mCamRectSize.x / 2;

            // top left -> bot right
            mCamRect.x      = camLeftBound;
            mCamRect.y      = camTopBound;
            mCamRect.width  = camRightBound;
            mCamRect.height = camBotBound;

            Vector3 topLeft = cam.transform.position;

            topLeft.x -= mCamRectSize.x / 2;
            topLeft.y += mCamRectSize.y / 2;

            Vector3 topRight = cam.transform.position;

            topRight.x += mCamRectSize.x / 2;
            topRight.y += mCamRectSize.y / 2;

            Vector3 botLeft = cam.transform.position;

            botLeft.x -= mCamRectSize.x / 2;
            botLeft.y -= mCamRectSize.y / 2;

            Vector3 botRight = cam.transform.position;

            botRight.x += mCamRectSize.x / 2;
            botRight.y -= mCamRectSize.y / 2;

            // set depth to the same
            topLeft.z  = mGameDepth;
            topRight.z = mGameDepth;
            botLeft.z  = mGameDepth;
            botRight.z = mGameDepth;

            // Draw the box
            JCS_Debug.DrawRect(topLeft, topRight, botRight, botLeft, mGameCamColor);
        }
 /// <summary>
 /// Add a canvas to the group.
 /// </summary>
 public void AddCanvas(JCS_Canvas canvas)
 {
     this.mCanvases.Add(canvas);
     mCanvases = JCS_Util.RemoveEmptySlotIncludeMissing(mCanvases);
     mCanvases = SortCanvases_Insertion();
 }
Exemple #24
0
        /* Functions */

        private void Awake()
        {
            if (instance != null)
            {
                string black_screen_name = JCS_UISettings.BLACK_SCREEN_NAME;
                string white_screen_name = JCS_UISettings.WHITE_SCREEN_NAME;

                // cuz the transform list will change while we set the transform to
                // the transform,
                List <Transform> readyToSetList = new List <Transform>();

                Transform tempTrans = instance.transform;
                // so record all the transform
                for (int index = 0; index < tempTrans.childCount; ++index)
                {
                    Transform child = tempTrans.GetChild(index);
                    if (child.name == black_screen_name ||
                        child.name == (black_screen_name + "(Clone)"))
                    {
                        continue;
                    }

                    if (child.name == white_screen_name ||
                        child.name == (white_screen_name + "(Clone)"))
                    {
                        continue;
                    }

                    if (child.name == "JCS_IgnorePanel")
                    {
                        continue;
                    }

                    // TODO(JenChieh): optimize this?
                    if (child.GetComponent <JCS_IgnoreDialogueObject>() != null)
                    {
                        continue;
                    }

                    // add to set list ready to set to the new transform as parent
                    readyToSetList.Add(child);
                }

                // set to the new transform
                foreach (Transform trans in readyToSetList)
                {
                    // set parent to the new canvas in the new scene
                    trans.SetParent(this.transform);
                }

                // Delete the old one
                DestroyImmediate(instance.gameObject);
            }


            // attach the new one
            instance = this;

            this.mAppRect = this.GetComponent <RectTransform>();
            this.mCanvas  = this.GetComponent <Canvas>();

            if (JCS_UISettings.instance.RESIZE_UI)
            {
                // resizable UI in order to resize the UI correctly
                JCS_ResizeUI rui = JCS_Utility.SpawnGameObject(RESIZE_UI_PATH).GetComponent <JCS_ResizeUI>();
                rui.transform.SetParent(this.transform);
            }
        }
Exemple #25
0
        /// <summary>
        /// Create the Game Object during the editing time and
        /// put under JCS_Canvas object.
        ///
        /// Save O(n) time complexity during editing time.
        /// </summary>
        /// <param name="settingPath"> path to spawn </param>
        /// <param name="jcsCanvas"> canvas to set on. </param>
        /// <returns> object just spawned. </returns>
        private static GameObject CreateHierarchyObjectUnderCanvas(string settingPath, JCS_Canvas jcsCanvas)
        {
            if (jcsCanvas == null)
            {
                JCS_Debug.Log("Can't find JCS_Canvas in hierarchy. Plz create canvas before creating new panel.");
                return(null);
            }

            // spawn the object first.
            GameObject hierarchyObj = CreateHierarchyObject(settingPath);

            // set the canvas as parent.
            hierarchyObj.transform.SetParent(jcsCanvas.transform);

            // init position.
            hierarchyObj.transform.localPosition = Vector3.zero;

            return(hierarchyObj);
        }
Exemple #26
0
        /// <summary>
        /// Create 9x9 slide panel.
        ///
        /// Need:
        ///     1) JCS_Camera
        ///     2) JCS_Canvas
        /// in the scene before create 9 x 9 slide panel.
        /// </summary>
        private static void CreateSlidePanel()
        {
            JCS_Canvas jcsCanvas = (JCS_Canvas)FindObjectOfType(typeof(JCS_Canvas));

            if (jcsCanvas == null)
            {
                JCS_Debug.Log("Can't find JCS_Canvas in hierarchy. Plz create canvas before creating new panel.");
                return;
            }

            // find the camera in the scene.
            JCS_2DCamera cam = (JCS_2DCamera)FindObjectOfType(typeof(JCS_Camera));

            if (cam == null)
            {
                JCS_Debug.Log("Can't find JCS_Canvas in hierarchy. Plz create canvas before creating new panel.");
                return;
            }

            const string settingPath = "JCSUnity_Resources/LevelDesignUI/JCS_SlideScreenPanelHolder";

            // spawn the pane holder.
            JCS_SlideScreenPanelHolder panelHolder9x9 = CreateHierarchyObjectUnderCanvas(settingPath, jcsCanvas).GetComponent <JCS_SlideScreenPanelHolder>();

            // create the array of panel.
            panelHolder9x9.slidePanels = new RectTransform[9];

            int starting_pos_x = -1920;
            int starting_pos_y = 1080;

            const string slidePanelPath = "JCSUnity_Resources/LevelDesignUI/JCS_SlidePanel";

            int index = 0;

            // create all nine panel and assign to the slide panel.
            for (int row = 0; row < 3; ++row)
            {
                for (int column = 0; column < 3; ++column)
                {
                    // get the rect transform from the slide panel object.
                    RectTransform slidePanel = CreateHierarchyObjectUnderCanvas(slidePanelPath, jcsCanvas).GetComponent <RectTransform>();

                    // set the position into 9x9.
                    Vector3 slidePanelNewPos = slidePanel.localPosition;
                    slidePanelNewPos.x       = starting_pos_x - (starting_pos_x * column);
                    slidePanelNewPos.y       = starting_pos_y - (starting_pos_y * row);
                    slidePanel.localPosition = slidePanelNewPos;

                    // set scale to one.
                    slidePanel.localScale = Vector3.one;

                    Image panelImage = slidePanel.GetComponent <Image>();
                    if (panelImage != null)
                    {
                        panelImage.color = JCS_Random.RandomColor();
                    }

                    // assign to slide panel holder.
                    panelHolder9x9.slidePanels[index] = slidePanel;

                    ++index;
                }
            }

            const string            slideScreenCameraPath = "JCSUnity_Resources/Camera/JCS_2DSlideScreenCamera";
            JCS_2DSlideScreenCamera slideScreenCamera     = CreateHierarchyObject(slideScreenCameraPath).GetComponent <JCS_2DSlideScreenCamera>();

            Undo.RegisterCreatedObjectUndo(slideScreenCamera, "Create 2D Slide Screen Camera");

            slideScreenCamera.name = "_2DSlideScreenCamera (Created)";

            // set the panel holder.
            slideScreenCamera.PanelHolder = panelHolder9x9;

            slideScreenCamera.SetJCS2DCamera(cam);

            // set to default 2d.
            slideScreenCamera.UnityGUIType = JCS_UnityGUIType.uGUI_2D;
        }