ALINE - Draw in game gizmos

Hi, I bought Aline so I can draw gizmos in-game. Currently the package does not provide any example on how to practically do this.

Can you please post an example script on how to create a static class to draw in game gizmos?

Thank you.

Hi

This documentaiton page explains how to do this: https://arongranberg.com/aline/docs/ingame.html

In short, you need to create a command builder instance and passing ‘true’ as the renderInGame parameter:

// Create a new CommandBuilder configured to draw in the game
using (var draw = DrawingManager.instance.gizmos.GetBuilder(true)) {
    // Use the exact same API as the global Draw class
    draw.WireBox(Vector3.zero, Vector3.one);
}

In a future update I will likely improve the API to make the long call to DrawingManager.instance.gizmos.GetBuilder a bit shorter: probably DrawingManager.GetBuilder.

Ok, in the docs it says that:
“It is recommended that you use the same CommandBuilder instance for most of your drawing instead of creating a new one for every little thing.”

Won’t this code create a new CommandBuilder each time it is called?

Ah, I should clarify that.
Yes, it does. However what I mean to say in the documentation is that in each frame you should not have a ton of command builders. It is ok to create a few new ones per frame.

If you want to draw from many different scripts each frame I would recommend something like this:

class Singleton : MonoBehaviour {
     public static CommandBuilder draw;
     void Awake () {
          draw = DrawingManager.instance.gizmos.GetBuilder(true);
     }
     void LateUpdate() {
          draw.Dispose();
          draw = DrawingManager.instance.gizmos.GetBuilder(true);
     }
     void OnDestroy {
          draw.Dispose();
     }
}

Then in other scripts you can do

void Update () {
     Singleton.draw.WireBox(transform.position, Vector3.one);
}

This might be a use case the package should support out of the box though?

I’m thinking a convenience api like

void Update () {
    Draw.ingame.WireBox(transform.position, Vector3.one);
}

might be useful?

I was expecting to use it as easy as with unity’s default draw gizmo functions, just call something like Debug.DrawLine() from anywhere :stuck_out_tongue:

The example you showed is exactly what I need but gives this error when I stop playmode:

Drawing data is being destroyed, but a drawing instance is still active. Are you sure you have called Dispose on all drawing instances? This will cause a memory leak!
UnityEngine.Debug:LogError(Object)
Drawing.BuilderData:Dispose() (at Assets/3rd Party/ALINE/DrawingData.cs:382)
Drawing.BuilderDataContainer:Dispose() (at Assets/3rd Party/ALINE/DrawingData.cs:449)
Drawing.DrawingData:ClearData() (at Assets/3rd Party/ALINE/DrawingData.cs:811)
Drawing.DrawingManager:OnApplicationQuit() (at Assets/3rd Party/ALINE/DrawingManager.cs:179)

Oops. I updated the code above to fix this.

Yeah. It is as easy for things that are only drawn in the editor, but currently not as easy for things drawn in the game as well.
This is definitely something I should fix. What do you think about the api suggestion in my previous post?

[EDIT] Also thank you for purchasing the package! You are one of the first users (the package was first released two days ago)! Sorry if it is a bit rough around the edges for some use cases. I will try to fix them as quickly as possible.

Something like Draw.ingame… should work well for me.

I added the OnDestroy code but I still get those errors along with:

IndexOutOfRangeException: Index was outside the bounds of the array.
Drawing.DrawingData+BuilderDataContainer.Get (System.Int32 uniqueID) (at Assets/3rd Party/ALINE/DrawingData.cs:433)
Drawing.CommandBuilder.Dispose () (at Assets/3rd Party/ALINE/CommandBuilder.cs:97)
DebugManager.OnDestroy () (at Assets/Scripts/DebugManager.cs:47)

Also is there a way to resize the arrow head when drawing an Arrow? It looks like the arrow head’s size is dependent on the arrows length.

Hi

Okay. I will try to add something like that to the next update. Or something even simpler if I can come up with another way.

Hmm… That looks like a bug. I will take a look at that.

Yes. There is an overload of the Arrow function that takes a size parameter: https://arongranberg.com/aline/docs/draw.html#Arrow2

Great, looking forward to the update!

Can you also add a way to set an absolute world size for the arrow head? So that it’s not dependent on the length of the line?

Hi

I have submitted version 1.0.2 now. It will be available in the asset store in a few days.
In it I include Draw.ingame (with updated docs). I have also changed the behaviour of Arrow to take a world space size for its head (as I think this is the more common use case) and I have added ArrowRelativeSizeHead for if you want a relative size head.

You can take a look at the full changelog here: https://arongranberg.com/aline/docs/changelog.html

Hi, thanks, works great! Sorry for the late reply, I’ve been busy with work.

I have one request: can you add a timed draw to Draw.ingame so that I can draw a gizmo in 1 call and make it remain drawn for x amount of seconds?

That would be really awesome for debugging functions that don’t get called every frame and don’t have instance variables.

I’m glad it’s working for you :slight_smile:
You can already specify a duration that things you draw should stay visible on the screen using the WithDuration scope:

using (Draw.ingame.WithDuration(1.0f)) {
    Draw.ingame.Line(a, b);
}

See https://arongranberg.com/aline/docs/scopes.html

Nice, I have just tested it but I think there is a small bug: the gizmos don’t disappear when exiting play mode.

Oh :stuck_out_tongue:
Hmm, yeah the Time.time property probably stops incrementing when exiting play mode and then the gizmos will continue to stick around.
They should at least disappear when you start the game again?

Thanks for reporting this. I’ll make sure to fix it for the next update. :slight_smile:

Yes, they disappear when the game is started again :slight_smile: