public static void BroadcastProperty(this GameObject go, CustomProperty property) { object objValue = null; if (property.m_Type == "bool") { objValue = property.GetValueAsBool(); } else if (property.m_Type == "color") { objValue = property.GetValueAsColor(); } else if (property.m_Type == "float") { objValue = property.GetValueAsFloat(); } else if (property.m_Type == "int") { objValue = property.GetValueAsInt(); } else { objValue = property.GetValueAsString(); } // Use properties on all types in hierary that inherit from MonoBehaviour var components = go.GetComponentsInChildren <MonoBehaviour>(); foreach (var comp in components) { // Look for methods first var method = FindMethodBySignature(comp, property.m_Name, objValue.GetType()); if (method != null) { method.Invoke(comp, new object[1] { objValue }); continue; } // Then properties var csprop = FindPropertyBySignature(comp, property.m_Name, objValue.GetType()); if (csprop != null) { csprop.SetValue(comp, objValue, null); continue; } // Finally, look for public fields var csfield = FindFieldBySignature(comp, property.m_Name, objValue.GetType()); if (csfield != null) { csfield.SetValue(comp, objValue); continue; } } }
public static void BroadcastProperty(this GameObject go, CustomProperty property) { object objValue = null; if (property.m_Type == "bool") { objValue = property.GetValueAsBool(); } else if (property.m_Type == "color") { objValue = property.GetValueAsColor(); } else if (property.m_Type == "float") { objValue = property.GetValueAsFloat(); } else if (property.m_Type == "int") { objValue = property.GetValueAsInt(); } else { objValue = property.GetValueAsString(); } // Use properties on all types in hierary that inherit from MonoBehaviour var components = go.GetComponentsInChildren <MonoBehaviour>(); foreach (var comp in components) { // Property must be public and instanced and writable var csprop = comp.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where( info => info.CanWrite && info.Name == property.m_Name && info.PropertyType == objValue.GetType() ).FirstOrDefault(); if (csprop != null) { csprop.SetValue(comp, objValue); } } }
public static void BroadcastProperty(this GameObject go, CustomProperty property, Dictionary <int, GameObject> objectsById) { object objValue; if (property.m_Type == "bool") { objValue = property.GetValueAsBool(); } else if (property.m_Type == "color") { objValue = property.GetValueAsColor(); } else if (property.m_Type == "float") { objValue = property.GetValueAsFloat(); } else if (property.m_Type == "int") { objValue = property.GetValueAsInt(); } else if (property.m_Type == "object") { var objectId = property.GetValueAsInt(); GameObject gameObject; if (!objectsById.TryGetValue(objectId, out gameObject)) { Debug.LogErrorFormat("Object property refers to invalid ID {0}", objectId); return; } else { objValue = gameObject; } } else { objValue = property.GetValueAsString(); } // Use properties on all types in hierary that inherit from MonoBehaviour var components = go.GetComponentsInChildren <MonoBehaviour>(); foreach (var comp in components) { // Look for methods first var method = FindMethodBySignature(comp, property.m_Name, objValue.GetType()); if (method != null) { method.Invoke(comp, new object[1] { objValue }); continue; } // Then properties var csprop = FindPropertyBySignature(comp, property.m_Name, objValue.GetType()); if (csprop != null) { csprop.SetValue(comp, objValue, null); continue; } // Finally, look for public fields var csfield = FindFieldBySignature(comp, property.m_Name, objValue.GetType()); if (csfield != null) { csfield.SetValue(comp, objValue); continue; } } }