Skip to main content

il2cpp_bridge_rs/api/wrappers/
time.rs

1//! Thin wrapper around `UnityEngine.Time`.
2//!
3//! These helpers are intentionally small and are mainly useful as examples of
4//! cache-backed static property access.
5
6use crate::api::cache;
7use crate::structs::core::Class;
8use std::ffi::c_void;
9
10/// Accessors for selected `UnityEngine.Time` properties.
11pub struct Time;
12
13impl Time {
14    /// Gets the `UnityEngine.Time` class
15    pub fn get_class() -> Option<Class> {
16        cache::coremodule().class("UnityEngine.Time")
17    }
18
19    /// The time at the beginning of this frame (Read Only)
20    pub fn get_time() -> f32 {
21        unsafe {
22            Self::get_class()
23                .and_then(|cls| cls.method(("get_time", 0)))
24                .map(|method| method.call::<f32>(&[] as &[*mut c_void]).unwrap_or(0.0))
25                .unwrap_or(0.0)
26        }
27    }
28
29    /// The time in seconds it took to complete the last frame (Read Only)
30    pub fn get_delta_time() -> f32 {
31        unsafe {
32            Self::get_class()
33                .and_then(|cls| cls.method(("get_deltaTime", 0)))
34                .map(|method| method.call::<f32>(&[] as &[*mut c_void]).unwrap_or(0.0))
35                .unwrap_or(0.0)
36        }
37    }
38
39    /// The interval in seconds at which physics and other fixed frame rate updates (like MonoBehaviour's FixedUpdate) are performed.
40    pub fn get_fixed_delta_time() -> f32 {
41        unsafe {
42            Self::get_class()
43                .and_then(|cls| cls.method(("get_fixedDeltaTime", 0)))
44                .map(|method| method.call::<f32>(&[] as &[*mut c_void]).unwrap_or(0.0))
45                .unwrap_or(0.0)
46        }
47    }
48
49    /// The scale at which time passes. This can be used for slow motion effects.
50    pub fn get_time_scale() -> f32 {
51        unsafe {
52            Self::get_class()
53                .and_then(|cls| cls.method(("get_timeScale", 0)))
54                .map(|method| method.call::<f32>(&[] as &[*mut c_void]).unwrap_or(1.0))
55                .unwrap_or(1.0)
56        }
57    }
58
59    /// Sets the scale at which time passes.
60    pub fn set_time_scale(value: f32) {
61        unsafe {
62            if let Some(method) = Self::get_class().and_then(|cls| cls.method(("set_timeScale", 1)))
63            {
64                let _ = method.call::<()>(&[&mut { value } as *mut f32 as *mut c_void]);
65            }
66        }
67    }
68}