/// <summary> /// Copies a Canvas to another GameObject. /// </summary> /// <param name="canvas">The canvas to copy.</param> /// <param name="gameObjectToAddTo">The game object to add the new Canvas to.</param> /// <returns>The new Canvas instance.</returns> public static Canvas Copy(this Canvas canvas, GameObject gameObjectToAddTo) { Canvas dupCanvas = gameObjectToAddTo.GetAddComponent <Canvas>(); RectTransform mainCanvasRectTransform = canvas.GetComponent <RectTransform>(); RectTransform dropdownCanvasRectTransform = dupCanvas.GetComponent <RectTransform>(); dropdownCanvasRectTransform.position = mainCanvasRectTransform.position; dropdownCanvasRectTransform.sizeDelta = mainCanvasRectTransform.sizeDelta; dropdownCanvasRectTransform.anchorMin = mainCanvasRectTransform.anchorMin; dropdownCanvasRectTransform.anchorMax = mainCanvasRectTransform.anchorMax; dropdownCanvasRectTransform.pivot = mainCanvasRectTransform.pivot; dropdownCanvasRectTransform.rotation = mainCanvasRectTransform.rotation; dropdownCanvasRectTransform.localScale = mainCanvasRectTransform.localScale; dupCanvas.gameObject.GetAddComponent <GraphicRaycaster>(); CanvasScaler mainScaler = canvas.GetComponent <CanvasScaler>(); if (mainScaler != null) { CanvasScaler scaler = dupCanvas.gameObject.GetAddComponent <MaterialCanvasScaler>(); scaler.uiScaleMode = mainScaler.uiScaleMode; scaler.referenceResolution = mainScaler.referenceResolution; scaler.screenMatchMode = mainScaler.screenMatchMode; scaler.matchWidthOrHeight = mainScaler.matchWidthOrHeight; scaler.referencePixelsPerUnit = mainScaler.referencePixelsPerUnit; } MaterialCanvasScaler mainMaterialScaler = mainScaler as MaterialCanvasScaler; if (mainMaterialScaler != null) { MaterialCanvasScaler materialScaler = dupCanvas.gameObject.GetAddComponent <MaterialCanvasScaler>(); materialScaler.useLegacyPhysicalSize = mainMaterialScaler.useLegacyPhysicalSize; materialScaler.supportSafeArea = mainMaterialScaler.supportSafeArea; } Kyub.Performance.SustainedCanvasView mainSustainedCanvasView = canvas.GetComponent <Kyub.Performance.SustainedCanvasView>(); if (mainSustainedCanvasView) { Kyub.Performance.SustainedCanvasView sustainedCanvasView = dupCanvas.gameObject.GetAddComponent <Kyub.Performance.SustainedCanvasView>(); sustainedCanvasView.RequiresConstantRepaint = mainSustainedCanvasView.RequiresConstantRepaint; sustainedCanvasView.UseRenderBuffer = mainSustainedCanvasView.UseRenderBuffer; sustainedCanvasView.MinimumSupportedFps = mainSustainedCanvasView.MinimumSupportedFps; } dupCanvas.renderMode = canvas.renderMode; return(dupCanvas); }
/// <summary> /// Copies the settings to other canvas. /// </summary> /// <param name="canvas">The canvas to copy from.</param> /// <param name="otherCanvas">The canvas to copy to.</param> public static void CopySettingsToOtherCanvas(this Canvas canvas, Canvas otherCanvas) { RectTransform mainCanvasRectTransform = canvas.GetComponent <RectTransform>(); RectTransform dropdownCanvasRectTransform = otherCanvas.GetComponent <RectTransform>(); dropdownCanvasRectTransform.position = mainCanvasRectTransform.position; dropdownCanvasRectTransform.sizeDelta = mainCanvasRectTransform.sizeDelta; dropdownCanvasRectTransform.anchorMin = mainCanvasRectTransform.anchorMin; dropdownCanvasRectTransform.anchorMax = mainCanvasRectTransform.anchorMax; dropdownCanvasRectTransform.pivot = mainCanvasRectTransform.pivot; dropdownCanvasRectTransform.rotation = mainCanvasRectTransform.rotation; dropdownCanvasRectTransform.localScale = mainCanvasRectTransform.localScale; otherCanvas.gameObject.GetAddComponent <GraphicRaycaster>(); CanvasScaler mainScaler = canvas.GetComponent <CanvasScaler>(); if (mainScaler != null) { CanvasScaler scaler = otherCanvas.gameObject.GetAddComponent <MaterialCanvasScaler>(); scaler.uiScaleMode = mainScaler.uiScaleMode; scaler.referenceResolution = mainScaler.referenceResolution; scaler.screenMatchMode = mainScaler.screenMatchMode; scaler.matchWidthOrHeight = mainScaler.matchWidthOrHeight; scaler.referencePixelsPerUnit = mainScaler.referencePixelsPerUnit; scaler.scaleFactor = mainScaler.scaleFactor; otherCanvas.scaleFactor = scaler.scaleFactor; } MaterialCanvasScaler mainMaterialScaler = mainScaler as MaterialCanvasScaler; if (mainMaterialScaler != null) { MaterialCanvasScaler materialScaler = otherCanvas.gameObject.GetAddComponent <MaterialCanvasScaler>(); materialScaler.useLegacyPhysicalSize = mainMaterialScaler.useLegacyPhysicalSize; materialScaler.supportSafeArea = mainMaterialScaler.supportSafeArea; } otherCanvas.renderMode = canvas.renderMode; otherCanvas.targetDisplay = canvas.targetDisplay; otherCanvas.worldCamera = canvas.worldCamera; otherCanvas.planeDistance = canvas.planeDistance; }
public override void OnInspectorGUI() { if (s_Styles == null) { s_Styles = new Styles(); } bool allAreRoot = true; bool showWorldDiffers = false; bool showWorld = ((target as CanvasScaler).GetComponent <Canvas>().renderMode == RenderMode.WorldSpace); for (int i = 0; i < targets.Length; i++) { CanvasScaler scaler = targets[i] as CanvasScaler; 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; using (new EditorGUI.DisabledScope(showWorld || showWorldDiffers)) { if (showWorld || showWorldDiffers) { EditorGUILayout.Popup(s_Styles.uiScaleModeContent.text, 0, new[] { "World" }); } else { EditorGUILayout.PropertyField(m_UiScaleMode, s_Styles.uiScaleModeContent); } } 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)CanvasScaler.ScaleMode.ConstantPixelSize) { EditorGUILayout.PropertyField(m_ScaleFactor); } // Scale with screen size else if (m_UiScaleMode.enumValueIndex == (int)CanvasScaler.ScaleMode.ScaleWithScreenSize) { EditorGUILayout.PropertyField(m_ReferenceResolution); EditorGUILayout.PropertyField(m_ScreenMatchMode); if (m_ScreenMatchMode.enumValueIndex == (int)CanvasScaler.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); } EditorGUILayout.PropertyField(m_SupportSafeArea); EditorGUILayout.PropertyField(m_ScaleFactor); } // Constant physical size else if (m_UiScaleMode.enumValueIndex == (int)CanvasScaler.ScaleMode.ConstantPhysicalSize) { EditorGUILayout.PropertyField(m_UseLegacyPhysicalSizeCalc); EditorGUILayout.Space(2); if (!m_UseLegacyPhysicalSizeCalc.boolValue) { EditorGUILayout.PropertyField(m_EditorForceDPI); EditorGUILayout.PropertyField(m_EditorForceDPIValue); EditorGUILayout.PropertyField(m_FallbackScreenDPI); EditorGUILayout.PropertyField(m_DefaultSpriteDPI, new GUIContent("Target Screen DPI")); EditorGUILayout.PropertyField(m_ScaleFactor); //Show useful informations MaterialCanvasScaler scaler = target as MaterialCanvasScaler; EditorGUILayout.LabelField(scaler.screenWidth + " x " + scaler.screenHeight + ", " + scaler.dpi + " dpi, " + scaler.screenSizeDigonal.ToString("##.##") + " inches", EditorStyles.miniLabel); } else { EditorGUILayout.PropertyField(m_PhysicalUnit); EditorGUILayout.PropertyField(m_FallbackScreenDPI); EditorGUILayout.PropertyField(m_DefaultSpriteDPI); } } EditorGUILayout.PropertyField(m_ReferencePixelsPerUnit); } EditorGUILayout.Space(); EditorGUILayout.PropertyField(onCanvasAreaChanged); serializedObject.ApplyModifiedProperties(); }