Merged in Naphier/unity-ui-extensions_ng (pull request #30)

Further improvements to color picker control
release
Sean Mann 2018-03-24 11:35:53 +00:00 committed by Simon Jackson
commit 4a2a4f555d
12 changed files with 9081 additions and 21432 deletions

1
.gitignore vendored
View File

@ -32,3 +32,4 @@ sysinfo.txt
# Builds # Builds
*.apk *.apk
*.unitypackage *.unitypackage
/.vs

View File

@ -0,0 +1,28 @@
using UnityEngine;
using UnityEditor;
namespace UnityEngine.UI.Extensions.ColorPicker
{
[CustomEditor(typeof(ColorPickerPresets))]
public class ColorPickerPresetsEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var colorPickerPresets = (ColorPickerPresets)target;
if (colorPickerPresets.saveMode != ColorPickerPresets.SaveType.JsonFile)
return;
string fileLocation = colorPickerPresets.JsonFilePath;
if (!System.IO.File.Exists(fileLocation))
return;
if (GUILayout.Button("Open JSON file."))
{
Application.OpenURL(fileLocation);
}
}
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 0dc729b738fb01043ac4c04b78575a98
timeCreated: 1520733906
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,10 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 53b194f66900add419869c57ffbe7b85 guid: 53b194f66900add419869c57ffbe7b85
timeCreated: 1480025159 timeCreated: 1519012868
licenseType: Pro licenseType: Pro
NativeFormatImporter: NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 100100000
userData: userData:
assetBundleName: assetBundleName:
assetBundleVariant: assetBundleVariant:

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

View File

@ -0,0 +1,117 @@
fileFormatVersion: 2
guid: b2ef02fbd2da4554ebbac497ca14bb8d
timeCreated: 1519009670
licenseType: Pro
TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -1
wrapU: 1
wrapV: 1
wrapW: -1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: Windows Store Apps
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,257 +1,303 @@
///Credit judah4 ///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/ ///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions.ColorPicker namespace UnityEngine.UI.Extensions.ColorPicker
{ {
[ExecuteInEditMode]
public class ColorPickerControl : MonoBehaviour
{
private float _hue = 0;
private float _saturation = 0;
private float _brightness = 0;
public class ColorPickerControl : MonoBehaviour private float _red = 0;
{ private float _green = 0;
private float _hue = 0; private float _blue = 0;
private float _saturation = 0;
private float _brightness = 0;
private float _red = 0; private float _alpha = 1;
private float _green = 0;
private float _blue = 0;
private float _alpha = 1; public ColorChangedEvent onValueChanged = new ColorChangedEvent();
public HSVChangedEvent onHSVChanged = new HSVChangedEvent();
public ColorChangedEvent onValueChanged = new ColorChangedEvent(); [SerializeField]
public HSVChangedEvent onHSVChanged = new HSVChangedEvent(); bool hsvSlidersOn = true;
public Color CurrentColor [SerializeField]
{ List<GameObject> hsvSliders = new List<GameObject>();
get
{
return new Color(_red, _green, _blue, _alpha);
}
set
{
if (CurrentColor == value)
return;
_red = value.r; [SerializeField]
_green = value.g; bool rgbSlidersOn = true;
_blue = value.b;
_alpha = value.a;
RGBChanged(); [SerializeField]
List<GameObject> rgbSliders = new List<GameObject>();
SendChangedEvent();
}
}
private void Start() [SerializeField]
{ GameObject alphaSlider;
SendChangedEvent();
}
public float H public void SetHSVSlidersOn(bool value)
{ {
get hsvSlidersOn = value;
{
return _hue;
}
set
{
if (_hue == value)
return;
_hue = value; foreach (var item in hsvSliders)
item.SetActive(value);
HSVChanged(); if (alphaSlider)
alphaSlider.SetActive(hsvSlidersOn || rgbSlidersOn);
}
SendChangedEvent(); public void SetRGBSlidersOn(bool value)
} {
} rgbSlidersOn = value;
foreach (var item in rgbSliders)
item.SetActive(value);
public float S if (alphaSlider)
{ alphaSlider.SetActive(hsvSlidersOn || rgbSlidersOn);
get }
{
return _saturation;
}
set
{
if (_saturation == value)
return;
_saturation = value;
HSVChanged(); void Update()
{
#if UNITY_EDITOR
SetHSVSlidersOn(hsvSlidersOn);
SetRGBSlidersOn(rgbSlidersOn);
#endif
}
SendChangedEvent(); public Color CurrentColor
} {
} get
{
return new Color(_red, _green, _blue, _alpha);
}
set
{
if (CurrentColor == value)
return;
public float V _red = value.r;
{ _green = value.g;
get _blue = value.b;
{ _alpha = value.a;
return _brightness;
}
set
{
if (_brightness == value)
return;
_brightness = value; RGBChanged();
HSVChanged(); SendChangedEvent();
}
}
SendChangedEvent(); private void Start()
} {
} SendChangedEvent();
}
public float R public float H
{ {
get get
{ {
return _red; return _hue;
} }
set set
{ {
if (_red == value) if (_hue == value)
return; return;
_red = value; _hue = value;
RGBChanged(); HSVChanged();
SendChangedEvent(); SendChangedEvent();
} }
} }
public float G public float S
{ {
get get
{ {
return _green; return _saturation;
} }
set set
{ {
if (_green == value) if (_saturation == value)
return; return;
_green = value; _saturation = value;
RGBChanged(); HSVChanged();
SendChangedEvent(); SendChangedEvent();
} }
} }
public float B public float V
{ {
get get
{ {
return _blue; return _brightness;
} }
set set
{ {
if (_blue == value) if (_brightness == value)
return; return;
_blue = value; _brightness = value;
RGBChanged(); HSVChanged();
SendChangedEvent(); SendChangedEvent();
} }
} }
private float A public float R
{ {
get get
{ {
return _alpha; return _red;
} }
set set
{ {
if (_alpha == value) if (_red == value)
return; return;
_alpha = value; _red = value;
SendChangedEvent(); RGBChanged();
}
}
private void RGBChanged() SendChangedEvent();
{ }
HsvColor color = HSVUtil.ConvertRgbToHsv(CurrentColor); }
_hue = color.NormalizedH; public float G
_saturation = color.NormalizedS; {
_brightness = color.NormalizedV; get
} {
return _green;
}
set
{
if (_green == value)
return;
private void HSVChanged() _green = value;
{
Color color = HSVUtil.ConvertHsvToRgb(_hue * 360, _saturation, _brightness, _alpha);
_red = color.r; RGBChanged();
_green = color.g;
_blue = color.b;
}
private void SendChangedEvent() SendChangedEvent();
{ }
onValueChanged.Invoke(CurrentColor); }
onHSVChanged.Invoke(_hue, _saturation, _brightness);
}
public void AssignColor(ColorValues type, float value) public float B
{ {
switch (type) get
{ {
case ColorValues.R: return _blue;
R = value; }
break; set
case ColorValues.G: {
G = value; if (_blue == value)
break; return;
case ColorValues.B:
B = value;
break;
case ColorValues.A:
A = value;
break;
case ColorValues.Hue:
H = value;
break;
case ColorValues.Saturation:
S = value;
break;
case ColorValues.Value:
V = value;
break;
default:
break;
}
}
public float GetValue(ColorValues type) _blue = value;
{
switch (type) RGBChanged();
{
case ColorValues.R: SendChangedEvent();
return R; }
case ColorValues.G: }
return G;
case ColorValues.B: private float A
return B; {
case ColorValues.A: get
return A; {
case ColorValues.Hue: return _alpha;
return H; }
case ColorValues.Saturation: set
return S; {
case ColorValues.Value: if (_alpha == value)
return V; return;
default:
throw new System.NotImplementedException(""); _alpha = value;
}
} SendChangedEvent();
} }
}
private void RGBChanged()
{
HsvColor color = HSVUtil.ConvertRgbToHsv(CurrentColor);
_hue = color.NormalizedH;
_saturation = color.NormalizedS;
_brightness = color.NormalizedV;
}
private void HSVChanged()
{
Color color = HSVUtil.ConvertHsvToRgb(_hue * 360, _saturation, _brightness, _alpha);
_red = color.r;
_green = color.g;
_blue = color.b;
}
private void SendChangedEvent()
{
onValueChanged.Invoke(CurrentColor);
onHSVChanged.Invoke(_hue, _saturation, _brightness);
}
public void AssignColor(ColorValues type, float value)
{
switch (type)
{
case ColorValues.R:
R = value;
break;
case ColorValues.G:
G = value;
break;
case ColorValues.B:
B = value;
break;
case ColorValues.A:
A = value;
break;
case ColorValues.Hue:
H = value;
break;
case ColorValues.Saturation:
S = value;
break;
case ColorValues.Value:
V = value;
break;
default:
break;
}
}
public float GetValue(ColorValues type)
{
switch (type)
{
case ColorValues.R:
return R;
case ColorValues.G:
return G;
case ColorValues.B:
return B;
case ColorValues.A:
return A;
case ColorValues.Hue:
return H;
case ColorValues.Saturation:
return S;
case ColorValues.Value:
return V;
default:
throw new System.NotImplementedException("");
}
}
}
} }

View File

@ -1,48 +1,194 @@
///Credit judah4 ///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/ ///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions.ColorPicker namespace UnityEngine.UI.Extensions.ColorPicker
{ {
public class ColorPickerPresets : MonoBehaviour public class ColorPickerPresets : MonoBehaviour
{ {
public ColorPickerControl picker; public ColorPickerControl picker;
public GameObject[] presets;
public Image createPresetImage;
void Awake() [SerializeField]
{ protected GameObject presetPrefab;
// picker.onHSVChanged.AddListener(HSVChanged);
picker.onValueChanged.AddListener(ColorChanged);
}
public void CreatePresetButton() [SerializeField]
{ protected int maxPresets = 16;
for (var i = 0; i < presets.Length; i++)
{
if (!presets[i].activeSelf)
{
presets[i].SetActive(true);
presets[i].GetComponent<Image>().color = picker.CurrentColor;
break;
}
}
}
public void PresetSelect(Image sender) [SerializeField]
{ protected Color[] predefinedPresets;
picker.CurrentColor = sender.color;
}
// Not working, it seems ConvertHsvToRgb() is broken. It doesn't work when fed protected List<Color> presets = new List<Color>();
// input h, s, v as shown below. public Image createPresetImage;
// private void HSVChanged(float h, float s, float v) public Transform createButton;
// {
// createPresetImage.color = HSVUtil.ConvertHsvToRgb(h, s, v, 1); public enum SaveType { None, PlayerPrefs, JsonFile }
// } [SerializeField]
private void ColorChanged(Color color) public SaveType saveMode = SaveType.None;
{
createPresetImage.color = color; [SerializeField]
} protected string playerPrefsKey;
}
public virtual string JsonFilePath
{
get { return Application.persistentDataPath + "/" + playerPrefsKey + ".json"; }
}
protected virtual void Reset()
{
playerPrefsKey = "colorpicker_" + GetInstanceID().ToString();
}
protected virtual void Awake()
{
picker.onHSVChanged.AddListener(HSVChanged);
picker.onValueChanged.AddListener(ColorChanged);
picker.CurrentColor = Color.white;
presetPrefab.SetActive(false);
presets.AddRange(predefinedPresets);
LoadPresets(saveMode);
}
public virtual void CreatePresetButton()
{
CreatePreset(picker.CurrentColor);
}
public virtual void LoadPresets(SaveType saveType)
{
string jsonData = "";
switch (saveType)
{
case SaveType.None:
break;
case SaveType.PlayerPrefs:
if (PlayerPrefs.HasKey(playerPrefsKey))
{
jsonData = PlayerPrefs.GetString(playerPrefsKey);
}
break;
case SaveType.JsonFile:
if (System.IO.File.Exists(JsonFilePath))
{
jsonData = System.IO.File.ReadAllText(JsonFilePath);
}
break;
default:
throw new System.NotImplementedException(saveType.ToString());
}
if (!string.IsNullOrEmpty(jsonData))
{
try
{
var jsonColors = JsonUtility.FromJson<JsonColor>(jsonData);
presets.AddRange(jsonColors.GetColors());
}
catch (System.Exception e)
{
Debug.LogException(e);
}
}
foreach (var item in presets)
{
CreatePreset(item, true);
}
}
public virtual void SavePresets(SaveType saveType)
{
if (presets == null || presets.Count <= 0)
{
Debug.LogError(
"presets cannot be null or empty: " + (presets == null ? "NULL" : "EMPTY"));
return;
}
var jsonColor = new JsonColor();
jsonColor.SetColors(presets.ToArray());
string jsonData = JsonUtility.ToJson(jsonColor);
switch (saveType)
{
case SaveType.None:
Debug.LogWarning("Called SavePresets with SaveType = None...");
break;
case SaveType.PlayerPrefs:
PlayerPrefs.SetString(playerPrefsKey, jsonData);
break;
case SaveType.JsonFile:
System.IO.File.WriteAllText(JsonFilePath, jsonData);
//Application.OpenURL(JsonFilePath);
break;
default:
throw new System.NotImplementedException(saveType.ToString());
}
}
protected class JsonColor
{
public Color32[] colors;
public void SetColors(Color[] colorsIn)
{
this.colors = new Color32[colorsIn.Length];
for (int i = 0; i < colorsIn.Length; i++)
{
this.colors[i] = colorsIn[i];
}
}
public Color[] GetColors()
{
Color[] colorsOut = new Color[colors.Length];
for (int i = 0; i < colors.Length; i++)
{
colorsOut[i] = colors[i];
}
return colorsOut;
}
}
public virtual void CreatePreset(Color color, bool loading)
{
createButton.gameObject.SetActive(presets.Count < maxPresets);
var newPresetButton = Instantiate(presetPrefab, presetPrefab.transform.parent);
newPresetButton.transform.SetAsLastSibling();
newPresetButton.SetActive(true);
newPresetButton.GetComponent<Image>().color = color;
createPresetImage.color = Color.white;
if (!loading)
{
presets.Add(color);
SavePresets(saveMode);
}
}
public virtual void CreatePreset(Color color)
{
CreatePreset(color, false);
}
public virtual void PresetSelect(Image sender)
{
picker.CurrentColor = sender.color;
}
protected virtual void HSVChanged(float h, float s, float v)
{
createPresetImage.color = HSVUtil.ConvertHsvToRgb(h * 360, s, v, 1);
//Debug.Log("hsv util color: " + createPresetImage.color);
}
protected virtual void ColorChanged(Color color)
{
createPresetImage.color = color;
//Debug.Log("color changed: " + color);
}
}
} }

View File

@ -0,0 +1,70 @@
namespace UnityEngine.UI.Extensions.ColorPicker
{
/// <summary>
/// Samples colors from a screen capture.
/// Warning! In the editor if you're not in Free aspect mode then
/// the captured area includes the grey areas to the left and right of the game view window.
/// In a build this will not be an issue.
///
/// This does not work well with a world space UI as positioning is working with screen space.
/// </summary>
public class ColorSampler : MonoBehaviour
{
[SerializeField]
protected Button sampler;
[SerializeField]
protected Outline samplerOutline;
protected Texture2D screenCapture;
public ColorChangedEvent oncolorSelected = new ColorChangedEvent();
protected Color color;
protected virtual void OnEnable()
{
screenCapture = ScreenCapture.CaptureScreenshotAsTexture();
sampler.gameObject.SetActive(true);
sampler.onClick.AddListener(SelectColor);
}
protected virtual void OnDisable()
{
Destroy(screenCapture);
sampler.gameObject.SetActive(false);
sampler.onClick.RemoveListener(SelectColor);
}
protected virtual void Update()
{
if (screenCapture == null)
return;
sampler.transform.position = Input.mousePosition;
color = screenCapture.GetPixel((int)Input.mousePosition.x, (int)Input.mousePosition.y);
HandleSamplerColoring();
}
protected virtual void HandleSamplerColoring()
{
sampler.image.color = color;
if (samplerOutline)
{
var c = Color.Lerp(Color.white, Color.black, color.grayscale > 0.5f ? 1 : 0);
c.a = samplerOutline.effectColor.a;
samplerOutline.effectColor = c;
}
}
protected virtual void SelectColor()
{
if (oncolorSelected != null)
oncolorSelected.Invoke(color);
enabled = false;
}
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 01c80b26fb6519c4ea3f410dc08f5814
timeCreated: 1519007668
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: