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
*.apk
*.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
guid: 53b194f66900add419869c57ffbe7b85
timeCreated: 1480025159
timeCreated: 1519012868
licenseType: Pro
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 100100000
userData:
assetBundleName:
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/
using System.Collections.Generic;
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 _hue = 0;
private float _saturation = 0;
private float _brightness = 0;
private float _red = 0;
private float _green = 0;
private float _blue = 0;
private float _red = 0;
private float _green = 0;
private float _blue = 0;
private float _alpha = 1;
private float _alpha = 1;
public ColorChangedEvent onValueChanged = new ColorChangedEvent();
public HSVChangedEvent onHSVChanged = new HSVChangedEvent();
public ColorChangedEvent onValueChanged = new ColorChangedEvent();
public HSVChangedEvent onHSVChanged = new HSVChangedEvent();
[SerializeField]
bool hsvSlidersOn = true;
public Color CurrentColor
{
get
{
return new Color(_red, _green, _blue, _alpha);
}
set
{
if (CurrentColor == value)
return;
[SerializeField]
List<GameObject> hsvSliders = new List<GameObject>();
_red = value.r;
_green = value.g;
_blue = value.b;
_alpha = value.a;
[SerializeField]
bool rgbSlidersOn = true;
RGBChanged();
[SerializeField]
List<GameObject> rgbSliders = new List<GameObject>();
SendChangedEvent();
}
}
[SerializeField]
GameObject alphaSlider;
private void Start()
{
SendChangedEvent();
}
public void SetHSVSlidersOn(bool value)
{
hsvSlidersOn = value;
public float H
{
get
{
return _hue;
}
set
{
if (_hue == value)
return;
foreach (var item in hsvSliders)
item.SetActive(value);
_hue = value;
if (alphaSlider)
alphaSlider.SetActive(hsvSlidersOn || rgbSlidersOn);
}
HSVChanged();
public void SetRGBSlidersOn(bool value)
{
rgbSlidersOn = value;
foreach (var item in rgbSliders)
item.SetActive(value);
SendChangedEvent();
}
}
if (alphaSlider)
alphaSlider.SetActive(hsvSlidersOn || rgbSlidersOn);
}
public float S
{
get
{
return _saturation;
}
set
{
if (_saturation == value)
return;
_saturation = value;
void Update()
{
#if UNITY_EDITOR
SetHSVSlidersOn(hsvSlidersOn);
SetRGBSlidersOn(rgbSlidersOn);
#endif
}
HSVChanged();
public Color CurrentColor
{
get
{
return new Color(_red, _green, _blue, _alpha);
}
set
{
if (CurrentColor == value)
return;
SendChangedEvent();
}
}
_red = value.r;
_green = value.g;
_blue = value.b;
_alpha = value.a;
public float V
{
get
{
return _brightness;
}
set
{
if (_brightness == value)
return;
RGBChanged();
_brightness = value;
SendChangedEvent();
}
}
HSVChanged();
private void Start()
{
SendChangedEvent();
}
SendChangedEvent();
}
}
public float H
{
get
{
return _hue;
}
set
{
if (_hue == value)
return;
public float R
{
get
{
return _red;
}
set
{
if (_red == value)
return;
_hue = value;
_red = value;
HSVChanged();
RGBChanged();
SendChangedEvent();
}
}
SendChangedEvent();
}
}
public float S
{
get
{
return _saturation;
}
set
{
if (_saturation == value)
return;
public float G
{
get
{
return _green;
}
set
{
if (_green == value)
return;
_saturation = value;
_green = value;
HSVChanged();
RGBChanged();
SendChangedEvent();
}
}
SendChangedEvent();
}
}
public float V
{
get
{
return _brightness;
}
set
{
if (_brightness == value)
return;
public float B
{
get
{
return _blue;
}
set
{
if (_blue == value)
return;
_brightness = value;
_blue = value;
HSVChanged();
RGBChanged();
SendChangedEvent();
}
}
SendChangedEvent();
}
}
public float R
{
get
{
return _red;
}
set
{
if (_red == value)
return;
private float A
{
get
{
return _alpha;
}
set
{
if (_alpha == value)
return;
_red = value;
_alpha = value;
RGBChanged();
SendChangedEvent();
}
}
SendChangedEvent();
}
}
private void RGBChanged()
{
HsvColor color = HSVUtil.ConvertRgbToHsv(CurrentColor);
public float G
{
get
{
return _green;
}
set
{
if (_green == value)
return;
_hue = color.NormalizedH;
_saturation = color.NormalizedS;
_brightness = color.NormalizedV;
}
_green = value;
private void HSVChanged()
{
Color color = HSVUtil.ConvertHsvToRgb(_hue * 360, _saturation, _brightness, _alpha);
RGBChanged();
_red = color.r;
_green = color.g;
_blue = color.b;
}
SendChangedEvent();
}
}
private void SendChangedEvent()
{
onValueChanged.Invoke(CurrentColor);
onHSVChanged.Invoke(_hue, _saturation, _brightness);
}
public float B
{
get
{
return _blue;
}
set
{
if (_blue == value)
return;
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;
}
}
_blue = value;
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("");
}
}
}
RGBChanged();
SendChangedEvent();
}
}
private float A
{
get
{
return _alpha;
}
set
{
if (_alpha == value)
return;
_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/
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions.ColorPicker
{
public class ColorPickerPresets : MonoBehaviour
{
public ColorPickerControl picker;
public GameObject[] presets;
public Image createPresetImage;
public class ColorPickerPresets : MonoBehaviour
{
public ColorPickerControl picker;
void Awake()
{
// picker.onHSVChanged.AddListener(HSVChanged);
picker.onValueChanged.AddListener(ColorChanged);
}
[SerializeField]
protected GameObject presetPrefab;
public void CreatePresetButton()
{
for (var i = 0; i < presets.Length; i++)
{
if (!presets[i].activeSelf)
{
presets[i].SetActive(true);
presets[i].GetComponent<Image>().color = picker.CurrentColor;
break;
}
}
}
[SerializeField]
protected int maxPresets = 16;
public void PresetSelect(Image sender)
{
picker.CurrentColor = sender.color;
}
[SerializeField]
protected Color[] predefinedPresets;
// Not working, it seems ConvertHsvToRgb() is broken. It doesn't work when fed
// input h, s, v as shown below.
// private void HSVChanged(float h, float s, float v)
// {
// createPresetImage.color = HSVUtil.ConvertHsvToRgb(h, s, v, 1);
// }
private void ColorChanged(Color color)
{
createPresetImage.color = color;
}
}
protected List<Color> presets = new List<Color>();
public Image createPresetImage;
public Transform createButton;
public enum SaveType { None, PlayerPrefs, JsonFile }
[SerializeField]
public SaveType saveMode = SaveType.None;
[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: