summaryrefslogtreecommitdiff
path: root/ActiveRagdoll/Assets/TABG/Scripts/Debug/GizmosHandle.cs
blob: 21229120ea8fac00ad4965f1079af49c317a9562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace Rigging.Debugging
{
    [DefaultExecutionOrder(-100)]
    public class GizmosHandle : MonoBehaviour
    {
        private static GizmosHandle instance;
        private static event System.Action onDrawGizmos;
        private static event System.Action onDrawGizmosFixedUpdate;

        private void OnDrawGizmos()
        {
            onDrawGizmos?.Invoke();
            onDrawGizmos = null;

            onDrawGizmosFixedUpdate?.Invoke();
        }

        private void FixedUpdate()
        {
            onDrawGizmosFixedUpdate = null;
        }

        public static void DrawCube(Vector3 position, Vector3 size)
        {
            Check();

            if (Time.inFixedTimeStep)
            {
                onDrawGizmosFixedUpdate += () =>
                {
                    Gizmos.DrawCube(position, size);
                };
            }
            else
            {
                onDrawGizmos += () =>
                {
                    Gizmos.DrawCube(position, size);
                };
            }
        }

        static void Check()
        {
            if(instance == null) {
                GameObject handle = new GameObject();
                handle.name = "GizmosHandle";
                instance = handle.AddComponent<GizmosHandle>();    
            }
        }

    }

}