Example #1
0
        //The base tab is the outline surrounding the color picker, as well as teh "Player Color" label and associated "Selected Color" image.
        private static void InitializeBaseTabElement(RectTransform joinServerBackground, GameObject playerSettingsPanel)
        {
            GameObject baseTab = playerSettingsPanel.RequireGameObject("BaseTab");

            //Re-position the border
            RectTransform baseTabTransform = (RectTransform)baseTab.transform;

            baseTabTransform.anchoredPosition = new Vector2(0.5f, 0.5f);
            baseTabTransform.anchorMin        = new Vector2(0.5f, 0.5f);
            baseTabTransform.anchorMax        = new Vector2(0.5f, 0.5f);
            baseTabTransform.pivot            = new Vector2(0.5f, 0.5f);

            //Resize the border to match the new parent. Capture the original width so we can make adjustments to child controls later
            //because the existing elements all have some really weird anchor settings the prevent them from moving automatically.
            float originalBaseTabWidth = baseTabTransform.rect.width;

            baseTabTransform.sizeDelta = new Vector2(joinServerBackground.rect.width, baseTabTransform.rect.height);

            //Move the SelectedColor element over to the right enough to match the shrinkage of the base tab.
            GameObject baseTabSelectedColor      = baseTabTransform.RequireGameObject("SelectedColor");
            Image      baseTabSelectedColorImage = baseTabSelectedColor.GetComponent <Image>();

            baseTabSelectedColorImage.rectTransform.anchoredPosition = new Vector2(
                baseTabSelectedColorImage.rectTransform.anchoredPosition.x + (originalBaseTabWidth - baseTabTransform.rect.width) / 2,
                baseTabSelectedColorImage.rectTransform.anchoredPosition.y);

            //Place the "Player Color" label to the right of the SelectedColor image and shrink it to fit the new tab region.
            GameObject    baseTabTextGameObject = baseTabTransform.RequireGameObject("Text");
            RectTransform baseTabTextTransform  = (RectTransform)baseTabTextGameObject.transform;

            baseTabTextTransform.anchorMin = new Vector2(0.5f, 0.5f);
            baseTabTextTransform.anchorMax = new Vector2(0.5f, 0.5f);
            baseTabTextTransform.pivot     = new Vector2(0.5f, 0.5f);
            baseTabTextTransform.sizeDelta = new Vector2(80, 35);

            baseTabTextTransform.anchoredPosition = new Vector2(
                baseTabSelectedColorImage.rectTransform.anchoredPosition.x + baseTabTextTransform.rect.width / 2f + 22f,
                baseTabSelectedColorImage.rectTransform.anchoredPosition.y);

            Text baseTabText = baseTabTextGameObject.GetComponent <Text>();

            baseTabText.text = "Player Color";

            //This resizes the actual Image that outlines all of the UI elements.
            GameObject baseTabBackgroundGameObject = baseTabTransform.RequireGameObject("Background");
            Image      baseTabBackgroundImage      = baseTabBackgroundGameObject.GetComponent <Image>();

            baseTabBackgroundImage.rectTransform.sizeDelta = new Vector2(joinServerBackground.rect.width, baseTabTransform.rect.height);

            //This removes the extra "tabs" from the base texture.
            Texture2D          newBaseTabTexture        = baseTabBackgroundImage.sprite.texture.Clone();
            TextureBlock       textureBlock             = new TextureBlock(3, 3, 160, (int)(baseTabBackgroundImage.sprite.textureRect.height - 71f));
            IColorSwapStrategy alphaChannelSwapper      = new AlphaChannelSwapper(0f);
            HsvSwapper         baseTabBackgroundSwapper = new HsvSwapper(alphaChannelSwapper);

            baseTabBackgroundSwapper.SetHueRange(185f, 215f);
            baseTabBackgroundSwapper.SetAlphaRange(0f, 175f);
            newBaseTabTexture.SwapTextureColors(baseTabBackgroundSwapper, textureBlock);
            baseTabBackgroundImage.sprite = Sprite.Create(newBaseTabTexture, new Rect(baseTabBackgroundImage.sprite.textureRect), new Vector2(0f, 0f));
        }
Example #2
0
        //This applies a color filter to a specific region of a 2D texture.
        public static void SwapTextureColors(
            this Texture2D texture,
            HsvSwapper filter,
            TextureBlock textureBlock)
        {
            Color[] pixels = texture.GetPixels(textureBlock.X, textureBlock.Y, textureBlock.BlockWidth, textureBlock.BlockHeight);

            filter.SwapColors(pixels);

            texture.SetPixels(textureBlock.X, textureBlock.Y, textureBlock.BlockWidth, textureBlock.BlockHeight, pixels);
            texture.Apply();
        }