public static void SetObject(string name, Type type, object o) { var realType = type.IsEnum ? Enum.GetUnderlyingType(type) : type; if (realType == typeof(bool)) { PlayerPrefsHelper.SetBool(name, (bool)o); } else if (realType == typeof(int)) { PlayerPrefs.SetInt(name, (int)o); } else if (realType == typeof(string)) { PlayerPrefs.SetString(name, (string)o); } else if (realType == typeof(float)) { PlayerPrefs.SetFloat(name, (float)o); } else { IEDebug.Log("Going to try to serialize PlayerPref '{0}' as XML", name); SetXmlObject(name, type, o); } }
public static object GetObject(string name, Type type) { object value; var realType = type.IsEnum ? Enum.GetUnderlyingType(type) : type; if (realType == typeof(bool)) { value = PlayerPrefsHelper.GetBool(name, false); } else if (realType == typeof(int)) { value = PlayerPrefs.GetInt(name, 0); } else if (realType == typeof(string)) { value = PlayerPrefs.GetString(name, ""); } else if (realType == typeof(float)) { value = PlayerPrefs.GetFloat(name, 0.0f); } else { IEDebug.Log("Going to try to deserialize PlayerPref {0} as XML", name); return(GetXmlObject(name, type)); } var typedValue = type.IsEnum ? Enum.ToObject(type, value) : value; return(typedValue); }
/// <summary> /// Destroys components of the specified type, with the specified name. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="self"></param> /// <param name="newState"></param> public static void SetBehaviors <T>(this GameObject self, bool newState) where T : Behaviour { var components = self.Components <T>().ToList(); if (components.Count == 0) { IEDebug.Log(null, "WARNING: In GameObject {0}, told to set behaviors of type {1} to {2}, but no behaviors of this type were found.", self.name, typeof(T), newState); return; } components.ForEach(x => x.enabled = newState); }
/// <summary> /// Creates a new checkbox bound to a boolean-valued property or field. Labels are taken from attributes. /// </summary> /// <param name="memberAccessExpr">A simple member access expression for accessing the property/field you want to bind this control to. For example, <c>() => IEModOptions.YourProperty</c></param> /// <returns></returns> public GameObject Checkbox(Expression <Func <bool> > memberAccessExpr) { /* * //+ Reference * FixBackerNamesChbox.transform.parent = ModPage; * FixBackerNamesChbox.name = "FixBackerNames"; * FixBackerNamesChbox.transform.localScale = new Vector3 (1, 1, 1); * FixBackerNamesChbox.transform.localPosition = new Vector3 (-210, 150, 0); * StringId++; * FixBackerNamesChbox.GetComponent<UIOptionsTag> ().CheckboxLabel = new GUIDatabaseString (StringId); * StringId++; * FixBackerNamesChbox.GetComponent<UIOptionsTag> ().TooltipString = new GUIDatabaseString (StringId); * FixBackerNamesChbox.GetComponent<UIOptionsTag> ().Checkbox.startsChecked = PlayerPrefs.GetInt ("FixBackerNames", 0) > 0 ? true : false; * FixBackerNamesChbox.GetComponent<UIOptionsTag> ().Checkbox.onStateChange = (UICheckbox.OnStateChange) new UICheckbox.OnStateChange((test, state) => { */ if (ExampleCheckbox == null) { IEDebug.Exception(null, "You must initialize the ExampleCheckbox to create a check box.", null); } var asMemberExpr = (MemberExpression)memberAccessExpr.Body; var member = asMemberExpr.Member; IEDebug.Log("Creating Checkbox : {0}", member.Name); var setter = ReflectHelper.CreateSetter(memberAccessExpr); var chBox = (GameObject)GameObject.Instantiate(ExampleCheckbox); chBox.transform.parent = CurrentParent; var getter = ReflectHelper.CreateGetter(memberAccessExpr); chBox.name = asMemberExpr.Member.Name; var uiTag = chBox.GetComponent <UIOptionsTag>(); chBox.transform.localScale = new Vector3(1, 1, 1); chBox.transform.localPosition = new Vector3(0, 0, 0); var label = GetLabel(member); var desc = GetDesc(member); uiTag.CheckboxLabel = IEModString.Register(label); uiTag.TooltipString = IEModString.Register(desc); uiTag.Checkbox.startsChecked = getter(); uiTag.Checkbox.onStateChange += (sender, state) => setter(state); IEDebug.Log("IEMod created: " + chBox.name); return(chBox); }
private static object GetXmlObject(string name, Type type) { if (!PlayerPrefs.HasKey(name)) { return(null); } var xmlSerializer = new XmlSerializer(type); var content = PlayerPrefs.GetString(name); if (String.IsNullOrEmpty(content)) { return(Activator.CreateInstance(type)); } object obj = null; try { obj = xmlSerializer.Deserialize(new StringReader(content)); } catch (Exception) { IEDebug.Log($"Error when deserializing: {name}"); } return(obj); }
public void Print(Object o) { IEDebug.Log(PrintString(o)); }