public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (!initialized) { if (property.type.EndsWith("Event")) { drawerOverride = new UnityEditorInternal.UnityEventDrawer(); } if (property.type.EndsWith("Map")) { drawerOverride = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(property.type + "Drawer") as PropertyDrawer; } initialized = true; } if (IsConditionMet(property)) { // 条件满足,开始绘制 LabelDrawer.DrawLabel(position, property, Attr.Label, Attr.Const, drawerOverride); } else if (Attr.AlwaysShow && Event.current.type == EventType.Repaint) { var tc = GUI.color; GUI.color = Color.gray; LabelDrawer.DrawLabel(position, property, Attr.Label, Attr.Const, drawerOverride); GUI.color = tc; } }
public static PropertyDrawer GetCustomPropertyDrawer(FieldInfo fieldInfo) { // Getting the field type this way assumes that the property instance is not a managed reference (with a SerializeReference attribute); if it was, it should be retrieved in a different way: Type fieldType = fieldInfo.FieldType; Type propertyDrawerType = (Type)Type.GetType("UnityEditor.ScriptAttributeUtility,UnityEditor") .GetMethod("GetDrawerTypeForType", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public) .Invoke(null, new object[] { fieldType }); PropertyDrawer propertyDrawer = null; if (typeof(PropertyDrawer).IsAssignableFrom(propertyDrawerType)) { propertyDrawer = (PropertyDrawer)Activator.CreateInstance(propertyDrawerType); } if (propertyDrawer != null) { typeof(PropertyDrawer) .GetField("m_FieldInfo", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .SetValue(propertyDrawer, fieldInfo); } return(propertyDrawer); }
public PropertyDictionaryProxy(SerializedProperty property, SerializedProperty keys, SerializedProperty values, PropertyDrawer drawer) { _property = property; _keysProperty = keys; _valuesProperty = values; _drawer = drawer; }
public ImGuiDrawer(SerializedProperty property, PropertyDrawer drawer) { _property = property; _drawer = drawer; onGUIHandler = OnGuiHandler; }
protected virtual float GetElementHeight(SerializedProperty property) { int elementIndex = List.IndexOf(property); PropertyDrawer drawer = GetDrawer(Target.Components[elementIndex].GetType()); return(drawer.GetPropertyHeight(property, GUIContent.none) + EditorGUIUtility.singleLineHeight + POPUP_SPACING); }
public static void DrawRangeField(this PropertyDrawer drawer, Rect position, SerializedProperty prop, GUIContent label, bool floatingPoint) { position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); EditorGUIUtility.labelWidth = 30.0f; EditorGUIUtility.fieldWidth = 60.0f; float width = 100.0f; float spacing = 10.0f; position.x -= (EditorGUI.indentLevel * 15.0f); position.width = width; if (floatingPoint) { DrawFloatTextField(drawer, position, "Min", "Minimum value", prop.FindPropertyRelative("Minimum")); } else { DrawIntTextField(drawer, position, "Min", "Minimum value", prop.FindPropertyRelative("Minimum")); } position.x = position.xMax + spacing; position.width = width; if (floatingPoint) { DrawFloatTextField(drawer, position, "Max", "Maximum value", prop.FindPropertyRelative("Maximum")); } else { DrawIntTextField(drawer, position, "Max", "Maximum value", prop.FindPropertyRelative("Maximum")); } }
private void CachePropertyDrawer() { if (cachedPropertyDrawer == null) { cachedPropertyDrawer = PropertyDrawerUtility.GetCustomPropertyDrawer(fieldInfo); } }
public static void DrawRangeField(this PropertyDrawer drawer, Rect position, SerializedProperty prop, bool floatingPoint) { EditorGUIUtility.labelWidth = 30.0f; EditorGUIUtility.fieldWidth = 40.0f; float width = position.width * 0.49f; float spacing = position.width * 0.02f; position.width = width; if (floatingPoint) { DrawFloatTextField(drawer, position, "Min", "Minimum value", prop.FindPropertyRelative("Minimum")); } else { DrawIntTextField(drawer, position, "Min", "Minimum value", prop.FindPropertyRelative("Minimum")); } position.x = position.xMax + spacing; position.width = width; if (floatingPoint) { DrawFloatTextField(drawer, position, "Max", "Maximum value", prop.FindPropertyRelative("Maximum")); } else { DrawIntTextField(drawer, position, "Max", "Maximum value", prop.FindPropertyRelative("Maximum")); } }
public static void SaveSelectedPropertyDrawerTargetObject <T>(PropertyDrawer propertyDrawer, SerializedProperty property, T[] newValues) { for (int i = 0; i < property.serializedObject.targetObjects.Length; i++) { propertyDrawer.fieldInfo.SetValue(property.serializedObject.targetObjects[i], newValues[i]); } }
PropertyDrawer GetDefaultPropertyDrawer(SerializedProperty _property) { if (m_DefaultPropertyDrawer != null) { return(m_DefaultPropertyDrawer); } FieldInfo targetField = _property.GetFieldInfo(); IEnumerable <Attribute> attributes = targetField.GetCustomAttributes(); int order = attribute.order + 1; if (order >= attributes.Count()) { return(null); } Attribute nextAttribute = attributes.ElementAt(order); Type attributeType = nextAttribute.GetType(); Type propertyDrawerType = (Type)Type.GetType("UnityEditor.ScriptAttributeUtility,UnityEditor").GetMethod("GetDrawerTypeForType", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new object[] { attributeType }); m_DefaultPropertyDrawer = (PropertyDrawer)Activator.CreateInstance(propertyDrawerType); s_PropertyDrawerType.GetField("m_FieldInfo", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(m_DefaultPropertyDrawer, targetField); s_PropertyDrawerType.GetField("m_Attribute", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(m_DefaultPropertyDrawer, nextAttribute); m_DefaultPropertyDrawer.attribute.order = order; return(m_DefaultPropertyDrawer); }
public static int GetRepresentedArrayIndex(this PropertyDrawer drawer, SerializedProperty property) { if (!drawer.RepresentAnArray()) { return(-1); } string propertyPath = property.propertyPath; int startIndex = propertyPath.Length - 1; int length = 0; while (propertyPath[startIndex - 1] != '[') { startIndex--; length++; } if (!int.TryParse(propertyPath.Substring(startIndex, length), out int ret)) { return(-1); } else { return(ret); } }
public static void SetFieldInfo(this PropertyDrawer drawer, FieldInfo fieldInfo) { var fieldInfoField = typeof(PropertyDrawer).GetField("m_FieldInfo", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new NullReferenceException("m_FieldInfo field was not found in PropertyDrawer class."); fieldInfoField.SetValue(drawer, fieldInfo); }
public static void SetAttribute(this PropertyDrawer drawer, PropertyAttribute attribute) { var attributeField = typeof(PropertyDrawer).GetField("m_Attribute", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new NullReferenceException("m_Attribute field was not found in PropertyDrawer class."); attributeField.SetValue(drawer, attribute); }
/// <summary> /// Adds the unity variable of type to list. /// </summary> /// <param name="name">Name.</param> /// <param name="type">Type.</param> /// <param name="drawer">Drawer.</param> void AddUnityVariableOfTypeToList(String name, Type type, PropertyDrawer drawer = null) { ReorderableList list = __variablesReordableList; var index = list.serializedProperty.arraySize; list.serializedProperty.arraySize++; list.index = index; UnityVariable variable = UnityVariable.CreateInstanceOf(type); variable.name = name; variable.drawer = drawer; // UnityVariable variable = (UnityVariable)ScriptableObject.CreateInstance<UnityVariable> (); // variable.name = name; // variable.drawer = drawer; // variable.Value = value; var element = list.serializedProperty.GetArrayElementAtIndex(index); element.objectReferenceValue = variable; serializedObject.ApplyModifiedProperties(); }
/// <summary> /// Gets the property drawer for the supplied field. /// </summary> /// <returns>The property drawer for the supplied field.</returns> /// <param name="field">Field.</param> /// <param name="propertyAttribute">Property attribute.</param> public static PropertyDrawer GetPropertyDrawer(this FieldInfo field, PropertyAttribute propertyAttribute) { System.Type fieldType = GetIListElementType(field.FieldType) ?? field.FieldType; System.Type propertyAttributeType = propertyAttribute == null ? null : propertyAttribute.GetType(); // early out of there is no PropertyDrawer if ( !( drawersForEachType.ContainsKey(fieldType) || (propertyAttributeType != null && drawersForEachType.ContainsKey(propertyAttributeType)) ) ) { return(null); } // instantiate the new property drawer bool isTypeDrawer = drawersForEachType.ContainsKey(fieldType); ConstructorInfo constructor = drawersForEachType[ isTypeDrawer ? fieldType : propertyAttributeType ].GetConstructor(new System.Type[0]); PropertyDrawer result = constructor.Invoke(null) as PropertyDrawer; // configure the property drawer's private fields if (isTypeDrawer) { propertyDrawerAttributeField.SetValue(result, null); } else { propertyDrawerAttributeField.SetValue(result, propertyAttribute); } propertyDrawerFieldInfoField.SetValue(result, field); return(result); }
protected virtual void Init(PropertyAttribute[] attribs) { var fieldType = _fieldInfo.FieldType; if (fieldType.IsListType()) { fieldType = fieldType.GetElementTypeOfListType(); } var fieldTypePropertyDrawerType = ScriptAttributeUtility.GetDrawerTypeForType(fieldType); if (fieldTypePropertyDrawerType != null && TypeUtil.IsType(fieldTypePropertyDrawerType, typeof(PropertyDrawer))) { _drawer = PropertyDrawerActivator.Create(fieldTypePropertyDrawerType, null, _fieldInfo); if (_drawer != null && _fieldInfo.FieldType.IsListType()) { _drawer = new ArrayPropertyDrawer(_drawer); } this.InternalDrawer = _drawer; } foreach (var attrib in attribs) { this.HandleAttribute(attrib, _fieldInfo, fieldType); } }
public ConditionalRenderer(string name, PropertyDrawer drawer, Func <SerializedProperty, GUIContent, float> getPropertyHeight, bool normalBooleanValue) { this.name = name; this.drawer = drawer; this.getPropertyHeight = getPropertyHeight; this.normalBooleanValue = normalBooleanValue; }
/// <summary> /// Extract Property and Decorator drawers for property /// </summary> public void HandleDrawnType(Type drawnType, Type propertyType, FieldInfo field, PropertyAttribute attribute) { Type drawerTypeForType = StratusScriptAttributeUtility.GetDrawerTypeForType(drawnType); if (drawerTypeForType != null) { if (typeof(PropertyDrawer).IsAssignableFrom(drawerTypeForType)) { if (propertyType != null && propertyType.IsArrayOrList()) { return; } propertyDrawer = (PropertyDrawer)Activator.CreateInstance(drawerTypeForType); propertyDrawer.SetFieldInfo(field); propertyDrawer.SetAttribute(attribute); } else if (typeof(DecoratorDrawer).IsAssignableFrom(drawerTypeForType)) { if (field != null && field.FieldType.IsArrayOrList() && !propertyType.IsArrayOrList()) { return; } DecoratorDrawer decoratorDrawer = (DecoratorDrawer)Activator.CreateInstance(drawerTypeForType); decoratorDrawer.SetAttribute(attribute); if (decoratorDrawers == null) { decoratorDrawers = new List <DecoratorDrawer>(); } decoratorDrawers.Add(decoratorDrawer); } } }
public static float GetHeight(SerializedProperty property, GUIContent label, PropertyDrawer drawerOverride = null) { if (drawerOverride != null) { return(drawerOverride.GetPropertyHeight(property, label)); } return(EditorGUI.GetPropertyHeight(property, label) + 12); }
/// <summary> /// If the target property has a [Tooltip] attribute, load it into its label. /// </summary> /// <param name="self"></param> /// <param name="label"></param> public static void LoadAttributeTooltip(PropertyDrawer self, GUIContent label) { var tooltipAttribute = from a in self.fieldInfo.GetCustomAttributes(typeof(TooltipAttribute), true).First() select a as TooltipAttribute; label.tooltip = tooltipAttribute.Match( some: a => a.tooltip, none: () => ""); }
public static void InitializePropertyDrawer(PropertyDrawer drawer, PropertyAttribute attrib, System.Reflection.FieldInfo fieldInfo) { if (drawer == null) { throw new System.ArgumentNullException("drawer"); } DynamicUtil.SetValue(drawer, "m_Attribute", attrib); DynamicUtil.SetValue(drawer, "m_FieldInfo", fieldInfo); }
/// <summary> /// If the target property has a [Tooltip] attribute, load it into its label. /// </summary> /// <param name="self"></param> /// <param name="label"></param> public static void LoadAttributeTooltip(PropertyDrawer self, GUIContent label) { var tooltipAttribute = self.fieldInfo.GetCustomAttributes(typeof(TooltipAttribute), true).FirstOrDefault() as TooltipAttribute; if (tooltipAttribute != null) { label.tooltip = tooltipAttribute.tooltip; } }
protected virtual void DrawBehaviourInspector(Rect rect, PropertyDrawer drawer) { if (Target.Behaviour == null) { return; } drawer.OnGUI(rect, Behaviour, GUIContent.none); }
public static void SetAttribute(PropertyDrawer drawer, PropertyAttribute attr) { if (attributeFieldInfo == null) { attributeFieldInfo = typeof(PropertyDrawer).GetField("m_Attribute", BindingFlags.Instance | BindingFlags.NonPublic); } if (attributeFieldInfo != null) { attributeFieldInfo.SetValue(drawer, attr); } }
public static void SetFieldInfo(PropertyDrawer drawer, FieldInfo info) { if (fieldInfoFieldInfo == null) { fieldInfoFieldInfo = typeof(PropertyDrawer).GetField("m_FieldInfo", BindingFlags.Instance | BindingFlags.NonPublic); } if (fieldInfoFieldInfo != null) { fieldInfoFieldInfo.SetValue(drawer, info); } }
public static T[] GetSelectedPropertyDrawerTargetObject <T>(PropertyDrawer propertyDrawer, SerializedProperty property) { T[] selectedStructs = new T[property.serializedObject.targetObjects.Length]; for (int i = 0; i < property.serializedObject.targetObjects.Length; i++) { selectedStructs[i] = (T)propertyDrawer.fieldInfo.GetValue(property.serializedObject.targetObjects[i]); } return(selectedStructs); }
public static void OnGUISafe(PropertyDrawer drawer, Rect position, SerializedProperty property, GUIContent label) { if (onGUISafeMethodInfo == null) { onGUISafeMethodInfo = typeof(PropertyDrawer).GetMethod("OnGUISafe", BindingFlags.Instance | BindingFlags.NonPublic); } if (onGUISafeMethodInfo != null) { onGUISafeMethodInfo.Invoke(drawer, new object[] { position, property, label }); } }
private void Init() { var dtp = ScriptAttributeUtility.GetDrawerTypeForType(_attrib.GetType()); var drawer = PropertyDrawerActivator.Create(dtp, _attrib, _fieldInfo); if (drawer is PropertyModifier) { (drawer as PropertyModifier).Init(true); } _visibleDrawer = drawer; }
private void Init(SerializedProperty property) { _attributes = null; _modifiers = null; _visibleDrawer = null; _attributes = (from a in this.fieldInfo.GetCustomAttributes(typeof(PropertyAttribute), true) where !(a is ModifierChainAttribute) orderby(a as PropertyAttribute).order ascending select a as PropertyAttribute).ToArray(); var propDrawerTp = typeof(PropertyDrawer); var lst = new List <PropertyModifier>(); if (_attributes.Length > 0) { for (int i = 0; i < _attributes.Length - 1; i++) { var attrib = _attributes[i]; if (attrib is PropertyModifierAttribute) { var dtp = ScriptAttributeUtility.GetDrawerTypeForType(attrib.GetType()); if (TypeUtil.IsType(dtp, typeof(PropertyModifier))) { var drawer = PropertyDrawerActivator.Create(dtp) as PropertyModifier; if (drawer == null) { continue; } PropertyDrawerActivator.InitializePropertyDrawer(drawer, attrib, this.fieldInfo); drawer.Init(false); lst.Add(drawer); } } } _modifiers = lst.ToArray(); var lastAttrib = _attributes.Last(); var lastDrawerTp = ScriptAttributeUtility.GetDrawerTypeForType(lastAttrib.GetType()); if (TypeUtil.IsType(lastDrawerTp, typeof(PropertyDrawer))) { var drawer = PropertyDrawerActivator.Create(lastDrawerTp, lastAttrib, this.fieldInfo); if (drawer is PropertyModifier) { (drawer as PropertyModifier).Init(true); } _visibleDrawer = drawer; } } _initialized = true; }
public static object CreatePropertyHandler(PropertyDrawer drawer) { if (!IsAvailable) { return(null); } object handler = PropertyHandler_Create(); PropertyHandler_SetPropertyDrawer(handler, drawer); return(handler); }