diff --git a/.media/01.png b/.media/01.png new file mode 100644 index 0000000..311b116 Binary files /dev/null and b/.media/01.png differ diff --git a/.media/02.png b/.media/02.png new file mode 100644 index 0000000..0a1cc33 Binary files /dev/null and b/.media/02.png differ diff --git a/Editor/MonetColorerCustomEditor.cs b/Editor/MonetColorerCustomEditor.cs new file mode 100644 index 0000000..f1b17c1 --- /dev/null +++ b/Editor/MonetColorerCustomEditor.cs @@ -0,0 +1,29 @@ +using UnityEditor; +using UnityEngine; +using Smoothie.MonetColors.Runtime; + +namespace Smoothie.MonetColors.Editor +{ + [CustomEditor(typeof(MonetColorer), true), CanEditMultipleObjects] + public class MonetColorerCustomEditor : UnityEditor.Editor + { + private SerializedProperty _color; + + private void OnEnable() + { + _color = serializedObject.FindProperty("_color"); + } + + public override void OnInspectorGUI() + { + serializedObject.Update(); + + var uiElement = target as MonetColorer; + uiElement.UpdateColor(); + EditorGUILayout.PropertyField(_color); + EditorGUILayout.LabelField(Colors.Names[_color.intValue]); + + serializedObject.ApplyModifiedProperties(); + } + } +} diff --git a/Editor/com.smoothie.monetcolors.asmdef b/Editor/com.smoothie.monetcolors.asmdef new file mode 100644 index 0000000..ab73b3f --- /dev/null +++ b/Editor/com.smoothie.monetcolors.asmdef @@ -0,0 +1,16 @@ +{ + "name": "Unity.Smoothie.MonetColors.Editor", + "rootNamespace": "Smoothie.MonetColors.Editor", + "references": [ + "GUID:e96f0dfb32d62404bb04759d4f531986" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..22c92c5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,25 @@ +BSD 2-Clause License + +Copyright (c) 2022, -Smooth-E- +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/Runtime/CameraMonetColorer.cs b/Runtime/CameraMonetColorer.cs new file mode 100644 index 0000000..77ce9c8 --- /dev/null +++ b/Runtime/CameraMonetColorer.cs @@ -0,0 +1,17 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace Smoothie.MonetColors.Runtime +{ + [RequireComponent(typeof(Camera))] + public class CameraMonetColorer : MonetColorer + { + public override void UpdateColor() + { + base.UpdateColor(); + var component = GetComponent(); + _colorValue.a = component.backgroundColor.a; + component.backgroundColor = _colorValue; + } + } +} diff --git a/Runtime/Colors.cs b/Runtime/Colors.cs new file mode 100644 index 0000000..136fab1 --- /dev/null +++ b/Runtime/Colors.cs @@ -0,0 +1,102 @@ +using UnityEngine; + +namespace Smoothie.MonetColors.Runtime +{ + public partial class Colors + { + public const int ColorsAmount = 65; + public static string[] Names { get; } = new string[ColorsAmount] { + "accent1_0", + "accent1_10", + "accent1_50", + "accent1_100", + "accent1_200", + "accent1_300", + "accent1_400", + "accent1_500", + "accent1_600", + "accent1_700", + "accent1_800", + "accent1_900", + "accent1_1000", + "accent2_0", + "accent2_10", + "accent2_50", + "accent2_100", + "accent2_200", + "accent2_300", + "accent2_400", + "accent2_500", + "accent2_600", + "accent2_700", + "accent2_800", + "accent2_900", + "accent2_1000", + "accent3_0", + "accent3_10", + "accent3_50", + "accent3_100", + "accent3_200", + "accent3_300", + "accent3_400", + "accent3_500", + "accent3_600", + "accent3_700", + "accent3_800", + "accent3_900", + "accent3_1000", + "neutral1_0", + "neutral1_10", + "neutral1_50", + "neutral1_100", + "neutral1_200", + "neutral1_300", + "neutral1_400", + "neutral1_500", + "neutral1_600", + "neutral1_700", + "neutral1_800", + "neutral1_900", + "neutral1_1000", + "neutral2_0", + "neutral2_10", + "neutral2_50", + "neutral2_100", + "neutral2_200", + "neutral2_300", + "neutral2_400", + "neutral2_500", + "neutral2_600", + "neutral2_700", + "neutral2_800", + "neutral2_900", + "neutral2_1000", + }; + + public static Color GetColor(int index) + { + Color color = _fallbackPalette[index]; + if (Application.platform == RuntimePlatform.Android) + { + AndroidJavaClass versionClass = new AndroidJavaClass("android.os.Build$VERSION"); + int sdkInt = versionClass.GetStatic("SDK_INT"); + if (sdkInt > 30) + { + AndroidJavaClass colorClass = new AndroidJavaClass("android.R$color"); + int colorInt = colorClass.GetStatic("system_" + Names[index]); + AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); + AndroidJavaObject context = unityPlayerClass.GetStatic("currentActivity"); + int colorValue = context.Call("getResources").Call("getColor", colorInt); + colorClass = new AndroidJavaClass("android.graphics.Color"); + AndroidJavaObject colorObject = colorClass.CallStatic("valueOf", colorValue); + float red = colorObject.Call ("red"); + float green = colorObject.Call ("green"); + float blue = colorObject.Call ("blue"); + color = new Color(red, green, blue); + } + } + return color; + } + + } +} diff --git a/Runtime/FallbackColors.cs b/Runtime/FallbackColors.cs new file mode 100644 index 0000000..26af107 --- /dev/null +++ b/Runtime/FallbackColors.cs @@ -0,0 +1,74 @@ +using UnityEngine; + +namespace Smoothie.MonetColors.Runtime { + public partial class Colors + { + private static Color[] _fallbackPalette { get; } = new Color[ColorsAmount] { + new Color32(255, 255, 255, 255), + new Color32(249, 252, 255, 255), + new Color32(224, 243, 255, 255), + new Color32(193, 232, 255, 255), + new Color32(118, 209, 255, 255), + new Color32(75, 182, 232, 255), + new Color32(33, 155, 204, 255), + new Color32(0, 127, 172, 255), + new Color32(0, 102, 139, 255), + new Color32(0, 76, 105, 255), + new Color32(0, 53, 73, 255), + new Color32(0, 30, 44, 255), + new Color32(0, 0, 0, 255), + new Color32(255, 255, 255, 255), + new Color32(249, 252, 255, 255), + new Color32(224, 243, 255, 255), + new Color32(209, 229, 244, 255), + new Color32(181, 202, 215, 255), + new Color32(154, 174, 187, 255), + new Color32(128, 148, 160, 255), + new Color32(101, 121, 133, 255), + new Color32(78, 97, 108, 255), + new Color32(55, 73, 85, 255), + new Color32(32, 51, 61, 255), + new Color32(9, 30, 40, 255), + new Color32(0, 0, 0, 255), + new Color32(255, 255, 255, 255), + new Color32(255, 251, 255, 255), + new Color32(245, 238, 255, 255), + new Color32(230, 222, 255, 255), + new Color32(202, 193, 234, 255), + new Color32(174, 166, 206, 255), + new Color32(147, 140, 177, 255), + new Color32(120, 114, 150, 255), + new Color32(96, 90, 124, 255), + new Color32(72, 66, 100, 255), + new Color32(50, 44, 76, 255), + new Color32(29, 23, 54, 255), + new Color32(0, 0, 0, 255), + new Color32(255, 255, 255, 255), + new Color32(252, 252, 255, 255), + new Color32(240, 240, 243, 255), + new Color32(225, 227, 229, 255), + new Color32(197, 199, 201, 255), + new Color32(170, 171, 174, 255), + new Color32(143, 145, 147, 255), + new Color32(116, 118, 121, 255), + new Color32(92, 95, 97, 255), + new Color32(69, 71, 73, 255), + new Color32(46, 49, 51, 255), + new Color32(25, 28, 30, 255), + new Color32(0, 0, 0, 255), + new Color32(255, 255, 255, 255), + new Color32(249, 252, 255, 255), + new Color32(235, 241, 248, 255), + new Color32(220, 227, 233, 255), + new Color32(192, 199, 205, 255), + new Color32(165, 172, 178, 255), + new Color32(138, 146, 151, 255), + new Color32(112, 119, 124, 255), + new Color32(88, 95, 101, 255), + new Color32(64, 72, 77, 255), + new Color32(42, 49, 54, 255), + new Color32(22, 28, 32, 255), + new Color32(0, 0, 0, 255), + }; + } +} \ No newline at end of file diff --git a/Runtime/ImageMonetColorer.cs b/Runtime/ImageMonetColorer.cs new file mode 100644 index 0000000..1d4b229 --- /dev/null +++ b/Runtime/ImageMonetColorer.cs @@ -0,0 +1,17 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace Smoothie.MonetColors.Runtime +{ + [RequireComponent(typeof(Image))] + public class ImageMonetColorer : MonetColorer + { + public override void UpdateColor() + { + base.UpdateColor(); + var component = GetComponent(); + _colorValue.a = component.color.a; + component.color = _colorValue; + } + } +} diff --git a/Runtime/MonetColorer.cs b/Runtime/MonetColorer.cs new file mode 100644 index 0000000..c172388 --- /dev/null +++ b/Runtime/MonetColorer.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +namespace Smoothie.MonetColors.Runtime +{ + public class MonetColorer : MonoBehaviour + { + [SerializeField, Range(0, 51)] protected int _color; + + protected Color _colorValue; + + private void Awake() => UpdateColor(); + + public virtual void UpdateColor() => _colorValue = Colors.GetColor(_color); + } +} diff --git a/Runtime/OutlineMonetColorer.cs b/Runtime/OutlineMonetColorer.cs new file mode 100644 index 0000000..9e9a1f7 --- /dev/null +++ b/Runtime/OutlineMonetColorer.cs @@ -0,0 +1,23 @@ +using UnityEngine; +using UnityEngine.UI; +using System; + +namespace Smoothie.MonetColors.Runtime +{ + [RequireComponent(typeof(Outline))] + public class OutlineMonetColorer : MonetColorer + { + public override void UpdateColor() + { + base.UpdateColor(); + Outline outline = null; + foreach (var component in GetComponents()) if (component.GetType() == typeof(Outline)) outline = component; + if (outline != null) + { + _colorValue.a = outline.effectColor.a; + outline.effectColor = _colorValue; + } + else throw new NullReferenceException("You don't have an Outline component. or something went wrong..."); + } + } +} diff --git a/Runtime/RawImageMonetColorer.cs b/Runtime/RawImageMonetColorer.cs new file mode 100644 index 0000000..a38179e --- /dev/null +++ b/Runtime/RawImageMonetColorer.cs @@ -0,0 +1,17 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace Smoothie.MonetColors.Runtime +{ + [RequireComponent(typeof(RawImage))] + public class RawImageMonetColorer : MonetColorer + { + public override void UpdateColor() + { + base.UpdateColor(); + var component = GetComponent(); + _colorValue.a = component.color.a; + component.color = _colorValue; + } + } +} diff --git a/Runtime/ShadowMonetColorer.cs b/Runtime/ShadowMonetColorer.cs new file mode 100644 index 0000000..7893a0a --- /dev/null +++ b/Runtime/ShadowMonetColorer.cs @@ -0,0 +1,23 @@ +using UnityEngine; +using UnityEngine.UI; +using System; + +namespace Smoothie.MonetColors.Runtime +{ + [RequireComponent(typeof(Shadow))] + public class ShadowMonetColorer : MonetColorer + { + public override void UpdateColor() + { + base.UpdateColor(); + Shadow shadow = null; + foreach (var component in GetComponents()) if (component.GetType() == typeof(Shadow)) shadow = component; + if (shadow != null) + { + _colorValue.a = shadow.effectColor.a; + shadow.effectColor = _colorValue; + } + else throw new NullReferenceException("You don't have a Shadow component. or something went wrong..."); + } + } +} diff --git a/Runtime/TextMonetColorer.cs b/Runtime/TextMonetColorer.cs new file mode 100644 index 0000000..8dafcbd --- /dev/null +++ b/Runtime/TextMonetColorer.cs @@ -0,0 +1,17 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace Smoothie.MonetColors.Runtime +{ + [RequireComponent(typeof(Text))] + public class TextMonetColorer : MonetColorer + { + public override void UpdateColor() + { + base.UpdateColor(); + var component = GetComponent(); + _colorValue.a = component.color.a; + component.color = _colorValue; + } + } +} diff --git a/Runtime/com.smoothie.monetcolors.asmdef b/Runtime/com.smoothie.monetcolors.asmdef new file mode 100644 index 0000000..00be257 --- /dev/null +++ b/Runtime/com.smoothie.monetcolors.asmdef @@ -0,0 +1,14 @@ +{ + "name": "com.smoothie.monetcolors", + "rootNamespace": "Smoothie.MonetColors.Runtime", + "references": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..852c420 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "version": "0.0.1", + "name": "com.smoothie.monetcolors", + "author": { + "name": "Smooth E", + "url": "https://github.com/Smooth-E" + }, + "displayName": "Monet Colors For Unity", + "description": "Use colors of Material You (Monet) engine introduced in Android 12 for your Unity UI." +} \ No newline at end of file diff --git a/reabme.md b/reabme.md new file mode 100644 index 0000000..69ed5f5 --- /dev/null +++ b/reabme.md @@ -0,0 +1,40 @@ +
+ +
+ +

Material You Colors for Unity

+ +With help of this Unity package you will be able to use Material You colors in your projects. It implements several components to control colors of different UI elements. It also can be used in projects which are targeting API lower than 31 (Android 12), because it has a fallback palette for such cases. + +## Usage + + + + + + +
+ Import the package to your project. To do so, open a package manager tab, click the *plus* button on the left upper corner of it and select *Add package from git URL*. Then enter a git URL of this repository. You can then add components like `ImageMonetColorer` to your objects and select needed color with sliders on those components. + + +
+ +## Tweaking for your needs + +Right now there are components implemented in this package for coloring the following UI elements: +- `Image` +- `Raw Image` +- `Outline` +- `Shadow` +- `Text` +- `Camera Background` + +However, you can easily create *colorers* for any component you need. First of all, inherit you component from a `MonetColorer` base class, then overide the `UpdateColor()` method as you want. Calling `base.UpdateColor()` will set the `_colorValue` and `_color` fields to a new values, when they are changed. + +**Fallback palette.** If you want a custom fallback palette, customize [this file](./Runtime/FallbackColors.cs). Currently, there is no tool to create such file, but there are plans to make one. For now you can use my [Previewer app](https://github.com/Smooth-E/monet-color-previewer) and edit its output by hand. + +## Licensing + +This project is licensed under a [BSD 2-Clause "Simplified" Licanse](./LICENSE), meaning you can use, edit and redistribute contets of this repository freely, but without any warranty and you also should credit me as an author. + +> Artwork used in this readme is taken from [Google Material Icon Library](https://fonts.google.com/icons?icon.set=Material+Icons). \ No newline at end of file