il2cpp_bridge_rs/structs/components/core/
game_object.rs1use super::component::ComponentTrait;
3use super::transform::Transform;
4use super::unity_object::UnityObject;
5use crate::api::cache;
6use crate::structs::components::scene::scene_management::Scene;
7use crate::structs::core::{Class, Il2cppObject};
8use crate::structs::Il2cppString;
9use std::ffi::c_void;
10use std::ops::Deref;
11
12#[repr(transparent)]
17#[derive(Debug, Clone)]
18pub struct GameObject {
19 pub object: UnityObject,
21}
22
23impl Deref for GameObject {
24 type Target = UnityObject;
25
26 fn deref(&self) -> &Self::Target {
27 &self.object
28 }
29}
30
31impl GameObject {
32 pub fn from_ptr(ptr: *mut c_void) -> Self {
40 Self {
41 object: UnityObject::from_ptr(ptr),
42 }
43 }
44
45 pub fn internal_create(obj: &mut GameObject, name: &str) -> Result<(), String> {
54 let core_module = cache::coremodule();
55 let game_object_class = core_module
56 .class("GameObject")
57 .ok_or_else(|| "Could not find GameObject class".to_string())?;
58
59 let create_method = game_object_class
60 .method("Internal_CreateGameObject")
61 .ok_or_else(|| {
62 "Could not find GameObject.Internal_CreateGameObject method".to_string()
63 })?;
64
65 unsafe {
66 let name_str = Il2cppString::new(name);
67 create_method.call::<()>(&[obj.as_ptr(), name_str as *mut c_void])?;
68 Ok(())
69 }
70 }
71
72 pub fn find(name: &str) -> Result<GameObject, String> {
74 let core_module = cache::coremodule();
75 let game_object_class = core_module
76 .class("GameObject")
77 .ok_or_else(|| "Could not find GameObject class".to_string())?;
78
79 let find_method = game_object_class
80 .method("Find")
81 .ok_or_else(|| "Could not find GameObject.Find method".to_string())?;
82
83 unsafe {
84 let name_str = Il2cppString::new(name);
85 let ptr = find_method.call::<*mut Il2cppObject>(&[name_str as *mut c_void])?;
86
87 if ptr.is_null() {
88 return Err("GameObject not found".to_string());
89 }
90
91 Ok(GameObject::from_ptr(ptr as *mut c_void))
92 }
93 }
94
95 pub fn get_component<T: ComponentTrait>(&self, class: &Class) -> Result<T, String> {
106 if class.object.is_null() {
107 return Err(format!(
108 "Class '{}' does not have a valid System.Type object",
109 class.name
110 ));
111 }
112
113 unsafe {
114 let ptr = self
115 .method(("GetComponent", ["System.Type"]))
116 .ok_or("Method 'GetComponent' not found")?
117 .call::<*mut c_void>(&[class.object])?;
118
119 if ptr.is_null() {
120 return Err("Component not found".to_string());
121 }
122
123 Ok(T::from_ptr(ptr))
124 }
125 }
126
127 pub fn get_transform(&self) -> Result<Transform, String> {
132 unsafe {
133 let ptr = self
134 .method("get_transform")
135 .ok_or("Method 'get_transform' not found")?
136 .call::<*mut Il2cppObject>(&[])?;
137
138 if ptr.is_null() {
139 return Err("Transform is null".to_string());
140 }
141
142 Ok(Transform::from_ptr(ptr as *mut c_void))
143 }
144 }
145
146 pub fn get_active_self(&self) -> Result<bool, String> {
151 unsafe {
152 self.method("get_activeSelf")
153 .ok_or("Method 'get_activeSelf' not found")?
154 .call::<bool>(&[])
155 }
156 }
157
158 pub fn get_active_in_hierarchy(&self) -> Result<bool, String> {
163 unsafe {
164 self.method("get_activeInHierarchy")
165 .ok_or("Method 'get_activeInHierarchy' not found")?
166 .call::<bool>(&[])
167 }
168 }
169
170 pub fn set_active(&self, active: bool) -> Result<(), String> {
178 unsafe {
179 let mut arg = active;
180 self.method("SetActive")
181 .ok_or("Method 'SetActive' not found")?
182 .call::<()>(&[&mut arg as *mut bool as *mut c_void])?;
183 Ok(())
184 }
185 }
186
187 pub fn get_layer(&self) -> Result<i32, String> {
192 unsafe {
193 self.method("get_layer")
194 .ok_or("Method 'get_layer' not found")?
195 .call::<i32>(&[])
196 }
197 }
198
199 pub fn set_layer(&self, layer: i32) -> Result<(), String> {
207 unsafe {
208 let mut arg = layer;
209 self.method("set_layer")
210 .ok_or("Method 'set_layer' not found")?
211 .call::<()>(&[&mut arg as *mut i32 as *mut c_void])?;
212 Ok(())
213 }
214 }
215
216 pub fn get_is_static(&self) -> Result<bool, String> {
221 unsafe {
222 self.method("get_isStatic")
223 .ok_or("Method 'get_isStatic' not found")?
224 .call::<bool>(&[])
225 }
226 }
227
228 pub fn get_scene(&self) -> Result<Scene, String> {
233 unsafe {
234 self.method("get_scene")
235 .ok_or("Method 'get_scene' not found")?
236 .call::<Scene>(&[])
237 }
238 }
239}