Browse Source
- 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 Roommaster
6 changed files with 118 additions and 85 deletions
@ -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); |
|
||||||
} |
|
||||||
} |
|
@ -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<Room>(); |
||||||
|
foreach (Room room in allRooms) |
||||||
|
{ |
||||||
|
room.SendMessage(actionParameter, SendMessageOptions.DontRequireReceiver); |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Binary file not shown.
Loading…
Reference in new issue