/// <summary>
        /// Draws a dropdown with all available profiles of types contained in the array profilyTypes.
        /// </summary>
        /// <returns>True if property was changed.</returns>
        public static bool DrawProfileDropDownList(SerializedProperty property, BaseMixedRealityProfile profile, Object oldProfileObject, Type[] profileTypes, bool requiresProfile, bool showAddButton)
        {
            bool changed = false;

            using (new EditorGUILayout.HorizontalScope())
            {
                List<ScriptableObject> profileInstanceList = new List<ScriptableObject>();
                List<GUIContent> profileContentList = new List<GUIContent>();

                // Pull profile instances and profile content from cache
                for(int i = 0; i < profileTypes.Length; i++)
                {
                    Type profileType = profileTypes[i];
                    profileInstanceList.AddRange(MixedRealityProfileUtility.GetProfilesOfType(profileType));
                    profileContentList.AddRange(MixedRealityProfileUtility.GetProfilePopupOptionsByType(profileType));
                }

                int dropdownOffset = 0;
                if (!requiresProfile || profileInstanceList.Count == 0)
                {
                    profileContentList.Insert(0, new GUIContent("(None)"));
                    dropdownOffset = 1;
                }

                ScriptableObject[] profileInstances = profileInstanceList.ToArray();
                GUIContent[] profileContent = profileContentList.ToArray();

                int selectedIndex = 0;
                // Find our selected index
                for (int i = 0; i < profileInstances.Length; i++)
                {
                    if (profileInstances[i] == oldProfileObject)
                    {   // If a profile is required, then the selected index is the same as the profile instance index, otherwise it is offset by the dropdownOffset
                        // due to the pre-existing (None) option
                        selectedIndex = i + dropdownOffset;
                        break;
                    }
                }

                int newIndex = EditorGUILayout.Popup(
                    new GUIContent(oldProfileObject != null ? "" : property.displayName),
                    selectedIndex,
                    profileContent,
                    GUILayout.ExpandWidth(true));

                int profileInstanceIndex = newIndex - dropdownOffset;
                property.objectReferenceValue = (profileInstanceIndex >= 0) ? profileInstances[profileInstanceIndex] : null;
                changed = property.objectReferenceValue != oldProfileObject;

                // Draw a button that finds the profile in the project window
                if (property.objectReferenceValue != null)
                {
                    // The view asset button should always be enabled.
                    using (new GUIEnabledWrapper())
                    {
                        if (GUILayout.Button("View Asset", EditorStyles.miniButton, GUILayout.Width(80)))
                        {
                            EditorGUIUtility.PingObject(property.objectReferenceValue);
                        }
                    }
                }

                // Draw the clone button
                if (property.objectReferenceValue == null)
                {
                    if (showAddButton && MixedRealityProfileUtility.IsConcreteProfileType(Selection.activeObject.GetType()))
                    {
                        if (GUILayout.Button(NewProfileContent, EditorStyles.miniButton, GUILayout.Width(20f)))
                        {
                            ScriptableObject instance = ScriptableObject.CreateInstance(Selection.activeObject.GetType());
                            var newProfile = instance.CreateAsset(AssetDatabase.GetAssetPath(Selection.activeObject)) as BaseMixedRealityProfile;
                            property.objectReferenceValue = newProfile;
                            property.serializedObject.ApplyModifiedProperties();
                            changed = true;
                        }
                    }
                }
                else
                {
                    var renderedProfile = property.objectReferenceValue as BaseMixedRealityProfile;
                    Debug.Assert(renderedProfile != null);
                    if (GUILayout.Button(new GUIContent("Clone", "Replace with a copy of the default profile."), EditorStyles.miniButton, GUILayout.Width(45f)))
                    {
                        MixedRealityProfileCloneWindow.OpenWindow(profile, renderedProfile, property);
                    }
                }
            }

            return changed;
        }
Example #2
0
        /// <summary>
        /// Draws a dropdown with all available profiles of profilyType.
        /// </summary>
        /// <returns>True if property was changed.</returns>
        public static bool DrawProfileDropDownList(SerializedProperty property, BaseMixedRealityProfile profile, Object oldProfileObject, Type profileType, bool showAddButton)
        {
            bool changed = false;

            using (new EditorGUILayout.HorizontalScope())
            {
                // Pull profile instances and profile content from cache
                ScriptableObject[] profileInstances = MixedRealityProfileUtility.GetProfilesOfType(profileType);
                GUIContent[]       profileContent   = MixedRealityProfileUtility.GetProfilePopupOptionsByType(profileType);
                // Set our selected index to our '(None)' option by default
                int selectedIndex = 0;
                // Find our selected index
                for (int i = 0; i < profileInstances.Length; i++)
                {
                    if (profileInstances[i] == oldProfileObject)
                    {   // Our profile content has a '(None)' option at the start
                        selectedIndex = i + 1;
                        break;
                    }
                }

                int newIndex = EditorGUILayout.Popup(
                    new GUIContent(oldProfileObject != null ? "" : property.displayName),
                    selectedIndex,
                    profileContent,
                    GUILayout.ExpandWidth(true));

                property.objectReferenceValue = (newIndex > 0) ? profileInstances[newIndex - 1] : null;
                changed = property.objectReferenceValue != oldProfileObject;

                // Draw a button that finds the profile in the project window
                if (property.objectReferenceValue != null)
                {
                    // The view asset button should always be enabled.
                    using (new GUIEnabledWrapper())
                    {
                        if (GUILayout.Button("View Asset", EditorStyles.miniButton, GUILayout.Width(80)))
                        {
                            EditorGUIUtility.PingObject(property.objectReferenceValue);
                        }
                    }
                }

                // Draw the clone button
                if (property.objectReferenceValue == null)
                {
                    if (showAddButton && MixedRealityProfileUtility.IsConcreteProfileType(profileType))
                    {
                        if (GUILayout.Button(NewProfileContent, EditorStyles.miniButton, GUILayout.Width(20f)))
                        {
                            ScriptableObject instance = ScriptableObject.CreateInstance(profileType);
                            var newProfile            = instance.CreateAsset(AssetDatabase.GetAssetPath(Selection.activeObject)) as BaseMixedRealityProfile;
                            property.objectReferenceValue = newProfile;
                            property.serializedObject.ApplyModifiedProperties();
                            changed = true;
                        }
                    }
                }
                else
                {
                    var renderedProfile = property.objectReferenceValue as BaseMixedRealityProfile;
                    Debug.Assert(renderedProfile != null);
                    if (GUILayout.Button(new GUIContent("Clone", "Replace with a copy of the default profile."), EditorStyles.miniButton, GUILayout.Width(45f)))
                    {
                        MixedRealityProfileCloneWindow.OpenWindow(profile, renderedProfile, property);
                    }
                }
            }

            return(changed);
        }