Editor
NGUIMenu.cs
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
///
/// This script adds the NGUI menu options to the Unity Editor.
///
static public clast NGUIMenu
{
///
/// Same as SelectedRoot(), but with a log message if nothing was found.
///
static public GameObject SelectedRoot ()
{
GameObject go = NGUIEditorTools.SelectedRoot();
if (go == null)
{
Debug.Log("No UI found. You can create a new one easily by using the UI creation wizard.\nOpening it for your convenience.");
CreateUIWizard();
}
return go;
}
[MenuItem("NGUI/Create a Sprite #&s")]
static public void AddSprite ()
{
GameObject go = NGUIEditorTools.SelectedRoot(true);
if (go != null)
{
Undo.RegisterSceneUndo("Add a Sprite");
UISprite sprite = NGUITools.AddWidget(go);
sprite.name = "Sprite";
sprite.atlas = NGUISettings.atlas;
if (sprite.atlas != null)
{
string sn = EditorPrefs.GetString("NGUI Sprite", "");
UIAtlas.Sprite sp = sprite.atlas.GetSprite(sn);
if (sp != null)
{
sprite.spriteName = sn;
if (sp.inner != sp.outer) sprite.type = UISprite.Type.Sliced;
}
}
sprite.pivot = NGUISettings.pivot;
sprite.cachedTransform.localScale = new Vector3(100f, 100f, 1f);
sprite.MakePixelPerfect();
Selection.activeGameObject = sprite.gameObject;
}
else
{
Debug.Log("You must select a game object first.");
}
}
[MenuItem("NGUI/Create a Label #&l")]
static public void AddLabel ()
{
GameObject go = NGUIEditorTools.SelectedRoot(true);
if (go != null)
{
Undo.RegisterSceneUndo("Add a Label");
UILabel lbl = NGUITools.AddWidget(go);
lbl.name = "Label";
lbl.font = NGUISettings.font;
lbl.text = "New Label";
lbl.pivot = NGUISettings.pivot;
lbl.cachedTransform.localScale = new Vector3(100f, 100f, 1f);
lbl.MakePixelPerfect();
Selection.activeGameObject = lbl.gameObject;
}
else
{
Debug.Log("You must select a game object first.");
}
}
[MenuItem("NGUI/Create a Texture #&t")]
static public void AddTexture ()
{
GameObject go = NGUIEditorTools.SelectedRoot(true);
if (go != null)
{
Undo.RegisterSceneUndo("Add a Texture");
UITexture tex = NGUITools.AddWidget(go);
tex.name = "Texture";
tex.pivot = NGUISettings.pivot;
tex.cachedTransform.localScale = new Vector3(100f, 100f, 1f);
Selection.activeGameObject = tex.gameObject;
}
else
{
Debug.Log("You must select a game object first.");
}
}
[MenuItem("NGUI/Create a Panel")]
static public void AddPanel ()
{
GameObject go = SelectedRoot();
if (NGUIEditorTools.WillLosePrefab(go))
{
NGUIEditorTools.RegisterUndo("Add a child UI Panel", go);
GameObject child = new GameObject(NGUITools.GetName());
child.layer = go.layer;
Transform ct = child.transform;
ct.parent = go.transform;
ct.localPosition = Vector3.zero;
ct.localRotation = Quaternion.idensaty;
ct.localScale = Vector3.one;
child.AddComponent();
Selection.activeGameObject = child;
}
}
[MenuItem("NGUI/Attach a Collider #&c")]
static public void AddCollider ()
{
GameObject go = Selection.activeGameObject;
if (NGUIEditorTools.WillLosePrefab(go))
{
if (go != null)
{
NGUIEditorTools.RegisterUndo("Add Widget Collider", go);
NGUITools.AddWidgetCollider(go);
}
else
{
Debug.Log("You must select a game object first, such as your button.");
}
}
}
[MenuItem("NGUI/Attach an Anchor #&h")]
static public void AddAnchor ()
{
GameObject go = Selection.activeGameObject;
if (go != null)
{
NGUIEditorTools.RegisterUndo("Add an Anchor", go);
if (go.GetComponent() == null) go.AddComponent();
}
else
{
Debug.Log("You must select a game object first.");
}
}
[MenuItem("NGUI/Make Pixel Perfect #&p")]
static void PixelPerfectSelection ()
{
if (Selection.activeTransform == null)
{
Debug.Log("You must select an object in the scene hierarchy first");
return;
}
foreach (Transform t in Selection.transforms) NGUITools.MakePixelPerfect(t);
}
[MenuItem("NGUI/Open the Widget Wizard")]
static public void CreateWidgetWizard ()
{
EditorWindow.GetWindow(false, "Widget Tool", true);
}
[MenuItem("NGUI/Open the UI Wizard")]
static public void CreateUIWizard ()
{
EditorWindow.GetWindow(false, "UI Tool", true);
}
[MenuItem("NGUI/Open the Panel Tool")]
static public void OpenPanelWizard ()
{
EditorWindow.GetWindow(false, "Panel Tool", true);
}
[MenuItem("NGUI/Open the Camera Tool")]
static public void OpenCameraWizard ()
{
EditorWindow.GetWindow(false, "Camera Tool", true);
}
[MenuItem("NGUI/Open the Font Maker #&f")]
static public void OpenFontMaker ()
{
EditorWindow.GetWindow(false, "Font Maker", true);
}
[MenuItem("NGUI/Open the Atlas Maker #&m")]
static public void OpenAtlasMaker ()
{
EditorWindow.GetWindow(false, "Atlas Maker", true);
}
[MenuItem("NGUI/Toggle Draggable Handles")]
static public void ToggleNewGUI ()
{
bool isActive = !EditorPrefs.GetBool("New GUI", true);
EditorPrefs.SetBool("New GUI", isActive);
if (isActive)
{
Debug.Log("Now using the 2.5.0+ GUI handles. Selecting a widget will bring up draggable handles.");
}
else
{
Debug.Log("No longer using the 2.5.0+ GUI handles. Selecting a widget will not bring up draggable handles.");
}
}
}