/// <summary> /// Get all the assets in the in-app message. /// </summary> /// <returns> /// All the assets in the in-app message. /// </returns> public List <string> ListOfAssets() { List <string> messageAssets = new List <string> (); for (int fi = 0; fi < Formats.Count; fi++) { SwrveMessageFormat format = Formats[fi]; for (int ii = 0; ii < format.Images.Count; ii++) { SwrveImage image = format.Images[ii]; if (!string.IsNullOrEmpty(image.File)) { messageAssets.Add(image.File); } } for (int bi = 0; bi < format.Buttons.Count; bi++) { SwrveButton button = format.Buttons[bi]; if (!string.IsNullOrEmpty(button.Image)) { messageAssets.Add(button.Image); } } } return(messageAssets); }
protected static SwrveImage LoadImageFromJSON(SwrveMessage message, Dictionary <string, object> imageData) { SwrveImage image = new SwrveImage(); image.Position.X = IntValueFromAttribute(imageData, "x"); image.Position.Y = IntValueFromAttribute(imageData, "y"); image.Size.X = IntValueFromAttribute(imageData, "w"); image.Size.Y = IntValueFromAttribute(imageData, "h"); image.File = StringValueFromAttribute(imageData, "image"); return(image); }
/// <summary> /// Remote the format assets from memory. /// </summary> public void UnloadAssets() { for (int ii = 0; ii < Images.Count; ii++) { SwrveImage image = Images[ii]; Texture2D.Destroy(image.Texture); image.Texture = null; } for (int bi = 0; bi < Buttons.Count; bi++) { SwrveButton button = Buttons[bi]; Texture2D.Destroy(button.Texture); button.Texture = null; } }
public static void DrawMessage(SwrveMessageFormat format, int centerx, int centery) { if (Animator != null) { AnimateMessage(format); } if (format.BackgroundColor.HasValue && GetBlankTexture() != null) { Color backgroundColor = format.BackgroundColor.Value; backgroundColor.a = backgroundColor.a * format.Message.BackgroundAlpha; GUI.color = backgroundColor; WholeScreen.width = Screen.width; WholeScreen.height = Screen.height; GUI.DrawTexture(WholeScreen, blankTexture, ScaleMode.StretchToFill, true, 10.0f); GUI.color = Color.white; } bool rotatedFormat = format.Rotate; // Rotate the inner message if necessary if (rotatedFormat) { Vector2 pivotPoint = new Vector2(centerx, centery); GUIUtility.RotateAroundPivot(90, pivotPoint); } float scale = format.Scale * format.Message.AnimationScale; GUI.color = Color.white; // Draw images for (int ii = 0; ii < format.Images.Count; ii++) { SwrveImage image = format.Images[ii]; if (image.Texture != null) { float computedSize = scale * image.AnimationScale; Point centerPoint = image.GetCenteredPosition(image.Texture.width, image.Texture.height, computedSize, scale); centerPoint.X += centerx; centerPoint.Y += centery; image.Rect.x = centerPoint.X; image.Rect.y = centerPoint.Y; image.Rect.width = image.Texture.width * computedSize; image.Rect.height = image.Texture.height * computedSize; GUI.DrawTexture(image.Rect, image.Texture, ScaleMode.StretchToFill, true, 10.0f); } else { GUI.Box(image.Rect, image.File); } } // Draw buttons for (int bi = 0; bi < format.Buttons.Count; bi++) { SwrveButton button = format.Buttons[bi]; if (button.Texture != null) { float computedSize = scale * button.AnimationScale; Point centerPoint = button.GetCenteredPosition(button.Texture.width, button.Texture.height, computedSize, scale); button.Rect.x = centerPoint.X + centerx; button.Rect.y = centerPoint.Y + centery; button.Rect.width = button.Texture.width * computedSize; button.Rect.height = button.Texture.height * computedSize; if (rotatedFormat) { // Rotate 90 degrees the hit area Point widgetCenter = button.GetCenter(button.Texture.width, button.Texture.height, computedSize); button.PointerRect.x = centerx - (button.Position.Y * scale) + widgetCenter.Y; button.PointerRect.y = centery + (button.Position.X * scale) + widgetCenter.X; button.PointerRect.width = button.Rect.height; button.PointerRect.height = button.Rect.width; } else { button.PointerRect = button.Rect; } if (Animator != null) { Animator.AnimateButtonPressed(button); } else { GUI.color = (button.Pressed) ? ButtonPressedColor : Color.white; } GUI.DrawTexture(button.Rect, button.Texture, ScaleMode.StretchToFill, true, 10.0f); } else { GUI.Box(button.Rect, button.Image); } GUI.color = Color.white; } // Do closing logic if ((Animator == null && format.Closing) || (Animator != null && Animator.IsMessageDismissed(format))) { format.Dismissed = true; format.UnloadAssets(); } }
/// <summary> /// Load an in-app message format from a JSON response. /// </summary> /// <param name="message"> /// Parent in-app message. /// </param> /// <param name="messageFormatData"> /// JSON object with the individual message format data. /// </param> /// <returns> /// Parsed in-app message format. /// </returns> public static SwrveMessageFormat LoadFromJSON(SwrveSDK sdk, SwrveMessage message, Dictionary <string, object> messageFormatData) { SwrveMessageFormat messageFormat = new SwrveMessageFormat(message); messageFormat.Name = (string)messageFormatData ["name"]; messageFormat.Language = (string)messageFormatData ["language"]; if (messageFormatData.ContainsKey("scale")) { messageFormat.Scale = MiniJsonHelper.GetFloat(messageFormatData, "scale", 1); } if (messageFormatData.ContainsKey("orientation")) { messageFormat.Orientation = SwrveOrientationHelper.Parse((string)messageFormatData ["orientation"]); } messageFormat.BackgroundColor = sdk.DefaultBackgroundColor; if (messageFormatData.ContainsKey("color")) { string strColor = (string)messageFormatData ["color"]; Color? c = messageFormat.BackgroundColor; if (strColor.Length == 8) { // RRGGBB byte a = byte.Parse(strColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); byte r = byte.Parse(strColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); byte g = byte.Parse(strColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); byte b = byte.Parse(strColor.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); c = new Color32(r, g, b, a); } else if (strColor.Length == 6) { // AARRGGBB byte r = byte.Parse(strColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); byte g = byte.Parse(strColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); byte b = byte.Parse(strColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); c = new Color32(r, g, b, 255); } messageFormat.BackgroundColor = c; } Dictionary <string, object> sizeJson = (Dictionary <string, object>)messageFormatData ["size"]; messageFormat.Size.X = MiniJsonHelper.GetInt(((Dictionary <string, object>)sizeJson ["w"]), "value"); messageFormat.Size.Y = MiniJsonHelper.GetInt(((Dictionary <string, object>)sizeJson ["h"]), "value"); IList <object> jsonButtons = (List <object>)messageFormatData ["buttons"]; for (int i = 0, j = jsonButtons.Count; i < j; i++) { SwrveButton button = LoadButtonFromJSON(message, (Dictionary <string, object>)jsonButtons [i]); messageFormat.Buttons.Add(button); } IList <object> jsonImages = (List <object>)messageFormatData ["images"]; for (int ii = 0, ji = jsonImages.Count; ii < ji; ii++) { SwrveImage image = LoadImageFromJSON(message, (Dictionary <string, object>)jsonImages [ii]); messageFormat.Images.Add(image); } return(messageFormat); }