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
use unity_native_plugin_sys::*;

pub trait UnityInterface {
    fn get_interface_guid() -> UnityInterfaceGUID;
    fn new(interface: *const IUnityInterface) -> Self;
}

static mut UNITY_INTERFACES: Option<UnityInterfaces> = None;

pub struct UnityInterfaces {
    interfaces: *mut unity_native_plugin_sys::IUnityInterfaces,
}

impl UnityInterfaces {
    pub fn get() -> &'static UnityInterfaces {
        unsafe { UNITY_INTERFACES.as_ref().unwrap() }
    }

    pub fn set_native_unity_interfaces(interfaces: *mut unity_native_plugin_sys::IUnityInterfaces) {
        unsafe {
            UNITY_INTERFACES = if !interfaces.is_null() {
                Some(UnityInterfaces {
                    interfaces: interfaces,
                })
            } else {
                None
            }
        }
    }

    pub fn interface<T: UnityInterface>(&self) -> Option<T> {
        unsafe {
            if let Some(intf) = (&*self.interfaces).GetInterface {
                let r = intf(T::get_interface_guid());
                if !r.is_null() {
                    return Some(T::new(r));
                }
            }
            None
        }
    }
}