Created
June 7, 2014 14:09
-
-
Save DomDomHaas/bf1094ca7d0314165966 to your computer and use it in GitHub Desktop.
Unity Robust Data Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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