static EntityVisual CreateVisualFromPrefab(MARSEntity entity, GameObject prefab)
        {
            if (prefab == null)
            {
                return(null);
            }

            // Set the prefab root to not active so that OnEnable will not be called yet when instantiated.
            // This allows scripts on the prefab to reference the entity on the visual script which is assigned after instantiate
            prefab.SetActive(false);
            var go = Instantiate(prefab);

            go.name = entity.gameObject.name;

            var visual = go.GetComponent <EntityVisual>();

            if (visual == null)
            {
                visual = go.AddComponent <EntityVisual>();
            }

            visual.entity = entity;

            var interactionTargets = visual.GetComponentsInChildren <InteractionTarget>();

            foreach (var target in interactionTargets)
            {
                target.AttachTarget = entity.transform;
            }

            // Now set it active will enable all its components
            go.SetActive(true);

            return(visual);
        }
        /// <summary>
        /// Update the visual color for an entity
        /// </summary>
        /// <param name="entity">The entity that needs its visual to be updated</param>
        public void UpdateColor(MARSEntity entity)
        {
            EntityVisual visual;

            if (m_EntityVisuals.TryGetValue(entity, out visual))
            {
                visual.ApplyColors();
            }
        }
        internal static void DoPickProxyPresetButton(MARSEntity childEntity, Rect rect)
        {
            PopupWindow.Show(rect, new ChangeProxyWindow(childEntity));

            EditorEvents.RulesUiUsed.Send(new RuleUiArgs
            {
                label = k_PickProxyPresetButtonAnalyticsLabel
            });
        }
Exemple #4
0
        public override void OnEnable()
        {
            conditionBase         = (ConditionBase)target;
            m_AdjustableComponent = target as IAdjustableComponent;
            targetTransform       = conditionBase.transform;
            entity = targetTransform.GetComponent <MARSEntity>();

            base.OnEnable();
        }
        /// <summary>
        /// Removes the current visual for an entity so that it will be recreated.
        /// </summary>
        /// <param name="entity"> The entity for which the visual is invalid</param>
        public void InvalidateVisual(MARSEntity entity)
        {
            EntityVisual visual;

            if (m_EntityVisuals.TryGetValue(entity, out visual))
            {
                m_EntityVisuals.Remove(entity);
                if (visual != null)
                {
                    UnityObjectUtils.Destroy(visual.gameObject);
                }
            }
        }
Exemple #6
0
        bool IsEntityTracking(MARSEntity entity)
        {
            if ((!entity) || (!entity.isActiveAndEnabled)) // destroyed or deactivated
            {
                return(false);
            }

            if (m_IsGroupReplicator)
            {
                return(((ProxyGroup)entity).queryState == QueryState.Tracking);
            }
            else
            {
                return(((Proxy)entity).queryState == QueryState.Tracking);
            }
        }
Exemple #7
0
        public void OnEnable()
        {
            m_SelectedEntity = (MARSEntity)target;

            m_ColorProperty = serializedObject.FindProperty("m_Color");
            if (m_ColorProperty != null)
            {
                SetupColorOptionMenu();
            }

            // HACK: This should not be null but it is when entering play mode with a simulation object selected
            if (!m_SelectedEntity)
            {
                return;
            }

            m_SelectedTransform = m_SelectedEntity.transform;
            RefreshComponentList();

            Undo.undoRedoPerformed += OnUndoRedoPerformed;
        }
        protected void SetupProxyPresetUI(MARSEntity entity)
        {
            m_ProxyField            = this.Q <ObjectField>(k_ProxyPresetName);
            m_ProxyField.objectType = typeof(Proxy);
            m_ProxyField.RegisterValueChangedCallback(OnChangeProxy);

            m_ProxyField.RegisterCallback <ExecuteCommandEvent>(RulesModule.PickObject);

            var selector = m_ProxyField.Q <VisualElement>(className: ObjectField.selectorUssClassName);

            selector.style.display = DisplayStyle.None;

            m_PickProxyPresetButton          = this.Q <Button>(k_PickProxyPresetName);
            m_PickProxyPresetButton.clicked += () =>
            {
                var mouse = Event.current.mousePosition;
                RulesModule.DoPickProxyPresetButton(entity, new Rect(mouse.x, mouse.y, 0, 0));
            };

            m_PickProxyPresetButton.RegisterCallback <ExecuteCommandEvent>(RulesModule.PickObject);
        }
        /// <summary>
        /// Disable the collider for an entity's visual. Used for preventing an entity from attaching to its own visual.
        /// </summary>
        /// <param name="entity"> The entity for which the visual should be disabled.</param>
        public void DisableVisualCollision(MARSEntity entity)
        {
            if (entity == null)
            {
                return;
            }

            EntityVisual visual;

            if (m_EntityVisuals.TryGetValue(entity, out visual))
            {
                if (visual == null || m_DisabledCollisionVisuals.Contains(visual))
                {
                    return;
                }

                foreach (var collider in visual.GetComponentsInChildren <Collider>())
                {
                    collider.enabled = false;
                }

                m_DisabledCollisionVisuals.Add(visual);
            }
        }
        EntityVisual CreateVisual(MARSEntity entity, SceneType sceneType)
        {
            EntityVisual visual = null;

            // Collect all traits from conditions
            var traits     = new HashSet <string>();
            var conditions = entity.GetComponents <ICondition>();

            foreach (var condition in conditions)
            {
                if (!condition.enabled)
                {
                    continue;
                }

                var tagCondition = condition as ISemanticTagCondition; // Skip traits from exclude tag conditions
                if (tagCondition != null && tagCondition.matchRule == SemanticTagMatchRule.Exclude)
                {
                    continue;
                }

                traits.Add(condition.traitName);
            }

            // Also collect traits from multi-conditions
            var multiconditions = entity.GetComponents <MultiConditionBase>();

            foreach (var multicondition in multiconditions)
            {
                if (multicondition.HostedComponents != null)
                {
                    foreach (var subcondition in multicondition.HostedComponents)
                    {
                        var tagCondition = subcondition as ISemanticTagCondition; // Skip traits from exclude tag conditions
                        if (tagCondition != null && tagCondition.matchRule == SemanticTagMatchRule.Exclude)
                        {
                            continue;
                        }

                        traits.Add(subcondition.traitName);
                    }
                }
            }

            // First check mappings, the first matching trait in the mappings list will determine the visual
            foreach (var visualMapping in m_VisualMappings)
            {
                var prefab    = visualMapping.visualPrefab;
                var traitName = visualMapping.trait;
                if (prefab != null && traits.Contains(traitName))
                {
                    visual = CreateVisualFromPrefab(entity, prefab);
                    break;
                }
            }

            // If there is no prefab for the entity's traits, create the default
            if (visual == null)
            {
                visual = CreateVisualFromPrefab(entity, m_DefaultVisual);
            }

            if (visual == null)
            {
                return(null);
            }

#if UNITY_EDITOR
            var selected = Selection.gameObjects.Contains(entity.gameObject);
            visual.InteractionTarget.SetSelected(selected);
#endif

            // Check that the scene type we are using has a visuals root and set the visuals to that root
            switch (sceneType)
            {
            case SceneType.Simulation:
                if (m_SimulationSceneVisualsRoot == null)
                {
                    m_SimulationSceneVisualsRoot = new GameObject(k_VisualsRootName);
                    m_SimulationSceneVisualsRoot.transform.SetParent(simEntitiesRoot);
                    m_SimulationSceneVisualsRoot.hideFlags = k_VisualsRootHideFlags;
                }

                visual.transform.SetParent(m_SimulationSceneVisualsRoot.transform, true);
                visual.gameObject.SetLayerAndHideFlagsRecursively(entity.gameObject.layer, k_VisualHideFlags);
                break;

            case SceneType.Scene:
                if (m_MainSceneVisualsRoot == null)
                {
                    m_MainSceneVisualsRoot           = new GameObject(k_VisualsRootName);
                    m_MainSceneVisualsRoot.hideFlags = k_VisualsRootHideFlags;
                }

                visual.transform.SetParent(m_MainSceneVisualsRoot.transform);
                visual.gameObject.SetLayerAndHideFlagsRecursively(entity.gameObject.layer, k_VisualHideFlags);
                break;

            case SceneType.Prefab:
#if UNITY_EDITOR
                if (m_PrefabStageScene.IsValid())
                {
                    // Prefab stage root needs to be recreated for new prefab stages
                    if (m_PrefabStageVisualsRoot == null)
                    {
                        // Needs different hide flags for proper selection, hiding and not modifying
                        // the prefab scene in prefab mode.
                        m_PrefabStageVisualsRoot = new GameObject(k_VisualsRootName)
                        {
                            hideFlags = k_PrefabStageHideFlags
                        };
                        SceneManager.MoveGameObjectToScene(m_PrefabStageVisualsRoot, m_PrefabStageScene);
                    }

                    visual.transform.SetParent(m_PrefabStageVisualsRoot.transform);
                    visual.gameObject.SetLayerAndHideFlagsRecursively(entity.gameObject.layer, k_PrefabStageHideFlags);
                }
#endif
                break;
            }

            var sizeCondition = entity.GetComponent <PlaneSizeCondition>(); // PlaneSizeConditions will control scale
            if (sizeCondition != null)
            {
                m_SizeConditions.Add(visual, sizeCondition);
            }

            var markerCondition = entity.GetComponent <MarkerCondition>();
            if (markerCondition != null)
            {
                m_ImageMarkerConditions.Add(visual, markerCondition);
            }

            if (traits.Contains(TraitNames.Face))
            {
                m_FaceVisuals.Add(visual.transform);
            }

            return(visual);
        }
 public EntityVisual GetVisual(MARSEntity entity)
 {
     return(m_EntityVisuals.TryGetValue(entity, out var visual) ? visual : null);
 }
 public ChangeProxyWindow(MARSEntity entity)
 {
     m_Entity = entity;
 }