public void AddOnOffer(OnOfferData data) { if (onOffer != null) { return; } // Safety check. onOffer = gameObject.AddComponent <PropOnOffer>(); onOffer.Initialize(this, data); }
static private void SetPropDataFieldValue(PropData propData, string fieldName, string fieldValueString) { // What extension of PropData is this? Type propDataType = propData.GetType(); // Get the FieldInfo of the requested name from this propData's class. FieldInfo fieldInfo = propDataType.GetField(fieldName); // Get the VALUE of this field from the string! if (fieldInfo == null) { Debug.LogError("We've been provided an unidentified prop field type. " + debug_roomDataLoadingRoomKey + ". PropData: " + propData + ", fieldName: " + fieldName); } else if (fieldInfo.FieldType == typeof(bool)) { fieldInfo.SetValue(propData, bool.Parse(fieldValueString)); } else if (fieldInfo.FieldType == typeof(float)) { fieldInfo.SetValue(propData, TextUtils.ParseFloat(fieldValueString)); } else if (fieldInfo.FieldType == typeof(int)) { fieldInfo.SetValue(propData, TextUtils.ParseInt(fieldValueString)); } else if (fieldInfo.FieldType == typeof(string)) { fieldInfo.SetValue(propData, fieldValueString); } else if (fieldInfo.FieldType == typeof(Rect)) { fieldInfo.SetValue(propData, TextUtils.GetRectFromString(fieldValueString)); } else if (fieldInfo.FieldType == typeof(Vector2)) { fieldInfo.SetValue(propData, TextUtils.GetVector2FromString(fieldValueString)); } else if (fieldInfo.FieldType == typeof(OnOfferData)) { fieldInfo.SetValue(propData, OnOfferData.FromString(fieldValueString)); } else if (fieldInfo.FieldType == typeof(TravelMindData)) { fieldInfo.SetValue(propData, TravelMindData.FromString(fieldValueString)); } else { Debug.LogWarning("Unrecognized field type in Room file: " + debug_roomDataLoadingRoomKey + ". PropData: " + propData + ", fieldName: " + fieldName); } }
// ---------------------------------------------------------------- // Initialize // ---------------------------------------------------------------- public void Initialize(IOnOffable myProp, OnOfferData data) { this.myProp = myProp; DurOn = data.durOn; DurOff = data.durOff; StartOffset = data.startOffset; // Make appliedOffset, which is StartOffset between 0 and total-dur. float durTotal = DurOn + DurOff; float appliedOffset = StartOffset; if (appliedOffset < 0) { appliedOffset += durTotal; } appliedOffset = appliedOffset % (durTotal); // Use StartOffset! timeUntilToggle = DurOn; timeUntilToggle -= appliedOffset; if (timeUntilToggle >= 0) // start ON with this much time left. { myProp.SetIsOn(true); } else // start OFF with this much time left. { timeUntilToggle += DurOff; myProp.SetIsOn(false); } // Move OnOffer component just under my Script, for easiness. #if UNITY_EDITOR UnityEditorInternal.ComponentUtility.MoveComponentUp(this); UnityEditorInternal.ComponentUtility.MoveComponentUp(this); UnityEditorInternal.ComponentUtility.MoveComponentUp(this); UnityEditorInternal.ComponentUtility.MoveComponentUp(this); UnityEditorInternal.ComponentUtility.MoveComponentUp(this); UnityEditorInternal.ComponentUtility.MoveComponentUp(this); UnityEditorInternal.ComponentUtility.MoveComponentDown(this); #endif }
/// str Example: (durOn:0.6, durOff:1.5, startOffset:0) static public OnOfferData FromString(string str) { OnOfferData data = new OnOfferData(); //int colon, comma; //colon = str.IndexOf (':'); //comma = str.IndexOf (','); //data.durOn = TextUtils.ParseFloat(str.Substring (colon+1, comma - (colon+1))); //colon = str.IndexOf (':', colon+1); //comma = str.IndexOf (',', comma+1); //data.durOff = TextUtils.ParseFloat(str.Substring (colon+1, comma - (colon+1))); //colon = str.IndexOf (':', colon+1); //comma = str.Length - 1; //data.startOffset = TextUtils.ParseFloat(str.Substring (colon+1, comma - (colon+1))); str = str.Substring(1, str.Length - 2); // cut the parenthesis. string[] values = str.Split(','); data.durOn = TextUtils.ParseFloat(values[0]); data.durOff = TextUtils.ParseFloat(values[1]); data.startOffset = TextUtils.ParseFloat(values[2]); return(data); }