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

Further improvements to color picker control
pull/413/head
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,9 +1,10 @@
///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;
@ -19,6 +20,51 @@ namespace UnityEngine.UI.Extensions.ColorPicker
public ColorChangedEvent onValueChanged = new ColorChangedEvent();
public HSVChangedEvent onHSVChanged = new HSVChangedEvent();
[SerializeField]
bool hsvSlidersOn = true;
[SerializeField]
List<GameObject> hsvSliders = new List<GameObject>();
[SerializeField]
bool rgbSlidersOn = true;
[SerializeField]
List<GameObject> rgbSliders = new List<GameObject>();
[SerializeField]
GameObject alphaSlider;
public void SetHSVSlidersOn(bool value)
{
hsvSlidersOn = value;
foreach (var item in hsvSliders)
item.SetActive(value);
if (alphaSlider)
alphaSlider.SetActive(hsvSlidersOn || rgbSlidersOn);
}
public void SetRGBSlidersOn(bool value)
{
rgbSlidersOn = value;
foreach (var item in rgbSliders)
item.SetActive(value);
if (alphaSlider)
alphaSlider.SetActive(hsvSlidersOn || rgbSlidersOn);
}
void Update()
{
#if UNITY_EDITOR
SetHSVSlidersOn(hsvSlidersOn);
SetRGBSlidersOn(rgbSlidersOn);
#endif
}
public Color CurrentColor
{
get

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;
[SerializeField]
protected GameObject presetPrefab;
[SerializeField]
protected int maxPresets = 16;
[SerializeField]
protected Color[] predefinedPresets;
protected List<Color> presets = new List<Color>();
public Image createPresetImage;
public Transform createButton;
void Awake()
public enum SaveType { None, PlayerPrefs, JsonFile }
[SerializeField]
public SaveType saveMode = SaveType.None;
[SerializeField]
protected string playerPrefsKey;
public virtual string JsonFilePath
{
// picker.onHSVChanged.AddListener(HSVChanged);
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 void CreatePresetButton()
public virtual void CreatePresetButton()
{
for (var i = 0; i < presets.Length; i++)
CreatePreset(picker.CurrentColor);
}
public virtual void LoadPresets(SaveType saveType)
{
if (!presets[i].activeSelf)
string jsonData = "";
switch (saveType)
{
presets[i].SetActive(true);
presets[i].GetComponent<Image>().color = picker.CurrentColor;
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);
}
}
public void PresetSelect(Image sender)
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;
}
// 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)
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: