Cleanup and ensuring the UIParticleSystem is disposed on Destroy correctly.

Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/412
development
Simon Jackson 2023-01-03 11:42:22 +00:00
parent 60eed9b87a
commit 12f6174469
6 changed files with 149 additions and 146 deletions

View File

@ -6,82 +6,82 @@ namespace UnityEngine.UI.Extensions.ColorPicker
{ {
[RequireComponent(typeof(Text))] [RequireComponent(typeof(Text))]
public class ColorLabel : MonoBehaviour public class ColorLabel : MonoBehaviour
{
public ColorPickerControl picker;
public ColorValues type;
public string prefix = "R: ";
public float minValue = 0;
public float maxValue = 255;
public int precision = 0;
private Text label;
private void Awake()
{ {
label = GetComponent<Text>(); public ColorPickerControl picker;
} public ColorValues type;
private void OnEnable() public string prefix = "R: ";
{ public float minValue = 0;
if (Application.isPlaying && picker != null) public float maxValue = 255;
public int precision = 0;
private Text label;
private void Awake()
{ {
picker.onValueChanged.AddListener(ColorChanged); label = GetComponent<Text>();
picker.onHSVChanged.AddListener(HSVChanged);
}
}
private void OnDestroy()
{
if (picker != null)
{
picker.onValueChanged.RemoveListener(ColorChanged);
picker.onHSVChanged.RemoveListener(HSVChanged);
} }
}
private void OnEnable()
{
if (Application.isPlaying && picker != null)
{
picker.onValueChanged.AddListener(ColorChanged);
picker.onHSVChanged.AddListener(HSVChanged);
}
}
private void OnDestroy()
{
if (picker != null)
{
picker.onValueChanged.RemoveListener(ColorChanged);
picker.onHSVChanged.RemoveListener(HSVChanged);
}
}
#if UNITY_EDITOR #if UNITY_EDITOR
private void OnValidate() private void OnValidate()
{ {
label = GetComponent<Text>(); label = GetComponent<Text>();
UpdateValue(); UpdateValue();
} }
#endif #endif
private void ColorChanged(Color color) private void ColorChanged(Color color)
{
UpdateValue();
}
private void HSVChanged(float hue, float sateration, float value)
{
UpdateValue();
}
private void UpdateValue()
{
if (picker == null)
{ {
label.text = prefix + "-"; UpdateValue();
} }
else
{
float value = minValue + (picker.GetValue(type) * (maxValue - minValue));
label.text = prefix + ConvertToDisplayString(value); private void HSVChanged(float hue, float sateration, float value)
{
UpdateValue();
}
private void UpdateValue()
{
if (picker == null)
{
label.text = prefix + "-";
}
else
{
float value = minValue + (picker.GetValue(type) * (maxValue - minValue));
label.text = prefix + ConvertToDisplayString(value);
}
}
private string ConvertToDisplayString(float value)
{
if (precision > 0)
return value.ToString("f " + precision);
else
return Mathf.FloorToInt(value).ToString();
} }
} }
private string ConvertToDisplayString(float value)
{
if (precision > 0)
return value.ToString("f " + precision);
else
return Mathf.FloorToInt(value).ToString();
}
}
} }

View File

@ -59,7 +59,9 @@ namespace UnityEngine.UI.Extensions.ColorPicker
private void OnDestroy() private void OnDestroy()
{ {
if (image.texture != null) if (image.texture != null)
{
DestroyImmediate(image.texture); DestroyImmediate(image.texture);
}
} }
#if UNITY_EDITOR #if UNITY_EDITOR

View File

@ -7,95 +7,95 @@ namespace UnityEngine.UI.Extensions.ColorPicker
{ {
[RequireComponent(typeof(InputField))] [RequireComponent(typeof(InputField))]
public class HexColorField : MonoBehaviour public class HexColorField : MonoBehaviour
{
public ColorPickerControl ColorPicker;
public bool displayAlpha;
private InputField hexInputField;
private const string hexRegex = "^#?(?:[0-9a-fA-F]{3,4}){1,2}$";
private void Awake()
{ {
hexInputField = GetComponent<InputField>(); public ColorPickerControl ColorPicker;
// Add listeners to keep text (and color) up to date public bool displayAlpha;
hexInputField.onEndEdit.AddListener(UpdateColor);
ColorPicker.onValueChanged.AddListener(UpdateHex);
}
private void OnDestroy() private InputField hexInputField;
{
hexInputField.onValueChanged.RemoveListener(UpdateColor);
ColorPicker.onValueChanged.RemoveListener(UpdateHex);
}
private void UpdateHex(Color newColor) private const string hexRegex = "^#?(?:[0-9a-fA-F]{3,4}){1,2}$";
{
hexInputField.text = ColorToHex(newColor);
}
private void UpdateColor(string newHex) private void Awake()
{
Color32 color;
if (HexToColor(newHex, out color))
ColorPicker.CurrentColor = color;
else
Debug.Log("hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
}
private string ColorToHex(Color32 color)
{
if (displayAlpha)
return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.r, color.g, color.b, color.a);
else
return string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b);
}
public static bool HexToColor(string hex, out Color32 color)
{
// Check if this is a valid hex string (# is optional)
if (System.Text.RegularExpressions.Regex.IsMatch(hex, hexRegex))
{ {
int startIndex = hex.StartsWith("#") ? 1 : 0; hexInputField = GetComponent<InputField>();
if (hex.Length == startIndex + 8) //#RRGGBBAA // Add listeners to keep text (and color) up to date
{ hexInputField.onEndEdit.AddListener(UpdateColor);
color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier), ColorPicker.onValueChanged.AddListener(UpdateHex);
byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 6, 2), NumberStyles.AllowHexSpecifier));
}
else if (hex.Length == startIndex + 6) //#RRGGBB
{
color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
255);
}
else if (hex.Length == startIndex + 4) //#RGBA
{
color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 3] + hex[startIndex + 3], NumberStyles.AllowHexSpecifier));
}
else //#RGB
{
color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
255);
}
return true;
} }
else
private void OnDestroy()
{ {
color = new Color32(); hexInputField.onValueChanged.RemoveListener(UpdateColor);
return false; ColorPicker.onValueChanged.RemoveListener(UpdateHex);
}
private void UpdateHex(Color newColor)
{
hexInputField.text = ColorToHex(newColor);
}
private void UpdateColor(string newHex)
{
Color32 color;
if (HexToColor(newHex, out color))
ColorPicker.CurrentColor = color;
else
Debug.Log("hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
}
private string ColorToHex(Color32 color)
{
if (displayAlpha)
return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.r, color.g, color.b, color.a);
else
return string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b);
}
public static bool HexToColor(string hex, out Color32 color)
{
// Check if this is a valid hex string (# is optional)
if (System.Text.RegularExpressions.Regex.IsMatch(hex, hexRegex))
{
int startIndex = hex.StartsWith("#") ? 1 : 0;
if (hex.Length == startIndex + 8) //#RRGGBBAA
{
color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 6, 2), NumberStyles.AllowHexSpecifier));
}
else if (hex.Length == startIndex + 6) //#RRGGBB
{
color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
255);
}
else if (hex.Length == startIndex + 4) //#RGBA
{
color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 3] + hex[startIndex + 3], NumberStyles.AllowHexSpecifier));
}
else //#RGB
{
color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
255);
}
return true;
}
else
{
color = new Color32();
return false;
}
} }
} }
}
} }

View File

@ -52,7 +52,9 @@ namespace UnityEngine.UI.Extensions.ColorPicker
private void OnDestroy() private void OnDestroy()
{ {
if (image.texture != null) if (image.texture != null)
{
DestroyImmediate(image.texture); DestroyImmediate(image.texture);
}
} }
#if UNITY_EDITOR #if UNITY_EDITOR

View File

@ -96,10 +96,8 @@ namespace UnityEngine.UI.Extensions
{ {
effectRoot.SetActive(true); effectRoot.SetActive(true);
} }
{
}
} }
void OnDestroy() void OnDestroy()
{ {
if (!Application.isPlaying) if (!Application.isPlaying)

View File

@ -388,6 +388,7 @@ namespace UnityEngine.UI.Extensions
{ {
currentMaterial = null; currentMaterial = null;
currentTexture = null; currentTexture = null;
base.OnDestroy();
} }
public void StartParticleEmission() public void StartParticleEmission()