Skip to content

Instantly share code, notes, and snippets.

@sabresaurus
Created October 16, 2019 19:17
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 sabresaurus/14ede7bcccdca15f141004a46989dab8 to your computer and use it in GitHub Desktop.
Save sabresaurus/14ede7bcccdca15f141004a46989dab8 to your computer and use it in GitHub Desktop.
Allows you to restrict visibility of serialised fields based on an attribute, e.g. [ConditionalVisibility("siblingBool","True"),SerializeField] float foo = 0;
// MIT License
//
// Copyright (c) 2019 Sabresaurus
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
public class ConditionalVisibilityAttribute : PropertyAttribute
{
public string MemberName { get; private set; }
public string TargetValue { get; private set; }
public ConditionalVisibilityAttribute(string memberName, string targetValue)
{
this.MemberName = memberName;
this.TargetValue = targetValue;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(ConditionalVisibilityAttribute))]
public class ConditionalVisibilityDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (IsVisible(property))
{
EditorGUI.PropertyField(position, property, label);
}
}
bool IsVisible(SerializedProperty property)
{
ConditionalVisibilityAttribute conditionalAttribute = (ConditionalVisibilityAttribute) attribute;
// Find the property that the visibility is conditional upon
string conditionPath = property.propertyPath.Replace(property.name, conditionalAttribute.MemberName);
SerializedProperty matchedProperty = property.serializedObject.FindProperty(conditionPath);
if (matchedProperty.propertyType == SerializedPropertyType.ObjectReference)
{
if (conditionalAttribute.TargetValue == "[NotNull]" && matchedProperty.objectReferenceValue == null)
{
// Desire not null, but object is null, fail
return false;
}
else if (conditionalAttribute.TargetValue == "[Null]" && matchedProperty.objectReferenceValue != null)
{
// Desire not null, but object is not null, fail
return false;
}
else
{
// All criteria met, or none understood, no reason to hide it either way
return true;
}
}
else
{
string stringValue = GetStringValue(matchedProperty);
return string.Equals(stringValue, conditionalAttribute.TargetValue);
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (IsVisible(property))
{
return base.GetPropertyHeight(property, label);
}
else
{
return 0;
}
}
static string GetStringValue(SerializedProperty property)
{
if (property.propertyType == SerializedPropertyType.Enum)
{
return property.enumNames[property.enumValueIndex];
}
else if (property.propertyType == SerializedPropertyType.String)
{
return property.stringValue;
}
else if (property.propertyType == SerializedPropertyType.Boolean)
{
return property.boolValue.ToString();
}
else
{
throw new System.NotImplementedException();
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment