NudgeGUIElements

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
 
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(13 intermediate revisions by 3 users not shown)
Line 7: Line 7:
 
Author: Jonathan Czeck (aarku)
 
Author: Jonathan Czeck (aarku)
 
==Description==
 
==Description==
This script provides menu items to nudge the pixelInset of a GUITexture by 1 pixel or 10 pixels. This is useful for positioning user interfaces.  It saves time because you don't have to repeatedly perform addition and subtraction on both xMin and xMax or yMin and yMax values.
+
This script provides menu items to nudge the pixelInset of a GUITexture and the pixelOffset of a GUIText by 1 pixel or 10 pixels. This is useful for positioning user interfaces.  It saves time because you don't have to repeatedly perform addition and subtraction on the pixelInset and pixelOffset of your GUI elements.
  
 
==Usage==
 
==Usage==
Place this script in ''YourProject/Assets/Editor/NudgeGUITexture.cs'' and menu items will automatically appear in the Custom menu after it is compiled.  Then select a GUITexture using pixel positioning and select one of the Nudge menu items.
+
Place this script in ''YourProject/Assets/Editor/NudgeGUIElements.cs'' and menu items will automatically appear in the Custom menu after it is compiled.  Then select one or more GUIText and/or GUITexture and select a nudge item in the menu i.e. "Custom/Nudge/Left Fast".
  
==Problems==
+
==C# - NudgeGUIElements.cs==
Sometimes the key combinations do not work.  It is not clear why.  Also, you must coerce Unity to update the display after nudging.
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
+
==C# - NudgeGUITexture.cs==
+
<csharp>using UnityEngine;
+
 
using UnityEditor;
 
using UnityEditor;
  
public class NudgeGUITexture : ScriptableObject
+
public class NudgeGUIElements : ScriptableObject
 
{
 
{
     [MenuItem ("Custom/Nudge GUITexture/Left ^j")]
+
     [MenuItem ("Custom/Nudge GUI/Left ^j")]
 
     static void NudgeLeft()
 
     static void NudgeLeft()
 
     {
 
     {
Line 27: Line 24:
 
     }
 
     }
  
     [MenuItem ("Custom/Nudge GUITexture/Left Fast ^#j")]
+
     [MenuItem ("Custom/Nudge GUI/Left Fast ^#j")]
 
     static void NudgeLeftFast()
 
     static void NudgeLeftFast()
 
     {
 
     {
Line 33: Line 30:
 
     }
 
     }
  
     [MenuItem ("Custom/Nudge GUITexture/Right ^l")]
+
     [MenuItem ("Custom/Nudge GUI/Right ^l")]
 
     static void NudgeRight()
 
     static void NudgeRight()
 
     {
 
     {
Line 39: Line 36:
 
     }
 
     }
  
     [MenuItem ("Custom/Nudge GUITexture/Right Fast ^#l")]
+
     [MenuItem ("Custom/Nudge GUI/Right Fast ^#l")]
 
     static void NudgeRightFast()
 
     static void NudgeRightFast()
 
     {
 
     {
Line 45: Line 42:
 
     }
 
     }
  
     [MenuItem ("Custom/Nudge GUITexture/Up ^i")]
+
     [MenuItem ("Custom/Nudge GUI/Up ^o")]
 
     static void NudgeUp()
 
     static void NudgeUp()
 
     {
 
     {
Line 51: Line 48:
 
     }
 
     }
  
     [MenuItem ("Custom/Nudge GUITexture/Up Fast ^#i")]
+
     [MenuItem ("Custom/Nudge GUI/Up Fast ^#o")]
 
     static void NudgeUpFast()
 
     static void NudgeUpFast()
 
     {
 
     {
Line 57: Line 54:
 
     }
 
     }
  
     [MenuItem ("Custom/Nudge GUITexture/Down ^k")]
+
     [MenuItem ("Custom/Nudge GUI/Down ^k")]
 
     static void NudgeDown()
 
     static void NudgeDown()
 
     {
 
     {
Line 63: Line 60:
 
     }
 
     }
 
      
 
      
     [MenuItem ("Custom/Nudge GUITexture/Down Fast ^#k")]
+
     [MenuItem ("Custom/Nudge GUI/Down Fast ^#k")]
 
     static void NudgeDownFast()
 
     static void NudgeDownFast()
 
     {
 
     {
Line 85: Line 82:
 
             guiTexture.pixelInset = pixelInset;
 
             guiTexture.pixelInset = pixelInset;
 
         }
 
         }
 +
       
 +
        objects = Selection.GetFiltered(typeof(GUIText), SelectionMode.Deep | SelectionMode.Editable);
 +
       
 +
        foreach (GUIText guiText in objects)
 +
        {
 +
            Vector2 pixelOffset = guiText.pixelOffset;
 +
 +
            pixelOffset.x += x;
 +
            pixelOffset.y += y;
 +
 +
            guiText.pixelOffset = pixelOffset;
 +
        }
 +
 +
        // Trick Editor into updating
 +
        GameObject go = new GameObject();
 +
        DestroyImmediate( go );
 
     }
 
     }
 
}
 
}
</csharp>
+
</syntaxhighlight>

Latest revision as of 20:45, 10 January 2012

Author: Jonathan Czeck (aarku)

[edit] Description

This script provides menu items to nudge the pixelInset of a GUITexture and the pixelOffset of a GUIText by 1 pixel or 10 pixels. This is useful for positioning user interfaces. It saves time because you don't have to repeatedly perform addition and subtraction on the pixelInset and pixelOffset of your GUI elements.

[edit] Usage

Place this script in YourProject/Assets/Editor/NudgeGUIElements.cs and menu items will automatically appear in the Custom menu after it is compiled. Then select one or more GUIText and/or GUITexture and select a nudge item in the menu i.e. "Custom/Nudge/Left Fast".

[edit] C# - NudgeGUIElements.cs

using UnityEngine;
using UnityEditor;
 
public class NudgeGUIElements : ScriptableObject
{
    [MenuItem ("Custom/Nudge GUI/Left ^j")]
    static void NudgeLeft()
    {
        Nudge(-1f, 0f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Left Fast ^#j")]
    static void NudgeLeftFast()
    {
        Nudge(-10f, 0f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Right ^l")]
    static void NudgeRight()
    {
        Nudge(1f, 0f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Right Fast ^#l")]
    static void NudgeRightFast()
    {
        Nudge(10f, 0f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Up ^o")]
    static void NudgeUp()
    {
        Nudge(0f, 1f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Up Fast ^#o")]
    static void NudgeUpFast()
    {
        Nudge(0f, 10f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Down ^k")]
    static void NudgeDown()
    {
        Nudge(0f, -1f);
    }
 
    [MenuItem ("Custom/Nudge GUI/Down Fast ^#k")]
    static void NudgeDownFast()
    {
        Nudge(0f, -10f);
    }
 
    static void Nudge(float x, float y)
    {
        // this probably could be thought out better
        Object[] objects = Selection.GetFiltered(typeof(GUITexture), SelectionMode.Deep | SelectionMode.Editable);
 
        foreach (GUITexture guiTexture in objects)
        {
            Rect pixelInset = guiTexture.pixelInset;
 
            pixelInset.xMin += x;
            pixelInset.xMax += x;
            pixelInset.yMin += y;
            pixelInset.yMax += y;
 
            guiTexture.pixelInset = pixelInset;
        }
 
        objects = Selection.GetFiltered(typeof(GUIText), SelectionMode.Deep | SelectionMode.Editable);
 
        foreach (GUIText guiText in objects)
        {
            Vector2 pixelOffset = guiText.pixelOffset;
 
            pixelOffset.x += x;
            pixelOffset.y += y;
 
            guiText.pixelOffset = pixelOffset;
        }
 
        // Trick Editor into updating
        GameObject go = new GameObject();
        DestroyImmediate( go );
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox