public static void GetTilingCount(exISprite _sprite, out int _colCount, out int _rowCount) { exTextureInfo ti = _sprite.textureInfo; if (ti != null && ti.width + _sprite.tiledSpacing.x != 0 && ti.height + _sprite.tiledSpacing.y != 0) { _colCount = Mathf.Max(Mathf.CeilToInt(_sprite.width / (ti.width + _sprite.tiledSpacing.x)), 1); _rowCount = Mathf.Max(Mathf.CeilToInt(_sprite.height / (ti.height + _sprite.tiledSpacing.y)), 1); } else { _colCount = 1; _rowCount = 1; } }
// ------------------------------------------------------------------ // Apply exSprite or ex3DSprite change // ------------------------------------------------------------------ public static void ApplySpriteScale(exISprite _sprite, Vector3 _size, Vector3 _center) { if (_sprite.spriteType == exSpriteType.Sliced && _sprite.textureInfo != null && _sprite.textureInfo.hasBorder) { _size.x = Mathf.Max(_size.x, _sprite.leftBorderSize + _sprite.rightBorderSize); _size.y = Mathf.Max(_size.y, _sprite.bottomBorderSize + _sprite.topBorderSize); } _sprite.width = _size.x; _sprite.height = _size.y; Vector3 offset = new Vector3(-_sprite.offset.x, -_sprite.offset.y, 0.0f); Vector3 anchorOffset = Vector3.zero; switch (_sprite.anchor) { case Anchor.TopLeft: anchorOffset = new Vector3(-_size.x * 0.5f, _size.y * 0.5f, 0.0f); break; case Anchor.TopCenter: anchorOffset = new Vector3(0.0f, _size.y * 0.5f, 0.0f); break; case Anchor.TopRight: anchorOffset = new Vector3(_size.x * 0.5f, _size.y * 0.5f, 0.0f); break; case Anchor.MidLeft: anchorOffset = new Vector3(-_size.x * 0.5f, 0.0f, 0.0f); break; case Anchor.MidCenter: anchorOffset = new Vector3(0.0f, 0.0f, 0.0f); break; case Anchor.MidRight: anchorOffset = new Vector3(_size.x * 0.5f, 0.0f, 0.0f); break; case Anchor.BotLeft: anchorOffset = new Vector3(-_size.x * 0.5f, -_size.y * 0.5f, 0.0f); break; case Anchor.BotCenter: anchorOffset = new Vector3(0.0f, -_size.y * 0.5f, 0.0f); break; case Anchor.BotRight: anchorOffset = new Vector3(_size.x * 0.5f, -_size.y * 0.5f, 0.0f); break; } Vector3 scaledOffset = offset + anchorOffset - (Vector3)_sprite.GetTextureOffset(); Transform trans = _sprite.transform; Vector3 lossyScale = trans.lossyScale; scaledOffset.x *= lossyScale.x; scaledOffset.y *= lossyScale.y; Vector3 newPos = _center + trans.rotation * scaledOffset; Vector3 localPos = trans.InverseTransformPoint(newPos); localPos.z = 0; // keep z unchagned trans.position = trans.TransformPoint(localPos); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public new void OnSceneGUI() { exSpriteBase spriteBase = target as exSpriteBase; exEditorUtility.GL_DrawWireFrame(spriteBase, Color.white, false); if (spriteBase && spriteBase.customSize) { Vector3 size; Vector3 center; bool changed = ProcessSceneEditorHandles(out size, out center); if (changed) { //center.z = originalCenterZ; exISprite sprite = spriteBase as exISprite; if (sprite != null) { ApplySpriteScale(sprite, size, center); // also update all planes in the same compnent exPlane[] planes = spriteBase.GetComponents <exPlane>(); for (int i = 0; i < planes.Length; ++i) { exPlane plane = planes[i]; if (plane != this) { // plane.width = sprite.width; // plane.height = sprite.height; plane.anchor = sprite.anchor; plane.offset = sprite.offset; } } } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void GL_DrawWireFrame(exPlane _node, Color _color, bool ignoreZ = true) { exISprite sprite = _node as exISprite; if (sprite == null || sprite.vertexCount < 1000) { Vector3[] vertices = _node.GetWorldVertices(); if (vertices.Length > 0) { if (sprite != null && sprite.spriteType == exSpriteType.Sliced) { Vector3[] rectVertices = new Vector3[16]; rectVertices[0] = vertices[0]; rectVertices[1] = vertices[4]; rectVertices[2] = vertices[7]; rectVertices[3] = vertices[3]; rectVertices[4] = vertices[8]; rectVertices[5] = vertices[12]; rectVertices[6] = vertices[15]; rectVertices[7] = vertices[11]; rectVertices[8] = vertices[0]; rectVertices[9] = vertices[12]; rectVertices[10] = vertices[13]; rectVertices[11] = vertices[1]; rectVertices[12] = vertices[2]; rectVertices[13] = vertices[14]; rectVertices[14] = vertices[15]; rectVertices[15] = vertices[3]; vertices = rectVertices; } GL_DrawRectLine(vertices, _color, ignoreZ); } } else { Vector3[] vertices = _node.GetLocalVertices(); if (vertices.Length > 0) { Rect aabb = exGeometryUtility.GetAABoundingRect(vertices); Matrix4x4 l2w = _node.transform.localToWorldMatrix; vertices = new Vector3[4] { l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMin, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMin, 0)), }; GL_DrawRectLine(vertices, _color, ignoreZ); } } // if (_node is exSpriteFont) { Vector3[] vertices = _node.GetRectVertices(Space.Self); Rect aabb = exGeometryUtility.GetAABoundingRect(vertices); Matrix4x4 l2w = _node.transform.localToWorldMatrix; vertices = new Vector3[4] { l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMin, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMin, 0)), }; GL_DrawRectLine(vertices, _color, ignoreZ); } }
public static void GetTilingCount(exISprite _sprite, out int _colCount, out int _rowCount) { exTextureInfo ti = _sprite.textureInfo; if (ti != null && ti.width + _sprite.tiledSpacing.x != 0 && ti.height + _sprite.tiledSpacing.y != 0) { _colCount = Mathf.Max(Mathf.CeilToInt(_sprite.width / (ti.width + _sprite.tiledSpacing.x)), 1); _rowCount = Mathf.Max(Mathf.CeilToInt(_sprite.height / (ti.height + _sprite.tiledSpacing.y)), 1); } else { _colCount = 1; _rowCount = 1; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void GetVertexAndIndexCount(this exISprite _sprite, out int _vertexCount, out int _indexCount) { switch (_sprite.spriteType) { case exSpriteType.Simple: _vertexCount = exMesh.QUAD_VERTEX_COUNT; _indexCount = exMesh.QUAD_INDEX_COUNT; break; case exSpriteType.Sliced: _vertexCount = 4 * 4; _indexCount = exMesh.QUAD_INDEX_COUNT * 9; if (_sprite.borderOnly) { _indexCount -= exMesh.QUAD_INDEX_COUNT; } break; case exSpriteType.Tiled: { int colCount, rowCount; exSpriteUtility.GetTilingCount(_sprite, out colCount, out rowCount); int quadCount = colCount * rowCount; _vertexCount = quadCount * exMesh.QUAD_VERTEX_COUNT; const int maxVertex = exMesh.MAX_VERTEX_COUNT; //const int maxVertex = 40000; if (_vertexCount > maxVertex) { Debug.LogWarning(_sprite.gameObject.name + " is too big. Consider using a bigger texture.", _sprite.gameObject); int sqrCount = (int)Mathf.Sqrt(maxVertex / exMesh.QUAD_VERTEX_COUNT); if (colCount > sqrCount) { _sprite.width = (_sprite.textureInfo.width + _sprite.tiledSpacing.x) * sqrCount; } if (rowCount > sqrCount) { _sprite.height = (_sprite.textureInfo.height + _sprite.tiledSpacing.y) * sqrCount; } exSpriteUtility.GetTilingCount(_sprite, out colCount, out rowCount); quadCount = colCount * rowCount; _vertexCount = quadCount * exMesh.QUAD_VERTEX_COUNT; exDebug.Assert(_vertexCount <= maxVertex); } _indexCount = quadCount * exMesh.QUAD_INDEX_COUNT; break; } case exSpriteType.Diced: { exTextureInfo ti = _sprite.textureInfo; if (ti == null || ti.isDiced == false) { _vertexCount = exMesh.QUAD_VERTEX_COUNT; _indexCount = exMesh.QUAD_INDEX_COUNT; return; } int quadCount = 0; DiceEnumerator dice = _sprite.textureInfo.dices; while (dice.MoveNext()) { if (dice.Current.sizeType != exTextureInfo.DiceType.Empty) { ++quadCount; } } if (quadCount == 0) { quadCount = 1; } //int colCount, rowCount; //exSpriteUtility.GetDicingCount (_sprite, out colCount, out rowCount); //exDebug.Assert (quadCount <= colCount * rowCount); _vertexCount = quadCount * exMesh.QUAD_VERTEX_COUNT; _indexCount = quadCount * exMesh.QUAD_INDEX_COUNT; if (_vertexCount > exMesh.MAX_VERTEX_COUNT) { Debug.LogError("The texture info [" + _sprite.textureInfo.name + "] has too many dices! Please using a bigger dice value.", _sprite.textureInfo); _vertexCount = exMesh.QUAD_VERTEX_COUNT; _indexCount = exMesh.QUAD_INDEX_COUNT; } break; } default: _vertexCount = exMesh.QUAD_VERTEX_COUNT; _indexCount = exMesh.QUAD_INDEX_COUNT; break; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static Vector2 GetTextureOffset(this exISprite _sprite) { Vector2 anchorOffset = Vector2.zero; if (_sprite.useTextureOffset) { exTextureInfo textureInfo = _sprite.textureInfo; switch (_sprite.anchor) { case Anchor.TopLeft: anchorOffset.x = textureInfo.trim_x; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height); break; case Anchor.TopCenter: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height); break; case Anchor.TopRight: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width); anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height); break; // case Anchor.MidLeft: anchorOffset.x = textureInfo.trim_x; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; case Anchor.MidCenter: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; case Anchor.MidRight: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width); anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; // case Anchor.BotLeft: anchorOffset.x = textureInfo.trim_x; anchorOffset.y = textureInfo.trim_y; break; case Anchor.BotCenter: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = textureInfo.trim_y; break; case Anchor.BotRight: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width); anchorOffset.y = textureInfo.trim_y; break; // default: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; } Vector2 customSizeScale = new Vector2(_sprite.width / _sprite.textureInfo.width, _sprite.height / _sprite.textureInfo.height); anchorOffset.x *= customSizeScale.x; anchorOffset.y *= customSizeScale.y; } return(anchorOffset); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static Vector3 CalcAttachPointLocalPosition(this exISprite _sprite, Vector2 _attachPos) { exTextureInfo textureInfo = _sprite.textureInfo; Vector2 anchorOffset; float halfHeight = textureInfo.height * 0.5f; float halfWidth = textureInfo.width * 0.5f; if (_sprite.useTextureOffset) { switch (_sprite.anchor) { case Anchor.TopLeft: anchorOffset.x = halfWidth + textureInfo.trim_x; anchorOffset.y = -halfHeight + textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height); break; case Anchor.TopCenter: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = -halfHeight + textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height); break; case Anchor.TopRight: anchorOffset.x = -halfWidth + textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width); anchorOffset.y = -halfHeight + textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height); break; // case Anchor.MidLeft: anchorOffset.x = halfWidth + textureInfo.trim_x; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; case Anchor.MidCenter: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; case Anchor.MidRight: anchorOffset.x = -halfWidth + textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width); anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; // case Anchor.BotLeft: anchorOffset.x = halfWidth + textureInfo.trim_x; anchorOffset.y = halfHeight + textureInfo.trim_y; break; case Anchor.BotCenter: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = halfHeight + textureInfo.trim_y; break; case Anchor.BotRight: anchorOffset.x = -halfWidth + textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width); anchorOffset.y = halfHeight + textureInfo.trim_y; break; // default: anchorOffset.x = textureInfo.trim_x - (textureInfo.rawWidth - textureInfo.width) * 0.5f; anchorOffset.y = textureInfo.trim_y - (textureInfo.rawHeight - textureInfo.height) * 0.5f; break; } } else { switch (_sprite.anchor) { case Anchor.TopLeft: anchorOffset.x = halfWidth; anchorOffset.y = -halfHeight; break; case Anchor.TopCenter: anchorOffset.x = 0.0f; anchorOffset.y = -halfHeight; break; case Anchor.TopRight: anchorOffset.x = -halfWidth; anchorOffset.y = -halfHeight; break; case Anchor.MidLeft: anchorOffset.x = halfWidth; anchorOffset.y = 0.0f; break; case Anchor.MidCenter: anchorOffset.x = 0.0f; anchorOffset.y = 0.0f; break; case Anchor.MidRight: anchorOffset.x = -halfWidth; anchorOffset.y = 0.0f; break; case Anchor.BotLeft: anchorOffset.x = halfWidth; anchorOffset.y = halfHeight; break; case Anchor.BotCenter: anchorOffset.x = 0.0f; anchorOffset.y = halfHeight; break; case Anchor.BotRight: anchorOffset.x = -halfWidth; anchorOffset.y = halfHeight; break; default: anchorOffset.x = 0.0f; anchorOffset.y = 0.0f; break; } } return(new Vector3(_attachPos.x + anchorOffset.x, _attachPos.y + anchorOffset.y, 0.0f)); }