Skip to content

Commit

Permalink
Merge pull request #2 from ryanslikesocool/2.1.0
Browse files Browse the repository at this point in the history
2.1.0
  • Loading branch information
ryanslikesocool committed Mar 6, 2024
2 parents f196795 + 7a0a5d8 commit ea7d893
Show file tree
Hide file tree
Showing 36 changed files with 180 additions and 71 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ sysinfo.txt
# Crashlytics generated file
crashlytics-build.properties

# Other
*.DS_Store
2 changes: 1 addition & 1 deletion Scripts/Editor.meta → Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions Editor/EditorThemeSwitcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#if UNITY_EDITOR_OSX
using UnityEditor;

namespace NativeColorScheme {
public static class EditorThemeSwitcher {
private static System.Reflection.MethodInfo switchSkinMethod;

public static ColorScheme? DesiredAppearance {
get => (byte)EditorPrefs.GetInt(SETTING_NAME, 0) switch {
1 => ColorScheme.Light,
2 => ColorScheme.Dark,
_ => null
};
set {
int newValue = value switch {
ColorScheme.Light => 1,
ColorScheme.Dark => 2,
_ => 0
};
EditorPrefs.SetInt(SETTING_NAME, newValue);
}
}

[InitializeOnLoadMethod]
static void Load() {
switchSkinMethod = System.Reflection.Assembly.GetAssembly(typeof(UnityEditorInternal.AssetStore)).GetType("UnityEditorInternal.InternalEditorUtility", true).GetMethod("SwitchSkinAndRepaintAllViews");

EditorApplication.update += UpdateColorScheme;
//EditorApplication.focusChanged += FocusChanged;
SetColorScheme(DesiredAppearance);
}

private static void FocusChanged(bool state)
=> UpdateColorScheme();

private static void SetDesiredColorScheme(ColorScheme? colorScheme) {
DesiredAppearance = colorScheme;
SetColorScheme(DesiredAppearance, true);
}

private static void UpdateColorScheme()
=> SetColorScheme(DesiredAppearance);

private static void SetColorScheme(ColorScheme? colorScheme, bool force = false) {
ColorScheme finalColorScheme = colorScheme ?? ColorSchemeUtil.Current ?? ColorScheme.Light;

if (finalColorScheme != DesiredAppearance || force) {
Menu.SetChecked(SYSTEM_PATH, colorScheme == null);
Menu.SetChecked(LIGHT_PATH, colorScheme == ColorScheme.Light);
Menu.SetChecked(DARK_PATH, colorScheme == ColorScheme.Dark);

if (finalColorScheme == ColorScheme.Dark ^ EditorGUIUtility.isProSkin) {
switchSkinMethod.Invoke(null, null);
}
}
}

// MARK: - Menu

[MenuItem(SYSTEM_PATH, priority = 0)]
private static void SetSystem()
=> SetDesiredColorScheme(null);

[MenuItem(LIGHT_PATH, priority = 11)]
private static void SetLight()
=> SetDesiredColorScheme(ColorScheme.Light);

[MenuItem(DARK_PATH, priority = 12)]
private static void SetDark()
=> SetDesiredColorScheme(ColorScheme.Dark);

// MARK: - Constants

private const string SETTING_NAME = "NativeColorScheme.ColorScheme";

private const string SYSTEM_PATH = "Window/Color Scheme/System";
private const string LIGHT_PATH = "Window/Color Scheme/Light";
private const string DARK_PATH = "Window/Color Scheme/Dark";
}
}
#endif
File renamed without changes.
18 changes: 18 additions & 0 deletions Editor/NativeColorScheme.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "NativeColorScheme.Editor",
"rootNamespace": "NativeColorScheme.Editor",
"references": [
"GUID:c8c56aad610fe4ee3936149e38efa88a"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/NativeColorScheme.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 34 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
# NativeColorScheme
System color scheme detection in Unity for macOS and iOS (runtime) and macOS (editor).
System color scheme detection in Unity for macOS and iOS.

## Requirements
Color Scheme requires macOS 11 or later, or iOS 14 or later
### Runtime
- macOS 11+
- iOS 14+
- tvOS 14+

**Recommended Installation** (Unity Package Manager)
- "Add package from git URL..."
- `https://github.com/ryanslikesocool/NativeColorScheme.git`
---

**Alternate Installation**
- Get the latest [release](https://github.com/ryanslikesocool/ColorScheme/releases)
- Open with the desired Unity project
- Import into the Plugins folder
### Editor
- macOS 11+, Unity 2020.3+

## Installation (Unity Package Manager)
- Select "Add package from git URL..." from the plus menu in the package manager window.
- Paste the package's git url.
```
https://github.com/ryanslikesocool/NativeColorScheme.git
```

## Usage
Get the value from `ColorScheme.ColorSchemeUtil.CurrentColorScheme` from anywhere in your code and use accordingly.\
Unsupported platforms/OS versions will return `ColorScheme.Unknown`.
### C#
```cs
using NativeColorScheme;

static class Somewhere {
static Color GetColorForColorScheme() => ColorSchemeUtil.Current switch {
ColorScheme.Light => Color.white,
ColorScheme.Dark => Color.black,
_ => Color.magenta
};
}
```
Unsupported platforms or OS versions will return `null`.

---

### Editor
Using the Unity editor on macOS, the plugin will automatically detect the system's color scheme and change the editor accordingly.\
The preference can be set to `System`, `Light`, or `Dark` from the `Window/Color Scheme` menu.
File renamed without changes.
6 changes: 6 additions & 0 deletions Runtime/ColorScheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NativeColorScheme {
public enum ColorScheme : byte {
Light = 1,
Dark = 2
}
}
File renamed without changes.
29 changes: 29 additions & 0 deletions Runtime/ColorSchemeUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS
using System.Runtime.InteropServices;
#endif

namespace NativeColorScheme {
public static class ColorSchemeUtil {
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
[DllImport("ColorScheme_macOS")]
private static extern int getCurrentColorScheme_macOS();

public static ColorScheme? Current => getCurrentColorScheme_macOS() switch {
1 => ColorScheme.Light,
2 => ColorScheme.Dark,
_ => null
};
#elif UNITY_IOS || UNITY_TVOS
[DllImport("__Internal")]
private static extern int getCurrentColorScheme_iOS();

public static ColorScheme? Current => getCurrentColorScheme_iOS() switch {
1 => ColorScheme.Light,
2 => ColorScheme.Dark,
_ => null
};
#else
public static ColorScheme? Current => null;
#endif
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed Scripts/.DS_Store
Binary file not shown.
9 changes: 0 additions & 9 deletions Scripts/ColorScheme.cs

This file was deleted.

21 changes: 0 additions & 21 deletions Scripts/ColorSchemeUtil.cs

This file was deleted.

27 changes: 0 additions & 27 deletions Scripts/Editor/EditorThemeSwitcher.cs

This file was deleted.

Binary file removed external~/.DS_Store
Binary file not shown.
Binary file removed external~/Dark Detector Xcode/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file removed external~/Dark Detector Xcode/iOS/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "com.developedwithlove.nativecolorscheme",
"displayName": "Native Color Scheme",
"version": "2.0.1",
"version": "2.1.0",
"unity": "2020.3",
"description": "System color scheme detection in Unity for macOS and iOS, as well as the Unity Editor on macOS",
"description": "System color scheme detection in Unity for macOS and iOS.",
"documentationUrl": "https://github.com/ryanslikesocool/NativeColorScheme",
"author": {
"name": "Ryan Boyer",
Expand Down

0 comments on commit ea7d893

Please sign in to comment.