From 23af2fc44346dab5048aa647829893d8ede164e6 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 29 May 2014 13:07:02 +0100 Subject: [PATCH] Renamed AnchorGUITexture to GUIButton - Retained the existing screen positioning logic - Add support for button actions 1. Open a URL on click (e.g. for a watermark sprite) 2. Call a named method on all Room objects - Moved example implementation to Button Room --- Assets/Fungus/Scripts/AnchorGUITexture.cs | 71 ------------ Assets/Fungus/Scripts/GUIButton.cs | 108 ++++++++++++++++++ ...orGUITexture.cs.meta => GUIButton.cs.meta} | 0 Assets/FungusExample/Scenes/Example.unity | Bin 65348 -> 65736 bytes Assets/FungusExample/Scripts/ButtonRoom.cs | 11 +- Assets/FungusExample/Scripts/SpriteRoom.cs | 13 --- 6 files changed, 118 insertions(+), 85 deletions(-) delete mode 100644 Assets/Fungus/Scripts/AnchorGUITexture.cs create mode 100644 Assets/Fungus/Scripts/GUIButton.cs rename Assets/Fungus/Scripts/{AnchorGUITexture.cs.meta => GUIButton.cs.meta} (100%) diff --git a/Assets/Fungus/Scripts/AnchorGUITexture.cs b/Assets/Fungus/Scripts/AnchorGUITexture.cs deleted file mode 100644 index 5d99922b..00000000 --- a/Assets/Fungus/Scripts/AnchorGUITexture.cs +++ /dev/null @@ -1,71 +0,0 @@ -using UnityEngine; -using System.Collections; - -/** - * Draws a GUI texture at a consistent size regardless of screen resolution. - * The texture can be positioned anywhere on the screen using normalized screen coords. - * Vertical and horizontal padding can be applied to offset the texture away from the screen edge. - */ -public class AnchorGUITexture : MonoBehaviour -{ - /// Texture to draw on the screen. - public Texture2D texture; - - /// Fraction of screen height (for resolution independent sizing). - public float verticalScale = 0.2f; - - /// Texture position on screen in localized screen coords ([0..1], [0..1]) - public Vector2 screenPosition; - - /// Vertical and horizontal space between edge of screen and texture (in pixels). - public Vector2 padding; - - void OnGUI() - { - if (texture == null) - { - return; - } - - // Calc initial center point - float x = screenPosition.x * Screen.width; - float y = screenPosition.y * Screen.height; - - // Height is calculated as a fraction of screen height for resolution independent sizing. - // Width is then calculated so as to maintain the original aspect ratio of the texture. - float height = Screen.height * verticalScale; - float width = texture.width * (height / texture.height); - - // Calc initial rect for rendering texture - float x1 = x - width / 2f; - float y1 = y - height / 2f; - float x2 = x + width / 2f; - float y2 = y + height / 2f; - - // Adjust rect to fit on screen, and apply vertical & horizontal padding - if (x1 < padding.x) - { - x1 = padding.x; - x2 = x1 + width; - } - if (x2 > Screen.width - padding.x) - { - x2 = Screen.width - padding.x; - x1 = x2 - width; - } - if (y1 < padding.y) - { - y1 = padding.y; - y2 = y1 + height; - } - if (y2 > Screen.height - padding.y) - { - y2 = Screen.height - padding.y; - y1 = y2 - height; - } - - // Draw the texture - Rect textureRect = new Rect(x1, y1, x2 - x1, y2 - y1); - GUI.DrawTexture(textureRect, texture); - } -} diff --git a/Assets/Fungus/Scripts/GUIButton.cs b/Assets/Fungus/Scripts/GUIButton.cs new file mode 100644 index 00000000..06555baa --- /dev/null +++ b/Assets/Fungus/Scripts/GUIButton.cs @@ -0,0 +1,108 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /** + * Draws a GUI button at a consistent size regardless of screen resolution. + * The button can be positioned anywhere on the screen using normalized screen coords. + * Vertical and horizontal padding can be applied to offset the button away from the screen edge. + * Several options are available for handling the player click action. + */ + public class GUIButton : MonoBehaviour + { + /// Texture to draw on the screen. + public Texture2D texture; + + /// Fraction of screen height (for resolution independent sizing). + public float verticalScale = 0.2f; + + /// Texture position on screen in localized screen coords ([0..1], [0..1]) + public Vector2 screenPosition; + + /// Vertical and horizontal space between edge of screen and texture (in pixels). + public Vector2 padding; + + /// Supported actions to perform when player clicks the button. + public enum ClickAction + { + /// Perform no action, useful when you just want to display a sprite in screen space. + None, + /// Open the URL specified in actionParameter in the browser + OpenURL, + /// Call a method specified by actionParameter on all Room objects in the scene. + SendMessage + }; + + /// Action to perform when player clicks the button. + public ClickAction clickAction; + + /// Parameter associated with the click action. + public string actionParameter; + + void OnGUI() + { + if (texture == null) + { + return; + } + + // Calc initial center point + float x = screenPosition.x * Screen.width; + float y = screenPosition.y * Screen.height; + + // Height is calculated as a fraction of screen height for resolution independent sizing. + // Width is then calculated so as to maintain the original aspect ratio of the texture. + float height = Screen.height * verticalScale; + float width = texture.width * (height / texture.height); + + // Calc initial rect for rendering texture + float x1 = x - width / 2f; + float y1 = y - height / 2f; + float x2 = x + width / 2f; + float y2 = y + height / 2f; + + // Adjust rect to fit on screen, and apply vertical & horizontal padding + if (x1 < padding.x) + { + x1 = padding.x; + x2 = x1 + width; + } + if (x2 > Screen.width - padding.x) + { + x2 = Screen.width - padding.x; + x1 = x2 - width; + } + if (y1 < padding.y) + { + y1 = padding.y; + y2 = y1 + height; + } + if (y2 > Screen.height - padding.y) + { + y2 = Screen.height - padding.y; + y1 = y2 - height; + } + + // Draw the texture + Rect textureRect = new Rect(x1, y1, x2 - x1, y2 - y1); + if (GUI.Button(textureRect, texture)) + { + switch (clickAction) + { + case ClickAction.OpenURL: + Application.OpenURL(actionParameter); + break; + case ClickAction.SendMessage: + // Send the message to all Room objects in the scene + Room[] allRooms = GameObject.FindObjectsOfType(); + foreach (Room room in allRooms) + { + room.SendMessage(actionParameter, SendMessageOptions.DontRequireReceiver); + } + break; + } + } + } + } +} diff --git a/Assets/Fungus/Scripts/AnchorGUITexture.cs.meta b/Assets/Fungus/Scripts/GUIButton.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/AnchorGUITexture.cs.meta rename to Assets/Fungus/Scripts/GUIButton.cs.meta diff --git a/Assets/FungusExample/Scenes/Example.unity b/Assets/FungusExample/Scenes/Example.unity index d4ad08750709dd9d9d6e47b72ed2411f00910813..bdbfc327a2b6cad0b1832a3a272ba8c30bb3ea54 100644 GIT binary patch delta 1386 zcmaKse@q)?7{{Np$1j^e;fkTDi*4yLl4&Wh!gUI(GdeJ`mEn+?AU`UMX3~!K8X0a` zmS`k~D9dA5BY%J{#>H&OmNRbtW58^ti4M_?gcxH;)NT5Q>zMQB==|Qe&Z~dyO`hC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0 zLbo@ZcI+dh4mq3@Dlp@ILJWBR<#t-N zGCVk4W&>+Lo_Eu4!PKyz8qC{o1gsT|?e}hQdQ+JlkhT9k5_RdAPPlfvld~2xeLs(W zuXDg|*6|rIZKj9)kjzN$j;g;7#+tG5R6)HVn~+@tMgb_ zhT;KjfUK}Tm=kO-HdrJ@0GKAvcb*`z7Azh)^U?;_p5&V*z@WWqni4EF-*c*7)8?}` ze`#t39AS1CRi(IS#5dCACnVpo%LJ9}K>{z@K>Np26#i%_UZPizKIEtrVlVNEFzt>K zDje~g&^4=Eqf`K8uz!Tsdm8%FscGw!)5JkFO_@ugenTpH$tI`0GrVA*8wm~+e-I%k zjG-UzN7BVUCDpmY=v diff --git a/Assets/FungusExample/Scripts/ButtonRoom.cs b/Assets/FungusExample/Scripts/ButtonRoom.cs index ae69a2b8..1666881c 100644 --- a/Assets/FungusExample/Scripts/ButtonRoom.cs +++ b/Assets/FungusExample/Scripts/ButtonRoom.cs @@ -5,7 +5,7 @@ namespace Fungus.Example { public class ButtonRoom : Room { - public Fungus.Room menuRoom; + public Room menuRoom; public AudioClip effectClip; @@ -13,8 +13,15 @@ namespace Fungus.Example public Button soundButton; public Button questionButton; + // GUIButton displays a button texture at the same size & position regardless of screen resolution. + // This is handy for displaying simple buttons in a consistent manner across devices. + public GUIButton mushroomButton; + void OnEnter() { + // Show the mushroom logo immediately + mushroomButton.enabled = true; + // Show button, always visible (because autoHide is set to false) ShowButton(homeButton, OnHomeClicked); @@ -34,6 +41,8 @@ namespace Fungus.Example void OnHomeClicked() { + mushroomButton.enabled = false; + MoveToRoom(menuRoom); } diff --git a/Assets/FungusExample/Scripts/SpriteRoom.cs b/Assets/FungusExample/Scripts/SpriteRoom.cs index 446ad938..2b5a9e06 100644 --- a/Assets/FungusExample/Scripts/SpriteRoom.cs +++ b/Assets/FungusExample/Scripts/SpriteRoom.cs @@ -11,15 +11,8 @@ namespace Fungus.Example public SpriteRenderer blueAlienSprite; public SpriteRenderer redMushroomSprite; - // AnchorGUITexture displays a texture at the same size & position regardless of screen resolution. - // This is handy for displaying simple GUI textures in a consistent manner across devices. - public AnchorGUITexture mushroomLogo; - void OnEnter() { - // Show the mushroom logo immediately - mushroomLogo.enabled = true; - HideSprite(redMushroomSprite); ShowSprite(blueAlienSprite); @@ -64,12 +57,6 @@ namespace Fungus.Example Say("Heh. That Blue Alien - what a guy!"); - // We want the mushroom logo to hide just before we move to the menuRoom, - // so we pass a delegate method to the Call command which will execute as part of the command sequence. - Call(delegate { - mushroomLogo.enabled = false; - }); - MoveToRoom(menuRoom); } }