Example #1
0
        private void OnGUI()
        {
            if (_style == null)
            {
                _style           = new GUIStyle(GUI.skin.button);
                _style.alignment = TextAnchor.MiddleLeft;
            }

            // The header.
            GUILayout.Label("Select a Type", EditorStyles.boldLabel);

            // Search text field.
            _searchString = GUILayout.TextField(_searchString ?? "");

            var i = 0;

            foreach (var type in _types)
            {
                // Skip non-matches.
                if (!type.Value.ToLower().Contains(_searchString.ToLower()))
                {
                    continue;
                }

                var simplerName = GetSimplerTypeName(type.Value);

                if (GUILayout.Button(simplerName, _style))
                {
                    // Create the new Syringe type wrapper.
                    var selectedType = new Syringe(Type.GetType(type.Key));

                    // Replace the value in the serialized property.
                    if (_arrayIndex > -1)
                    {
                        ((Syringe[])_fieldInfo.GetValue(_serializedProperty.serializedObject.targetObject))[_arrayIndex] = selectedType;
                    }
                    else if (_listIndex > -1)
                    {
                        ((List <Syringe>)_fieldInfo.GetValue(_serializedProperty.serializedObject.targetObject))[_listIndex] = selectedType;
                    }
                    else
                    {
                        _fieldInfo.SetValue(_serializedProperty.serializedObject.targetObject, selectedType);
                    }

                    // Mark the Target as Dirty
                    SyringePropertyDrawer.MarkObjectAsDirty(_serializedProperty.serializedObject.targetObject);

                    // Close the SyringeEditorWindow.
                    Close();
                }

                // Limit results.
                if (i++ > MaxResults)
                {
                    break;
                }
            }
        }
Example #2
0
        public static void Show(FieldInfo fieldInfo, SerializedProperty serializedProperty)
        {
            // Reset to Default Values
            _fieldInfo          = fieldInfo;
            _serializedProperty = serializedProperty;

            // Set _searchString to the value of the serialized property.
            var syringe = SyringePropertyDrawer.GetSelectedValueFromSerializedProperty(fieldInfo, serializedProperty, out _arrayIndex, out _listIndex);

            _searchString = syringe == null ? "" : syringe.FullName;

            // Open the SyringeEditorWindow near the InspectorWindow.
            GetWindowWithRect(typeof(SyringeEditorWindow), GetWindow(Type.GetType(typeof(EditorWindow).AssemblyQualifiedName.Replace("EditorWindow", "InspectorWindow"))).position, true, "Syringe - Select Type");
        }