Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Calling Debug.DrawLine inside system [help needed] [api update]

Discussion in 'Entity Component System' started by Mr-Mechanical, Mar 24, 2020.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I am trying to update an ANCIENT system I coded a while back to use the newer API. It uses ComponentDataArray and OnUpdate to call Debug.DrawLine inside a system.

    Code (CSharp):
    1.  protected override void OnUpdate()
    2. {
    3.      
    4.         ComponentDataArray<PositionOfThing> positions = things.GetComponentDataArray<PositionOfThing>();
    5.         for (int i = 0; i < positions.Length; i++)
    6.         {
    7.            Vector3 pos = positions[i].Value;
    8.            Debug.DrawLine(pos, pos + Vector3.up, Color.green);
    9.         }
    10.  
    11. }
    12.  
    How would I call a method that needs to be used from the main thread with modern API?

    Thanks!
     
  2. thebanjomatic

    thebanjomatic

    Joined:
    Nov 13, 2016
    Posts:
    36
    Code (CSharp):
    1.  class MySystem: SystemBase
    2. {  
    3.     protected override void OnUpdate()
    4.     {  
    5.         Entities
    6.             .WithoutBurst()
    7.             .ForEach((in PositionOfThing positionOfThing)
    8.             {
    9.                Vector3 pos = positionOfThing.Value;
    10.                Debug.DrawLine(pos, pos + Vector3.up, Color.green);
    11.             }).Run();
    12.     }
    13. }
    14.  
    Edit:
    As @Srokaaa has pointed out in 2020.1, this can actually just be:
    Code (CSharp):
    1.  class MySystem: SystemBase
    2. {
    3.     protected override void OnUpdate()
    4.     {  
    5.         Entities
    6.             .ForEach((in PositionOfThing positionOfThing)
    7.             {
    8.                Vector3 pos = positionOfThing.Value;
    9.                Debug.DrawLine(pos, pos + Vector3.up, Color.green);
    10.             }).Schedule();
    11.     }
    12. }
    13.  
     
    Last edited: Mar 24, 2020
    Mr-Mechanical likes this.
  3. Srokaaa

    Srokaaa

    Joined:
    Sep 18, 2018
    Posts:
    169
    Btw. in Unity 2020.1 Release Notes they state that you can call Debug.DrawLine() from a job so you don't even need main thread for this.

    https://unity3d.com/unity/alpha/2020.1.0a18

    Graphics: Debug.DrawLine now callable from C# jobs
     
  4. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Ooo nice! Thank you so much for your help!
     
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    This is just what I was looking for! I'll try it out! Thank you.
     
  6. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    That is a saver of lives for me. I was currently making a hacky implementation of this to visualise debug triangulations.