Example #1
0
        public static void DrawComponents(IContext context, IEntity entity)
        {
            var unfoldedComponents    = getUnfoldedComponents(context);
            var componentMemberSearch = getComponentMemberSearch(context);

            EntitasEditorLayout.BeginVerticalBox();
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("Components (" + entity.GetComponents().Length + ")", EditorStyles.boldLabel);
                    if (EntitasEditorLayout.MiniButtonLeft("▸"))
                    {
                        for (int i = 0; i < unfoldedComponents.Length; i++)
                        {
                            unfoldedComponents[i] = false;
                        }
                    }
                    if (EntitasEditorLayout.MiniButtonRight("▾"))
                    {
                        for (int i = 0; i < unfoldedComponents.Length; i++)
                        {
                            unfoldedComponents[i] = true;
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Space();

                var index = drawAddComponentMenu(context);
                if (index >= 0)
                {
                    var componentType = entity.ContextInfo.ComponentTypes[index];
                    var component     = entity.CreateComponent(index, componentType);
                    entity.AddComponent(index, component);
                }

                EditorGUILayout.Space();

                componentNameSearchString = EntitasEditorLayout.SearchTextField(componentNameSearchString);

                EditorGUILayout.Space();

                var indices    = entity.GetComponentIndices();
                var components = entity.GetComponents();
                for (int i = 0; i < components.Length; i++)
                {
                    DrawComponent(unfoldedComponents, componentMemberSearch, context, entity, indices[i], components[i]);
                }
            }
            EntitasEditorLayout.EndVerticalBox();
        }
        public override void OnInspectorGUI()
        {
            var creator = target as EntityCreator;

            _serializedCreator = new SerializedObject(creator);
            _transform         = creator.transform;
            var componentsField = creator.GetType().GetField("_components", BindingFlags.NonPublic | BindingFlags.Instance);
            var componentsProp  = _serializedCreator.FindProperty("_components");

            components = (componentsField.GetValue(creator) as SerializableComponent[])?.ToList() ?? new List <SerializableComponent>();

            components = components.FindAll(c => c != null);
            indices    = components.ConvertAll(c => Array.FindIndex(GameComponentsLookup.componentTypes, t => t == c.GetType()));

            if (expendNewComponent)
            {
                componentsProp.GetArrayElementAtIndex(components.Count - 1).isExpanded = true;
                expendNewComponent = false;
            }

            EntitasEditorLayout.BeginVerticalBox();
            {
                EditorGUILayout.LabelField($"Components ({components.Count})", EditorStyles.boldLabel);
                EditorGUILayout.Space();
                AddComponentMenu();
                EditorGUILayout.Space();
                componentNameSearchString = EntitasEditorLayout.SearchTextField(componentNameSearchString);
                EditorGUILayout.Space();
                for (int i = 0; i < components.Count && i < componentsProp.arraySize; i++)
                {
                    DrawComponent(indices[i], componentsProp.Get(i));
                }
            }
            EntitasEditorLayout.EndVerticalBox();
            componentsField.SetValue(creator, components.ToArray());
            EditorUtility.SetDirty(creator);
            _serializedCreator.ApplyModifiedProperties();
            if (GUI.changed)
            {
                if (Application.isPlaying)
                {
                    Debug.Log("Entity Creator is not editable in play mode.");
                }
                else
                {
                    EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
                }
            }
        }
        void DrawComponent(int index, SerializedProperty prop)
        {
            var component     = (SerializableComponent)prop.objectReferenceValue;
            var componentType = component.GetType();
            var componentName = componentType.Name.RemoveComponentSuffix();

            if (EntitasEditorLayout.MatchesSearchString(componentName.ToLower(), componentNameSearchString.ToLower()))
            {
                var boxStyle = styles[index];
                EditorGUILayout.BeginVertical(boxStyle);
                {
                    var memberInfos = componentType.GetPublicMemberInfos();
                    EditorGUILayout.BeginHorizontal();
                    {
                        if (memberInfos.Count == 0)
                        {
                            EditorGUILayout.LabelField(componentName, EditorStyles.boldLabel);
                        }
                        else
                        {
                            var foldoutStyle = new GUIStyle(EditorStyles.foldout);
                            foldoutStyle.fontStyle = FontStyle.Bold;
                            prop.isExpanded        = EditorGUILayout.Foldout(prop.isExpanded, componentName, foldoutStyle);
                        }
                        if (EntitasEditorLayout.MiniButton("-"))
                        {
                            components.Remove(component);
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                    if (prop.isExpanded)
                    {
                        foreach (var info in memberInfos)
                        {
                            var memberValue = info.GetValue(component);
                            if (memberValue != null && memberValue.Equals(null))
                            {
                                info.SetValue(component, null);
                                memberValue = null;
                            }
                            var memberType = memberValue == null ? info.type : memberValue.GetType();
                            EntityDrawer.DrawObjectMember(info, memberValue, component, info.SetValue, _transform);
                        }
                    }
                }
                EntitasEditorLayout.EndVerticalBox();
            }
        }
Example #4
0
        public static void DrawEntity(IContext context, IEntity entity)
        {
            var bgColor = GUI.backgroundColor;

            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Destroy Entity"))
            {
                entity.Destroy();
            }
            GUI.backgroundColor = bgColor;

            DrawComponents(context, entity);

            EditorGUILayout.Space();

            EditorGUILayout.IntField("CreationIndex", entity.creationIndex);
            EditorGUILayout.LabelField("Retained by (" + entity.retainCount + ")", EditorStyles.boldLabel);

            var safeAerc = entity.aerc as SafeAERC;

            if (safeAerc != null)
            {
                EntitasEditorLayout.BeginVerticalBox();
                {
                    foreach (var owner in safeAerc.owners.OrderBy(o => o.GetType().Name))
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.LabelField(owner.ToString());
                            if (EntitasEditorLayout.MiniButton("Release"))
                            {
                                entity.Release(owner);
                            }
                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }
                EntitasEditorLayout.EndVerticalBox();
            }
        }
Example #5
0
        public static void DrawEntity(IContext context, IEntity entity)
        {
            var bgColor = GUI.backgroundColor;

            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Destroy Entity"))
            {
                context.DestroyEntity(entity);
            }
            GUI.backgroundColor = bgColor;

            DrawComponents(context, entity);

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("Retained by (" + entity.retainCount + ")", EditorStyles.boldLabel);

            #if !ENTITAS_FAST_AND_UNSAFE
            EntitasEditorLayout.BeginVerticalBox();
            {
                foreach (var owner in entity.owners.OrderBy(o => o.GetType().Name))
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        EditorGUILayout.LabelField(owner.ToString());
                        if (EntitasEditorLayout.MiniButton("Release"))
                        {
                            entity.Release(owner);
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            EntitasEditorLayout.EndVerticalBox();
            #endif
        }
Example #6
0
        public static void DrawComponent(bool[] unfoldedComponents, string[] componentMemberSearch, IContext context, IEntity entity, int index, IComponent component)
        {
            var componentType = component.GetType();
            var componentName = componentType.Name.RemoveComponentSuffix();

            if (EntitasEditorLayout.MatchesSearchString(componentName.ToLower(), componentNameSearchString.ToLower()))
            {
                var boxStyle = getColoredBoxStyle(context, index);
                EditorGUILayout.BeginVertical(boxStyle);
                {
                    var memberInfos = componentType.GetPublicMemberInfos();
                    EditorGUILayout.BeginHorizontal();
                    {
                        if (memberInfos.Count == 0)
                        {
                            EditorGUILayout.LabelField(componentName, EditorStyles.boldLabel);
                        }
                        else
                        {
                            unfoldedComponents[index] = EntitasEditorLayout.Foldout(unfoldedComponents[index], componentName, foldoutStyle);
                            if (unfoldedComponents[index])
                            {
                                componentMemberSearch[index] = memberInfos.Count > 5
                                                                          ? EntitasEditorLayout.SearchTextField(componentMemberSearch[index])
                                                                          : string.Empty;
                            }
                        }
                        if (EntitasEditorLayout.MiniButton("-"))
                        {
                            entity.RemoveComponent(index);
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    if (unfoldedComponents[index])
                    {
                        var newComponent = entity.CreateComponent(index, componentType);
                        component.CopyPublicMemberValues(newComponent);

                        var changed         = false;
                        var componentDrawer = getComponentDrawer(componentType);
                        if (componentDrawer != null)
                        {
                            EditorGUI.BeginChangeCheck();
                            {
                                componentDrawer.DrawComponent(newComponent);
                            }
                            changed = EditorGUI.EndChangeCheck();
                        }
                        else
                        {
                            foreach (var info in memberInfos)
                            {
                                if (EntitasEditorLayout.MatchesSearchString(info.name.ToLower(), componentMemberSearch[index].ToLower()))
                                {
                                    var memberValue = info.GetValue(newComponent);
                                    var memberType  = memberValue == null ? info.type : memberValue.GetType();
                                    if (DrawObjectMember(memberType, info.name, memberValue, newComponent, info.SetValue))
                                    {
                                        changed = true;
                                    }
                                }
                            }
                        }

                        if (changed)
                        {
                            entity.ReplaceComponent(index, newComponent);
                        }
                        else
                        {
                            entity.GetComponentPool(index).Push(newComponent);
                        }
                    }
                }
                EntitasEditorLayout.EndVerticalBox();
            }
        }
        public override void OnInspectorGUI()
        {
            var contextObserver = ((ContextObserverBehaviour)target).contextObserver;

            EntitasEditorLayout.BeginVerticalBox();
            {
                EditorGUILayout.LabelField(contextObserver.context.contextInfo.name, EditorStyles.boldLabel);
                EditorGUILayout.LabelField("Entities", contextObserver.context.count.ToString());
                EditorGUILayout.LabelField("Reusable entities", contextObserver.context.reusableEntitiesCount.ToString());

                var retainedEntitiesCount = contextObserver.context.retainedEntitiesCount;
                if (retainedEntitiesCount != 0)
                {
                    var c = GUI.contentColor;
                    GUI.color = Color.red;
                    EditorGUILayout.LabelField("Retained entities", retainedEntitiesCount.ToString());
                    GUI.color = c;
                    EditorGUILayout.HelpBox("WARNING: There are retained entities.\nDid you call entity.Retain(owner) and forgot to call entity.Release(owner)?", MessageType.Warning);
                }

                EditorGUILayout.BeginHorizontal();
                {
                    if (GUILayout.Button("Create Entity"))
                    {
                        var entity          = contextObserver.context.CreateEntity();
                        var entityBehaviour = Object.FindObjectsOfType <EntityBehaviour>()
                                              .Single(eb => eb.entity == entity);

                        Selection.activeGameObject = entityBehaviour.gameObject;
                    }

                    var bgColor = GUI.backgroundColor;
                    GUI.backgroundColor = Color.red;
                    if (GUILayout.Button("Destroy All Entities"))
                    {
                        contextObserver.context.DestroyAllEntities();
                    }
                    GUI.backgroundColor = bgColor;
                }
                EditorGUILayout.EndHorizontal();
            }
            EntitasEditorLayout.EndVerticalBox();

            var groups = contextObserver.groups;

            if (groups.Length != 0)
            {
                EntitasEditorLayout.BeginVerticalBox();
                {
                    EditorGUILayout.LabelField("Groups (" + groups.Length + ")", EditorStyles.boldLabel);
                    foreach (var group in groups.OrderByDescending(g => g.count))
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.LabelField(group.ToString());
                            EditorGUILayout.LabelField(group.count.ToString(), GUILayout.Width(48));
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                }
                EntitasEditorLayout.EndVerticalBox();
            }

            EditorUtility.SetDirty(target);
        }