Tanoda
CombinablePropertyAttribute.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10#if UNITY_EDITOR
11using UnityEditor;
12#endif
13using System;
14using System.Reflection;
15using System.Collections.Generic;
16
17namespace Leap.Unity.Attributes {
18
19 using UnityObject = UnityEngine.Object;
20
21 public interface IPropertyConstrainer {
22#if UNITY_EDITOR
23 void ConstrainValue(SerializedProperty property);
24#endif
25 }
26
27 public interface IPropertyDisabler {
28#if UNITY_EDITOR
29 bool ShouldDisable(SerializedProperty property);
30#endif
31 }
32
33 public interface IFullPropertyDrawer {
34#if UNITY_EDITOR
35 void DrawProperty(Rect rect, SerializedProperty property, GUIContent label);
36#endif
37 }
38
39 public interface IAdditiveDrawer {
40#if UNITY_EDITOR
41 float GetWidth();
42 void Draw(Rect rect, SerializedProperty property);
43#endif
44 }
45
46 public interface ITopPanelDrawer {
47#if UNITY_EDITOR
48 float GetHeight();
49 void Draw(Rect panelRect, SerializedProperty property);
50#endif
51 }
52
53 public interface ISupportDragAndDrop {
54#if UNITY_EDITOR
55 Rect GetDropArea(Rect r, SerializedProperty property);
56 bool IsDropValid(UnityObject[] draggedObjects, SerializedProperty property);
57 void ProcessDroppedObjects(UnityObject[] droppedObjects, SerializedProperty property);
58#endif
59 }
60
65
66 public abstract class CombinablePropertyAttribute : PropertyAttribute {
67 private bool _isInitialized = false;
68
69 private FieldInfo _fieldInfo;
70 public FieldInfo fieldInfo {
71 get {
72 if (!_isInitialized) {
73 Debug.LogError("CombinablePropertyAttribute needed fieldInfo but was not "
74 + "initialized. Did you call Init()?");
75 }
76 return _fieldInfo;
77 }
78 protected set {
79 _fieldInfo = value;
80 }
81 }
82
83 private UnityObject[] _targets;
85 get {
86 if (!_isInitialized) {
87 Debug.LogError("CombinablePropertyAttribute needed fieldInfo but was not "
88 + "initialized. Did you call Init()?");
89 }
90 return _targets;
91 }
92 protected set {
93 _targets = value;
94 }
95 }
96
97#if UNITY_EDITOR
105 public void Init(SerializedProperty property) {
106 var propertyName = property.name;
107 var serializedObject = property.serializedObject;
108 var targetObjectType = serializedObject.targetObject.GetType();
109 fieldInfo = targetObjectType.GetField(propertyName,
110 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
111 | BindingFlags.FlattenHierarchy);
112
113 targets = property.serializedObject.targetObjects;
114
115 _isInitialized = true;
116 }
117
122 public void Init(FieldInfo fieldInfo, UnityObject[] targets) {
123 this.fieldInfo = fieldInfo;
124 this.targets = targets;
125
126 _isInitialized = true;
127 }
128
129 public virtual IEnumerable<SerializedPropertyType> SupportedTypes {
130 get {
131 yield break;
132 }
133 }
134
135 public virtual void OnPropertyChanged(SerializedProperty property) { }
136#endif
137 }
138}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Object UnityObject
Definition: EditorUtils.cs:19