AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ShowIfNullAttribute.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 #if UNITY_EDITOR
6 using UnityEditor;
7 #endif
8 
9 namespace HoloToolkit.Unity
10 {
11  // Shows / hides based on whether named member is null
12  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
13  public sealed class ShowIfNullAttribute : ShowIfAttribute
14  {
15  public ShowIfNullAttribute(string nullableMemberName, bool showIfConditionMet = false)
16  {
17  MemberName = nullableMemberName;
18  ShowIfConditionMet = showIfConditionMet;
19  }
20 
21 #if UNITY_EDITOR
22  public override bool ShouldShow(object target)
23  {
24  bool isNullable = true;
25  if (target != null)
26  isNullable = IsNullable(target, MemberName);
27 
28  if (!isNullable)
29  throw new InvalidCastException("Member " + MemberName + " is not nullable.");
30 
31  UnityEngine.Object memberValue = (UnityEngine.Object)GetMemberValue(target, MemberName);
32  bool conditionMet = memberValue == null;
33  return ShowIfConditionMet ? conditionMet : !conditionMet;
34  }
35 #endif
36  }
37 }
ShowIfNullAttribute(string nullableMemberName, bool showIfConditionMet=false)