示例#1
0
        /// <summary>
        /// Creates a canvas containing all of the UI elements.
        /// </summary>
        /// <returns>The canvas.</returns>
        public static Transform CreateCanvas(UICanvasType canvasType)
        {
            var uiConfiguration =
                UIConfiguration.GetInstance().GetCanvasConfig(canvasType);

            if (uiConfiguration != null)
            {
                // Canvas Config
                var name         = uiConfiguration.Name;
                var sortingOrder = uiConfiguration.SortingOrder;
                var renderMode   = uiConfiguration.RenderMode;

                // Canvas Scaler Config
                var scaleMode              = uiConfiguration.UICanvasScalerConfig.ScaleMode;
                var referenceResolution    = uiConfiguration.UICanvasScalerConfig.ReferenceResolution;
                var screenMatchMode        = uiConfiguration.UICanvasScalerConfig.ScreenMatchMode;
                var matchWidthOrHeight     = uiConfiguration.UICanvasScalerConfig.MatchWidthOrHeight;
                var referencePixelsPerUnit = uiConfiguration.UICanvasScalerConfig.ReferencePixelsPerUnit;

                // UI Canvas
                var uiCanvas =
                    new GameObject(name, typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));

                // Canvas
                var canvas = uiCanvas.GetComponent <Canvas>();
                canvas.sortingOrder = sortingOrder;
                canvas.renderMode   = renderMode;

                // Canvas Scaler
                var canvasScaler = uiCanvas.GetComponent <CanvasScaler>();
                canvasScaler.uiScaleMode            = scaleMode;
                canvasScaler.referenceResolution    = referenceResolution;
                canvasScaler.screenMatchMode        = screenMatchMode;
                canvasScaler.matchWidthOrHeight     = matchWidthOrHeight;
                canvasScaler.referencePixelsPerUnit = referencePixelsPerUnit;

                return(uiCanvas.GetComponent <Transform>());
            }

            return(null);
        }
示例#2
0
 public UICanvasConfig GetCanvasConfig(UICanvasType canvasType)
 {
     return(CanvasConfig.FirstOrDefault((x) => x.CanvasType == canvasType));
 }