playMaker

Author Topic: A Little extension on GUIs  (Read 2785 times)

FXCarl

  • Playmaker Newbie
  • *
  • Posts: 4
A Little extension on GUIs
« on: June 22, 2013, 09:44:24 AM »
Add two bool setting:

1. you can centered display gui element, not only front top left
2. set rect center as gui pivot. For us, its useful ...

just using this GUIActions.cs , no drawbacks

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
// base type for GUI actions that need a Rect
[Tooltip("GUI base action - don't use!")]
public abstract class GUIAction : FsmStateAction
{
[UIHint(UIHint.Variable)]
public FsmRect screenRect;

public FsmFloat left;
public FsmFloat top;
public FsmFloat width;
public FsmFloat height;

[RequiredField]
public FsmBool normalized;
public FsmBool centered;
public FsmBool centeredPivot;

internal Rect rect;

public override void Reset()
{
screenRect = null;
left = 0;
top = 0;
width = 1;
height = 1;
normalized = true;
centered = false;
centeredPivot = false;
}

public override void OnGUI()
{
rect = !screenRect.IsNone ? screenRect.Value : new Rect();

if (!left.IsNone) rect.x = left.Value;
if (!top.IsNone) rect.y = top.Value;
if (!width.IsNone) rect.width = width.Value;
if (!height.IsNone) rect.height = height.Value;

if (normalized.Value)
{
rect.x *= Screen.width;
rect.width *= Screen.width;
rect.y *= Screen.height;
rect.height *= Screen.height;
}

if (centered.Value)
{
rect.x += Screen.width * 0.5f;
rect.y += Screen.height * 0.5f;
}

if (centeredPivot.Value)
{
rect.x -= rect.width * 0.5f;
rect.y -= rect.height * 0.5f;
}
}
}
}