Tanoda
PhysicsCallbacks.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections;
11using System.Collections.Generic;
12using UnityEngine;
13
14namespace Leap.Unity.Interaction {
15
16 public class PhysicsCallbacks : MonoBehaviour {
17
18 public static Action OnPrePhysics {
19 get { return Provider._onPrePhysics; }
20 set { Provider._onPrePhysics = value; }
21 }
22
23 public static Action OnPostPhysics {
24 get { return Provider._onPostPhysics; }
25 set { Provider._onPostPhysics = value; }
26 }
27
29 public static PhysicsCallbacks Provider {
30 get {
31 if (_instance == null) {
32 _instance = ConstructSingleton();
33 }
34 return _instance;
35 }
36 }
37
38 private static PhysicsCallbacks ConstructSingleton() {
39 GameObject parent = new GameObject("Physics Callbacks Provider");
40 parent.transform.position = new Vector3(-10000F, -10000F, -10000F);
41
42 GameObject trigger0 = new GameObject("OnPostPhysics Trigger 0");
43 trigger0.transform.parent = parent.transform;
44 trigger0.transform.localPosition = Vector3.zero;
45 var body = trigger0.AddComponent<Rigidbody>();
46 body.isKinematic = true;
47 var box = trigger0.AddComponent<BoxCollider>();
48 box.isTrigger = true;
49
50 GameObject trigger1 = Instantiate<GameObject>(trigger0);
51 trigger1.name = "OnPostPhysics Trigger 1";
52 trigger1.transform.parent = parent.transform;
53 trigger1.transform.localPosition = Vector3.zero;
54
55 PhysicsCallbacks postPhysicsTrigger = trigger0.AddComponent<PhysicsCallbacks>();
56 return postPhysicsTrigger;
57 }
58
59 private Action _onPrePhysics = () => { };
60 private Action _onPostPhysics = () => { };
61
62 void FixedUpdate() {
63 _onPrePhysics();
64 }
65
66 void OnTriggerStay() {
67 _onPostPhysics();
68 }
69
70 }
71
72}