Editor
NGUISettings.cs
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
///
/// Unity doesn't keep the values of static variables after scripts change get recompiled. One way around this
/// is to store the references in EditorPrefs -- retrieve them at start, and save them whenever something changes.
///
public clast NGUISettings
{
static bool mLoaded = false;
static UIFont mFont;
static UIAtlas mAtlas;
static UIWidget.Pivot mPivot = UIWidget.Pivot.Center;
static Textastet mFontData;
static Texture2D mFontTexture;
static string mPartial = "";
static string mFontName = "New Font";
static string mAtlasName = "New Atlas";
static int mAtlasPadding = 1;
static public bool mAtlasTrimming = true;
static bool mUnityPacking = true;
static bool mForceSquare = true;
static bool mAllow4096 = false;
static Color mColor = Color.white;
static int mLayer = 0;
static Font mDynFont;
static int mDynFontSize = 16;
static FontStyle mDynFontStyle = FontStyle.Normal;
static Object GetObject (string name)
{
int astetID = EditorPrefs.GetInt(name, -1);
return (astetID != -1) ? EditorUtility.InstanceIDToObject(astetID) : null;
}
static void Load ()
{
int l = LayerMask.NameToLayer("UI");
if (l == -1) l = LayerMask.NameToLayer("GUI");
if (l == -1) l = 31;
mLoaded = true;
mPartial = EditorPrefs.GetString("NGUI Partial");
mFontName = EditorPrefs.GetString("NGUI Font Name");
mAtlasName = EditorPrefs.GetString("NGUI Atlas Name");
mFontData = GetObject("NGUI Font astet") as Textastet;
mFontTexture = GetObject("NGUI Font Texture") as Texture2D;
mFont = GetObject("NGUI Font") as UIFont;
mAtlas = GetObject("NGUI Atlas") as UIAtlas;
mAtlasPadding = EditorPrefs.GetInt("NGUI Atlas Padding", 1);
mAtlasTrimming = EditorPrefs.GetBool("NGUI Atlas Trimming", true);
mUnityPacking = EditorPrefs.GetBool("NGUI Unity Packing", true);
mForceSquare = EditorPrefs.GetBool("NGUI Force Square Atlas", true);
mPivot = (UIWidget.Pivot)EditorPrefs.GetInt("NGUI Pivot", (int)mPivot);
mLayer = EditorPrefs.GetInt("NGUI Layer", l);
mDynFont = GetObject("NGUI DynFont") as Font;
mDynFontSize = EditorPrefs.GetInt("NGUI DynFontSize", 16);
mDynFontStyle = (FontStyle)EditorPrefs.GetInt("NGUI DynFontStyle", (int)FontStyle.Normal);
LoadColor();
}
static void Save ()
{
EditorPrefs.SetString("NGUI Partial", mPartial);
EditorPrefs.SetString("NGUI Font Name", mFontName);
EditorPrefs.SetString("NGUI Atlas Name", mAtlasName);
EditorPrefs.SetInt("NGUI Font astet", (mFontData != null) ? mFontData.GetInstanceID() : -1);
EditorPrefs.SetInt("NGUI Font Texture", (mFontTexture != null) ? mFontTexture.GetInstanceID() : -1);
EditorPrefs.SetInt("NGUI Font", (mFont != null) ? mFont.GetInstanceID() : -1);
EditorPrefs.SetInt("NGUI Atlas", (mAtlas != null) ? mAtlas.GetInstanceID() : -1);
EditorPrefs.SetInt("NGUI Atlas Padding", mAtlasPadding);
EditorPrefs.SetBool("NGUI Atlas Trimming", mAtlasTrimming);
EditorPrefs.SetBool("NGUI Unity Packing", mUnityPacking);
EditorPrefs.SetBool("NGUI Force Square Atlas", mForceSquare);
EditorPrefs.SetInt("NGUI Pivot", (int)mPivot);
EditorPrefs.SetInt("NGUI Layer", mLayer);
EditorPrefs.SetInt("NGUI DynFont", (mDynFont != null) ? mDynFont.GetInstanceID() : -1);
EditorPrefs.SetInt("NGUI DynFontSize", mDynFontSize);
EditorPrefs.SetInt("NGUI DynFontStyle", (int)mDynFontStyle);
SaveColor();
}
static void LoadColor ()
{
string sc = EditorPrefs.GetString("NGUI Color");
if (!string.IsNullOrEmpty(sc))
{
string[] colors = sc.Split(' ');
if (colors.Length == 4)
{
float.TryParse(colors[0], out mColor.r);
float.TryParse(colors[1], out mColor.g);
float.TryParse(colors[2], out mColor.b);
float.TryParse(colors[3], out mColor.a);
}
}
}
static void SaveColor ()
{
EditorPrefs.SetString("NGUI Color", mColor.r + " " + mColor.g + " " + mColor.b + " " + mColor.a);
}
///
/// Color is used to easily copy/paste the widget's color value.
///
static public Color color
{
get
{
if (!mLoaded) Load();
return mColor;
}
set
{
if (mColor != value)
{
mColor = value;
SaveColor();
}
}
}
///
/// Default bitmap font used by NGUI.
///
static public UIFont font
{
get
{
if (!mLoaded) Load();
return mFont;
}
set
{
if (mFont != value)
{
mFont = value;
mFontName = (mFont != null) ? mFont.name : "New Font";
Save();
}
}
}
///
/// Default dynamic font used by NGUI.
///
static public Font dynamicFont
{
get
{
if (!mLoaded) Load();
return mDynFont;
}
set
{
if (mDynFont != value)
{
mDynFont = value;
mFontName = (mDynFont != null) ? mDynFont.name : "New Font";
Save();
}
}
}
///
/// Default atlas used by NGUI.
///
static public UIAtlas atlas
{
get
{
if (!mLoaded) Load();
return mAtlas;
}
set
{
if (mAtlas != value)
{
mAtlas = value;
mAtlasName = (mAtlas != null) ? mAtlas.name : "New Atlas";
Save();
}
}
}
///
/// Default pivot point used by sprites.
///
static public UIWidget.Pivot pivot
{
get
{
if (!mLoaded) Load();
return mPivot;
}
set
{
if (mPivot != value)
{
mPivot = value;
Save();
}
}
}
///
/// Default layer used by the UI.
///
static public int layer
{
get
{
if (!mLoaded) Load();
return mLayer;
}
set
{
if (mLayer != value)
{
mLayer = value;
Save();
}
}
}
///
/// Name of the font, used by the Font Maker.
///
static public string fontName { get { if (!mLoaded) Load(); return mFontName; } set { if (mFontName != value) { mFontName = value; Save(); } } }
///
/// Data used to create the font, used by the Font Maker.
///
static public Textastet fontData { get { if (!mLoaded) Load(); return mFontData; } set { if (mFontData != value) { mFontData = value; Save(); } } }
///
/// Texture used to create the font, used by the Font Maker.
///
static public Texture2D fontTexture { get { if (!mLoaded) Load(); return mFontTexture; } set { if (mFontTexture != value) { mFontTexture = value; Save(); } } }
///
/// Name of the atlas, used by the Atlas maker.
///
static public string atlasName { get { if (!mLoaded) Load(); return mAtlasName; } set { if (mAtlasName != value) { mAtlasName = value; Save(); } } }
///
/// Size of the dynamic font.
///
static public int dynamicFontSize { get { if (!mLoaded) Load(); return mDynFontSize; } set { if (mDynFontSize != value) { mDynFontSize = value; Save(); } } }
///
/// Dynamic font's style.
///
static public FontStyle dynamicFontStyle { get { if (!mLoaded) Load(); return mDynFontStyle; } set { if (mDynFontStyle != value) { mDynFontStyle = value; Save(); } } }
///
/// Name of the partial sprite name, used to filter sprites.
///
static public string partialSprite
{
get
{
if (!mLoaded) Load();
return mPartial;
}
set
{
if (mPartial != value)
{
mPartial = value;
EditorPrefs.SetString("NGUI Partial", mPartial);
}
}
}
///
/// Added padding in-between of sprites when creating an atlas.
///
static public int atlasPadding { get { if (!mLoaded) Load(); return mAtlasPadding; } set { if (mAtlasPadding != value) { mAtlasPadding = value; Save(); } } }
///
/// Whether the transparent pixels will be trimmed away when creating an atlas.
///
static public bool atlasTrimming { get { if (!mLoaded) Load(); return mAtlasTrimming; } set { if (mAtlasTrimming != value) { mAtlasTrimming = value; Save(); } } }
///
/// Whether Unity's method or MaxRectBinPack will be used when creating an atlas
///
static public bool unityPacking { get { if (!mLoaded) Load(); return mUnityPacking; } set { if (mUnityPacking != value) { mUnityPacking = value; Save(); } } }
///
/// Whether the Atlas Maker will force a square atlas texture when creating an atlas
///
static public bool forceSquareAtlas { get { if (!mLoaded) Load(); return mForceSquare; } set { if (mForceSquare != value) { mForceSquare = value; Save(); } } }
///
/// Whether the atlas maker will allow 4096 width/height textures on mobiles.
///
static public bool allow4096 { get { if (!mLoaded) Load(); return mAllow4096; } set { if (mAllow4096 != value) { mAllow4096 = value; Save(); } } }
}