// ------------------------------------------------------------------
/// \param _sprite the sprite
/// \param _texture the raw texture used in the sprite
/// build the sprite by texture
// ------------------------------------------------------------------
public static void Build(this exSprite _sprite, Texture2D _texture = null)
{
#if UNITY_3_4
bool isPrefab = (EditorUtility.GetPrefabType(_sprite) == PrefabType.Prefab);
#else
bool isPrefab = (PrefabUtility.GetPrefabType(_sprite) == PrefabType.Prefab);
#endif
EditorUtility.SetDirty(_sprite);
//
if (_sprite.atlas == null && _texture == null)
{
_sprite.clippingPlane = null;
GameObject.DestroyImmediate(_sprite.meshFilter.sharedMesh, true);
_sprite.meshFilter.sharedMesh = null;
_sprite.renderer.sharedMaterial = null;
return;
}
exClipping clipping = _sprite.clippingPlane;
if (clipping != null)
{
clipping.RemovePlane(_sprite);
}
// set a texture to it
if (_sprite.atlas != null)
{
_sprite.renderer.sharedMaterial = _sprite.atlas.material;
}
else if (_texture != null)
{
_sprite.renderer.sharedMaterial = exEditorHelper.GetDefaultMaterial(_texture, _texture.name);
}
EditorUtility.UnloadUnusedAssets();
//
if (_sprite.useAtlas == false && _sprite.customSize == false && _sprite.trimTexture == false)
{
_sprite.width = _texture.width;
_sprite.height = _texture.height;
}
// prefab do not need rebuild mesh
if (isPrefab == false)
{
// NOTE: it is possible user duplicate an GameObject,
// if we directly change the mesh, the original one will changed either.
Mesh newMesh = new Mesh();
newMesh.hideFlags = HideFlags.DontSave;
newMesh.Clear();
// build vertices, normals, uvs and colors.
_sprite.ForceUpdateMesh(newMesh);
// set the new mesh in MeshFilter
GameObject.DestroyImmediate(_sprite.meshFilter.sharedMesh, true); // delete old mesh (to avoid leaking)
_sprite.meshFilter.sharedMesh = newMesh;
}
// update collider
if (_sprite.collisionHelper)
{
_sprite.collisionHelper.UpdateCollider();
}
//
if (clipping != null)
{
clipping.AddPlaneInEditor(_sprite);
}
}