Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
Created June 7, 2014 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DomDomHaas/bf1094ca7d0314165966 to your computer and use it in GitHub Desktop.
Save DomDomHaas/bf1094ca7d0314165966 to your computer and use it in GitHub Desktop.
Unity Robust Data Validation
using UnityEngine;
using System.Collections;
// script according to the talk at unit2013 : https://www.youtube.com/watch?v=Ozc_hXzp_KU
public class ValidateMe05 : MonoBehaviour
{
private const float minFloatValue = 0.1f;
private const float maxFloatValue = 15f;
[SerializeField]
[Range(minFloatValue, maxFloatValue)]
private float
myFloat;
public float MyFloatValue {
get{ return myFloat;}
set {
value = Mathf.Clamp (value, minFloatValue, maxFloatValue);
if (!Mathf.Approximately (value, myFloat)) {
myFloat = value;
}
}
}
private const int maxStringLength = 15;
[SerializeField]
private string
myString;
public string MyString {
get{ return myString;}
set {
//if (string.IsNullOrEmpty(value))
if (value == null)
myString = string.Empty;
if (value.Length > maxStringLength)
value = value.Substring (0, maxStringLength);
if (!value.Equals (myString))
myString = value;
}
}
#if UNITY_EDITOR
public void OnValidate ()
{
MyFloatValue = myFloat;
MyString = myString;
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment