예제 #1
0
        // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
        static public GameObject GetOrCreateCanvasGameObject()
        {
            GameObject selectedGo = Selection.activeGameObject;

            // Try to find a gameobject that is the selected GO or one if its parents.
            IMCCanvasScaler canvas = (selectedGo != null) ? selectedGo.GetComponentInParent <IMCCanvasScaler>() : null;

            if (canvas != null && canvas.gameObject.activeInHierarchy)
            {
                return(canvas.gameObject);
            }

            // No canvas in selection or its parents? Then use just any canvas..
            canvas = Object.FindObjectOfType(typeof(IMCCanvasScaler)) as IMCCanvasScaler;
            if (canvas != null && canvas.gameObject.activeInHierarchy)
            {
                return(canvas.gameObject);
            }

            // No canvas in the scene at all? Then create a new one.
            return(CreateNewUI());
        }
예제 #2
0
        public override void OnInspectorGUI()
        {
            if (s_Styles == null)
            {
                s_Styles = new Styles();
            }

            bool allAreRoot       = true;
            bool showWorldDiffers = false;
            bool showWorld        = ((target as IMCCanvasScaler).GetComponent <Canvas>().renderMode == RenderMode.WorldSpace);

            for (int i = 0; i < targets.Length; i++)
            {
                IMCCanvasScaler scaler = targets[i] as IMCCanvasScaler;
                Canvas          canvas = scaler.GetComponent <Canvas>();
                if (!canvas.isRootCanvas)
                {
                    allAreRoot = false;
                    break;
                }
                if (showWorld && canvas.renderMode != RenderMode.WorldSpace || !showWorld && canvas.renderMode == RenderMode.WorldSpace)
                {
                    showWorldDiffers = true;
                    break;
                }
            }

            if (!allAreRoot)
            {
                EditorGUILayout.HelpBox("Non-root Canvases will not be scaled.", MessageType.Warning);
                return;
            }

            serializedObject.Update();

            EditorGUI.showMixedValue = showWorldDiffers;
            EditorGUI.BeginDisabledGroup(showWorld || showWorldDiffers);
            if (showWorld || showWorldDiffers)
            {
                EditorGUILayout.Popup(m_UiScaleMode.displayName, 0, new string[] { "World" });
            }
            else
            {
                EditorGUILayout.PropertyField(m_UiScaleMode);
            }
            EditorGUI.EndDisabledGroup();
            EditorGUI.showMixedValue = false;

            if (!showWorldDiffers && !(!showWorld && m_UiScaleMode.hasMultipleDifferentValues))
            {
                EditorGUILayout.Space();

                // World Canvas
                if (showWorld)
                {
                    EditorGUILayout.PropertyField(m_DynamicPixelsPerUnit);
                }
                // Constant pixel size
                else if (m_UiScaleMode.enumValueIndex == (int)IMCCanvasScaler.ScaleMode.ConstantPixelSize)
                {
                    EditorGUILayout.PropertyField(m_ScaleFactor);
                }
                // Scale with screen size
                else if (m_UiScaleMode.enumValueIndex == (int)IMCCanvasScaler.ScaleMode.ScaleWithScreenSize)
                {
                    EditorGUILayout.PropertyField(m_ReferenceResolution);
                    EditorGUILayout.PropertyField(m_ScreenMatchMode);
                    if (m_ScreenMatchMode.enumValueIndex == (int)IMCCanvasScaler.ScreenMatchMode.MatchWidthOrHeight && !m_ScreenMatchMode.hasMultipleDifferentValues)
                    {
                        Rect r = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + kSliderEndpointLabelsHeight);
                        DualLabeledSlider(r, m_MatchWidthOrHeight, s_Styles.matchContent, s_Styles.widthContent, s_Styles.heightContent);
                    }
                }
                // Constant physical size
                else if (m_UiScaleMode.enumValueIndex == (int)IMCCanvasScaler.ScaleMode.ConstantPhysicalSize)
                {
                    EditorGUILayout.PropertyField(m_PhysicalUnit);
                    EditorGUILayout.PropertyField(m_FallbackScreenDPI);
                    EditorGUILayout.PropertyField(m_DefaultSpriteDPI);
                }

                EditorGUILayout.PropertyField(m_ReferencePixelsPerUnit);
            }

            serializedObject.ApplyModifiedProperties();
        }