public static void FlushFrameTrace(int scriptingGraphAssetID, int frameCount, Entity entity,
                                    string entityName, DotsFrameTrace frameTrace)
 {
     if (OnRecordFrameTraceDelegate != null)
     {
         OnRecordFrameTraceDelegate?.Invoke(scriptingGraphAssetID, frameCount, entity, entityName, frameTrace);
     }
     else // TODO there's a frame delay to register the delegate - the first frames are disposed right away to avoid leaks
     {
         frameTrace.Dispose();
     }
 }
        private void OnRecordFrameTraceDelegate(int scriptingGraphAssetID, int frame, Entity entity,
                                                string entityName, DotsFrameTrace frameTrace)
        {
            frameTrace.EndRecording();
            if (_data == null)
            {
                _data = new Dictionary <int, Dictionary <int, GraphTrace> >();
            }
            if (!_data.TryGetValue(scriptingGraphAssetID, out var graphData))
            {
                _data.Add(scriptingGraphAssetID, graphData = new Dictionary <int, GraphTrace>());
            }
            if (!graphData.TryGetValue(entity.Index, out var frameBuffer))
            {
                graphData.Add(entity.Index, frameBuffer = new GraphTrace(entity, entityName));
            }

            frameBuffer.Frames.PushBack(new EntityFrameData(frame, frameTrace));
        }
        protected override void OnUpdate()
        {
#if UNITY_EDITOR // Live edit
            if (m_Version != LastVersion)
            {
                m_Version = LastVersion;
                foreach (var contextsValue in m_Contexts.Values)
                {
                    contextsValue?.Dispose();
                }
                m_Contexts.Clear();
                EntityManager.RemoveComponent <ScriptingGraphInstance>(m_Query);
            }
#endif

            Entities.With(m_UninitializedQuery).ForEach((Entity e, ScriptingGraph g) =>
            {
                // Start
                var inputs = EntityManager.HasComponent <ValueInput>(e)
                    ? EntityManager.GetBuffer <ValueInput>(e)
                    : new DynamicBuffer <ValueInput>();
                GraphInstance ctx = CreateEntityContext(inputs, e, g.ScriptingGraphAsset.Definition);
#if VS_TRACING
                ctx.ScriptingGraphAssetID = g.ScriptingGraphAsset.GetInstanceID();
#endif
                EntityManager.AddComponentData(e, new ScriptingGraphInstance());
                EntityManager.AddComponentData(e, new ScriptingGraphInstanceAlive());
#if !UNITY_EDITOR // keep it for live edit
                EntityManager.RemoveComponent <ScriptingGraph>(e);
#endif
            });

#if VS_DOTS_PHYSICS_EXISTS
            VisualScriptingPhysics.SetupCollisionTriggerData(EntityManager, ref m_TriggerData, m_Query, VisualScriptingPhysics.EventType.Trigger);
            VisualScriptingPhysics.SetupCollisionTriggerData(EntityManager, ref m_CollisionData, m_Query, VisualScriptingPhysics.EventType.Collision);
#endif
            // A list: I assume the most common case is "the entity has not been destroyed"
            NativeList <Entity> destroyed = new NativeList <Entity>(Allocator.Temp);
            Entities.With(m_Query).ForEach((Entity e) =>
            {
                var beingDestroyed = m_BeingDestroyedQueryMask.Matches(e);

                GraphInstance ctx;

                //Get the context
                if (beingDestroyed)
                {
                    if (!m_Contexts.TryGetValue(e, out ctx))
                    {
                        return;
                    }
                    destroyed.Add(e);
                }
                else
                {
                    if (!m_Contexts.TryGetValue(e, out ctx))
                    {
                        // MBRIAU: Should this be an error?
                        return;
                    }
                }

                ctx.LastSystemVersion = LastSystemVersion;
                ctx.ResetFrame(); // TODO move at the end

#if VS_TRACING
                if (ctx.FrameTrace == null)
                {
                    ctx.FrameTrace = new DotsFrameTrace(Allocator.Persistent);
                }
#endif

                if (beingDestroyed)
                {
                    ctx.TriggerEntryPoints <OnDestroy>();
                    ctx.RunFrame(e, Time);

                    EntityManager.RemoveComponent <ScriptingGraphInstance>(e);
                    EntityManager.RemoveComponent <ScriptingGraph>(e);
                }
                else
                {
                    // Start
                    if (ctx.IsStarting)
                    {
                        ctx.TriggerEntryPoints <OnStart>();
                        ctx.IsStarting = false;
                    }

                    // Update
                    ctx.TriggerEntryPoints <OnUpdate>();
                    ctx.TriggerEntryPoints <OnKey>();

#if VS_DOTS_PHYSICS_EXISTS
                    TriggerPhysicsEvents(e, ref m_TriggerData, VisualScriptingPhysics.TriggerEventId);
                    TriggerPhysicsEvents(e, ref m_CollisionData, VisualScriptingPhysics.CollisionEventId);
#endif
                    // Actually execute all nodes active
                    ctx.RunFrame(e, Time);

                    m_DispatchedEvents.AddRange(ctx.DispatchedEvents);
                }
            });

            TriggerEvents(m_DispatchedEvents);

#if VS_TRACING
            foreach (var graphInstancePair in m_Contexts)
            {
                var graphInstance = graphInstancePair.Value;
                if (graphInstance?.FrameTrace != null)
                {
                    DotsFrameTrace.FlushFrameTrace(graphInstance.ScriptingGraphAssetID, UnityEngine.Time.frameCount,
                                                   graphInstance.CurrentEntity,
#if UNITY_EDITOR
                                                   EntityManager.GetName(graphInstance.CurrentEntity),
#else
                                                   e.Index.ToString(),
#endif
                                                   graphInstance.FrameTrace);
                    graphInstance.FrameTrace = null;
                }
            }
#endif
            for (var index = 0; index < destroyed.Length; index++)
            {
                var entity = destroyed[index];
                m_Contexts[entity].Dispose();
                m_Contexts.Remove(entity);
            }

            destroyed.Dispose();
        }
Exemple #4
0
 public EntityFrameData(int frame, DotsFrameTrace frameTrace)
 {
     Frame      = frame;
     FrameTrace = frameTrace;
 }