遞迴搜尋
public static GameObject FindInChildren(GameObject gameObject, string name)
{
foreach(Transform t in gameObject.GetComponentsInChildren<Transform>())
{
if(t.name == name)
return t.gameObject;
}
return null;
}
圖片集合
public class SpriteCollection
{
var sprites : Sprite[];
var names : string[];
public function SpriteCollection(spritesheet : string)
{
sprites = resources.loadall.<sprite>(spritesheet);
names = new string[sprites.length];
for(var i = 0; i < names.length; i++)
{
names[i] = sprites[i].name;
}
}
public function GetSprite(name : string) : Sprite
{
return sprites[system.array.indexof(names, name)];
}
}
漸層文字
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu("UI/Effects/Gradient")]
public class Gradient : BaseVertexEffect
{
public enum Type
{
Vertical,
Horizontal
}
[SerializeField]
public Type GradientType = Type.Vertical;
[SerializeField]
[Range(-1.5f, 1.5f)]
public float Offset = 0f;
[SerializeField]
private Color32 StartColor = Color.white;
[SerializeField]
private Color32 EndColor = Color.black;
public override void ModifyVertices(List<UIVertex> _vertexList)
{
if (!IsActive())
return;
int nCount = _vertexList.Count;
switch (GradientType)
{
case Type.Vertical:
{
float fBottomY = _vertexList[0].position.y;
float fTopY = _vertexList[0].position.y;
float fYPos = 0f;
for (int i = nCount - 1; i >= 1; --i)
{
fYPos = _vertexList[i].position.y;
if (fYPos > fTopY)
fTopY = fYPos;
else if (fYPos < fBottomY)
fBottomY = fYPos;
}
float fUIElementHeight = 1f / (fTopY - fBottomY);
for (int i = nCount - 1; i >= 0; --i)
{
UIVertex uiVertex = _vertexList[i];
uiVertex.color = Color32.Lerp(
EndColor,
StartColor,
(uiVertex.position.y - fBottomY) * fUIElementHeight - Offset
);
_vertexList[i] = uiVertex;
}
}
break;
case Type.Horizontal:
{
float fLeftX = _vertexList[0].position.x;
float fRightX = _vertexList[0].position.x;
float fXPos = 0f;
for (int i = nCount - 1; i >= 1; --i)
{
fXPos = _vertexList[i].position.x;
if (fXPos > fRightX)
fRightX = fXPos;
else if (fXPos < fLeftX)
fLeftX = fXPos;
}
float fUIElementWidth = 1f / (fRightX - fLeftX);
for (int i = nCount - 1; i >= 0; --i)
{
UIVertex uiVertex = _vertexList[i];
uiVertex.color = Color32.Lerp(
StartColor,
EndColor,
(uiVertex.position.x - fLeftX) * fUIElementWidth - Offset
);
_vertexList[i] = uiVertex;
}
}
break;
default: break;
}
}
}
Ref: Reddit、Unity3D Gradient Effect