/// <summary>
/// Determines whether the 'parent' contains a 'child' in its hierarchy.
/// </summary>
static public bool IsChild (Transform parent, Transform child)
{
if (parent == null || child == null) return false;
while (child != null)
{
if (child == parent) return true;
child = child.parent;
}
return false;
}
/// <summary>
/// Recursively set the game object's layer.
/// </summary>
static public void SetLayer (GameObject go, int layer)
{
go.layer = layer;
Transform t = go.transform;
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
Transform child = t.GetChild(i);
SetLayer(child.gameObject, layer);
}
}
/// <summary>
/// Make the specified selection pixel-perfect.
/// </summary>
static public void MakePixelPerfect (Transform t)
{
UIWidget w = t.GetComponent<UIWidget>();
if (w != null)
{
w.MakePixelPerfect();
}
else
{
t.localPosition = Round(t.localPosition);
t.localScale = Round(t.localScale);
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
MakePixelPerfect(t.GetChild(i));
}
}
}
文件
/// <summary>
/// Save the specified binary data into the specified file.
/// </summary>
static public bool Save (string fileName, byte[] bytes)
{
#if UNITY_WEBPLAYER || UNITY_FLASH || UNITY_METRO
return false;
#else
if (!NGUITools.fileAccess) return false;
string path = Application.persistentDataPath + "/" + fileName;
if (bytes == null)
{
if (File.Exists(path)) File.Delete(path);
return true;
}
FileStream file = null;
try
{
file = File.Create(path);
}
catch (System.Exception ex)
{
NGUIDebug.Log(ex.Message);
return false;
}
file.Write(bytes, 0, bytes.Length);
file.Close();
return true;
#endif
}
/// <summary>
/// Load all binary data from the specified file.
/// </summary>
static public byte[] Load (string fileName)
{
#if UNITY_WEBPLAYER || UNITY_FLASH || UNITY_METRO
return null;
#else
if (!NGUITools.fileAccess) return null;
string path = Application.persistentDataPath + "/" + fileName;
if (File.Exists(path))
{
return File.ReadAllBytes(path);
}
return null;
#endif
}
/// <summary>
/// Pre-multiply shaders result in a black outline if this operation is done in the shader. It's better to do it outside.
/// </summary>
static public Color ApplyPMA (Color c)
{
if (c.a != 1f)
{
c.r *= c.a;
c.g *= c.a;
c.b *= c.a;
}
return c;
}
剪切板
/// <summary>
/// Access to the clipboard via a hacky method of accessing Unity's internals. Won't work in the web player.
/// </summary>
public static string clipboard
{
get
{
PropertyInfo copyBuffer = GetSystemCopyBufferProperty();
return (copyBuffer != null) ? (string)copyBuffer.GetValue(null, null) : null;
}
set
{
PropertyInfo copyBuffer = GetSystemCopyBufferProperty();
if (copyBuffer != null) copyBuffer.SetValue(null, value, null);
}
}
#endif
}
Sprite
/// <summary>
/// Add a sprite appropriate for the specified atlas sprite.
/// It will be sliced if the sprite has an inner rect, and a regular sprite otherwise.
/// </summary>
static public UISprite AddSprite (GameObject go, UIAtlas atlas, string spriteName)
{
UIAtlas.Sprite sp = (atlas != null) ? atlas.GetSprite(spriteName) : null;
UISprite sprite = AddWidget<UISprite>(go);
sprite.type = (sp == null || sp.inner == sp.outer) ? UISprite.Type.Simple : UISprite.Type.Sliced;
sprite.atlas = atlas;
sprite.spriteName = spriteName;
return sprite;
}
/// <summary>
/// Add a new widget of specified type.
/// </summary>
static public T AddWidget<T> (GameObject go) where T : UIWidget
{
int depth = CalculateNextDepth(go);
// Create the widget and place it above other widgets
T widget = AddChild<T>(go);
widget.depth = depth;
// Clear the local transform
Transform t = widget.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = new Vector3(100f, 100f, 1f);
widget.gameObject.layer = go.layer;
return widget;
}