exSprite.UpdateMesh C# (CSharp) Method

UpdateMesh() public method

public UpdateMesh ( Mesh _mesh ) : void
_mesh Mesh
return void
    public void UpdateMesh( Mesh _mesh )
    {
        exAtlas.Element el = null;
        if ( useAtlas )
            el = atlas_.elements[index_];

        // ========================================================
        // Update Vertex
        // ========================================================

        if ( (updateFlags & UpdateFlags.Vertex) != 0 ) {
            Vector2 finalScale = new Vector2 ( scale_.x * ppfScale_.x,
                                               scale_.y * ppfScale_.y );

            // init
            float halfWidthScaled = width_ * finalScale.x * 0.5f;
            float halfHeightScaled = height_ * finalScale.y * 0.5f;
            float offsetX = 0.0f;
            float offsetY = 0.0f;

            Vector3[] vertices = new Vector3[4];
            Vector3[] normals = new Vector3[4];

            // calculate anchor offset
            if ( useTextureOffset_ ) {
                // get original width and height
                float originalWidth = 0.0f;
                float originalHeight = 0.0f;
                Rect trimRect = new Rect ( 0, 0, 1, 1 );

                if ( el != null ) {
                    originalWidth   = el.originalWidth * finalScale.x;
                    originalHeight  = el.originalHeight * finalScale.y;
                    trimRect        = new Rect( el.trimRect.x * finalScale.x,
                                                el.trimRect.y * finalScale.y,
                                                el.trimRect.width * finalScale.x,
                                                el.trimRect.height * finalScale.y );
                }
                else {
                    if ( renderer.sharedMaterial != null ) {
                        Texture texture = renderer.sharedMaterial.mainTexture;
                        originalWidth   = texture.width * finalScale.x;
                        originalHeight  = texture.height * finalScale.y;
                        trimRect = new Rect ( trimUV.x * originalWidth,
                                              (1.0f - trimUV.height - trimUV.y ) * originalHeight,
                                              trimUV.width * originalWidth,
                                              trimUV.height * originalHeight );
                    }
                }

                switch ( anchor_ ) {
                    //
                case Anchor.TopLeft:
                    offsetX = -halfWidthScaled - trimRect.x;
                    offsetY = -halfHeightScaled - trimRect.y;
                    break;

                case Anchor.TopCenter:
                    offsetX = (originalWidth - trimRect.width) * 0.5f - trimRect.x;
                    offsetY = -halfHeightScaled - trimRect.y;
                    break;

                case Anchor.TopRight:
                    offsetX = halfWidthScaled + originalWidth - trimRect.xMax;
                    offsetY = -halfHeightScaled - trimRect.y;
                    break;

                    //
                case Anchor.MidLeft:
                    offsetX = -halfWidthScaled - trimRect.x;
                    offsetY = (originalHeight - trimRect.height) * 0.5f - trimRect.y;
                    break;

                case Anchor.MidCenter:
                    offsetX = (originalWidth - trimRect.width) * 0.5f - trimRect.x;
                    offsetY = (originalHeight - trimRect.height) * 0.5f - trimRect.y;
                    break;

                case Anchor.MidRight:
                    offsetX = halfWidthScaled + originalWidth - trimRect.xMax;
                    offsetY = (originalHeight - trimRect.height) * 0.5f - trimRect.y;
                    break;

                    //
                case Anchor.BotLeft:
                    offsetX = -halfWidthScaled - trimRect.x;
                    offsetY = halfHeightScaled + originalHeight - trimRect.yMax;
                    break;

                case Anchor.BotCenter:
                    offsetX = (originalWidth - trimRect.width) * 0.5f - trimRect.x;
                    offsetY = halfHeightScaled + originalHeight - trimRect.yMax;
                    break;

                case Anchor.BotRight:
                    offsetX = halfWidthScaled + originalWidth - trimRect.xMax;
                    offsetY = halfHeightScaled + originalHeight - trimRect.yMax;
                    break;

                default:
                    offsetX = (originalWidth - trimRect.width) * 0.5f - trimRect.x;
                    offsetY = (originalHeight - trimRect.height) * 0.5f - trimRect.y;
                    break;
                }
            }
            else {
                switch ( anchor_ ) {
                case Anchor.TopLeft     : offsetX = -halfWidthScaled;   offsetY = -halfHeightScaled;  break;
                case Anchor.TopCenter   : offsetX = 0.0f;               offsetY = -halfHeightScaled;  break;
                case Anchor.TopRight    : offsetX = halfWidthScaled;    offsetY = -halfHeightScaled;  break;

                case Anchor.MidLeft     : offsetX = -halfWidthScaled;   offsetY = 0.0f;               break;
                case Anchor.MidCenter   : offsetX = 0.0f;               offsetY = 0.0f;               break;
                case Anchor.MidRight    : offsetX = halfWidthScaled;    offsetY = 0.0f;               break;

                case Anchor.BotLeft     : offsetX = -halfWidthScaled;   offsetY = halfHeightScaled;   break;
                case Anchor.BotCenter   : offsetX = 0.0f;               offsetY = halfHeightScaled;   break;
                case Anchor.BotRight    : offsetX = halfWidthScaled;    offsetY = halfHeightScaled;   break;

                default                 : offsetX = 0.0f;               offsetY = 0.0f;               break;
                }
            }
            offsetX -= offset_.x;
            offsetY += offset_.y;

            float minX = 9999.0f;
            float minY = 9999.0f;
            float maxX = -9999.0f;
            float maxY = -9999.0f;

            // build vertices & normals
            for ( int r = 0; r < 2; ++r ) {
                for ( int c = 0; c < 2; ++c ) {
                    int i = r * 2 + c;
                    float x, y;
                    CalculateVertex( out x, out y,
                                     width_ * finalScale.x, height_ * finalScale.y,
                                     c, r,
                                     offsetX, offsetY );
                    vertices[i] = new Vector3( x, y, 0.0f );
                    normals[i] = new Vector3( 0.0f, 0.0f, -1.0f );

                    if ( x < minX ) minX = x;
                    else if ( x > maxX ) maxX = x;
                    if ( y < minY ) minY = y;
                    else if ( y > maxY ) maxY = y;
                }
            }

            float shearScaleWidth = maxX - minX;
            float shearScaleHeight = maxY - minY;

            _mesh.vertices = vertices;
            _mesh.normals = normals;
            _mesh.bounds = GetMeshBounds ( offsetX, offsetY, shearScaleWidth, shearScaleHeight );

            // update collider if we have
            UpdateBoundRect ( offsetX, offsetY, shearScaleWidth, shearScaleHeight );
            if ( collisionHelper )
                collisionHelper.UpdateCollider();

        // #if UNITY_EDITOR
        //             _mesh.RecalculateBounds();
        // #endif
        }

        // ========================================================
        // Update UV
        // ========================================================

        if ( (updateFlags & UpdateFlags.UV) != 0 ) {
            Vector2[] uvs = new Vector2[4];

            // if the sprite is in an atlas
            if ( el != null ) {
                float xStart  = el.coords.x;
                float yStart  = el.coords.y;
                float xEnd    = el.coords.xMax;
                float yEnd    = el.coords.yMax;

                if ( el.rotated ) {
                    uvs[0] = new Vector2 ( xEnd,    yEnd );
                    uvs[1] = new Vector2 ( xEnd,    yStart );
                    uvs[2] = new Vector2 ( xStart,  yEnd );
                    uvs[3] = new Vector2 ( xStart,  yStart );
                }
                else {
                    uvs[0] = new Vector2 ( xStart,  yEnd );
                    uvs[1] = new Vector2 ( xEnd,    yEnd );
                    uvs[2] = new Vector2 ( xStart,  yStart );
                    uvs[3] = new Vector2 ( xEnd,    yStart );
                }
            }
            else {
                float xStart  = trimUV.x;
                float yStart  = trimUV.y;
                float xEnd    = trimUV.xMax;
                float yEnd    = trimUV.yMax;

                uvs[0] = new Vector2 ( xStart,  yEnd );
                uvs[1] = new Vector2 ( xEnd,    yEnd );
                uvs[2] = new Vector2 ( xStart,  yStart );
                uvs[3] = new Vector2 ( xEnd,    yStart );
            }
            _mesh.uv = uvs;
        }

        // ========================================================
        // Update Color
        // ========================================================

        if ( (updateFlags & UpdateFlags.Color) != 0 ) {

            Color[] colors = new Color[4];
            for ( int i = 0; i < 4; ++i ) {
                colors[i] = color_;
            }
            _mesh.colors = colors;
        }

        // ========================================================
        // Update Index
        // ========================================================

        if (  (updateFlags & UpdateFlags.Index) != 0 ) {
            int[] indices = new int[6];
            indices[0] = 0;
            indices[1] = 1;
            indices[2] = 2;
            indices[3] = 2;
            indices[4] = 1;
            indices[5] = 3;
            _mesh.triangles = indices;
        }

        // NOTE: though we set updateFlags to None at exPlane::LateUpdate,
        //       the Editor still need this or it will caused editor keep dirty
        updateFlags = UpdateFlags.None;
    }

Usage Example

Example #1
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public override void OnInspectorGUI()
    {
        // ========================================================
        // Base GUI
        // ========================================================

        base.OnInspectorGUI();
        GUILayout.Space(20);

        // ========================================================
        // init values
        // ========================================================

        //
        bool needRebuild = false;

        editSprite.spanim = editSprite.GetComponent <exSpriteAnimation>();

        // get ElementInfo first
        Texture2D editTexture = exEditorHelper.LoadAssetFromGUID <Texture2D>(editSprite.textureGUID);

        // ========================================================
        // Texture preview (input)
        // ========================================================

        bool textureChanged = false;

        GUI.enabled = !inAnimMode;
        GUILayout.BeginHorizontal();
        GUILayout.Space(20);
        EditorGUIUtility.LookLikeControls();
        Texture2D newTexture = (Texture2D)EditorGUILayout.ObjectField(editTexture
                                                                      , typeof(Texture2D)
                                                                      , false
                                                                      , GUILayout.Width(100)
                                                                      , GUILayout.Height(100)
                                                                      );

        EditorGUIUtility.LookLikeInspector();
        if (newTexture != editTexture)
        {
            editTexture            = newTexture;
            editSprite.textureGUID = exEditorHelper.AssetToGUID(editTexture);
            textureChanged         = true;
            GUI.changed            = true;
        }
        GUILayout.Space(10);
        GUILayout.BeginVertical();
        GUILayout.Space(90);
        GUILayout.Label(editTexture ? editTexture.name : "None");
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUI.enabled = true;

        // ========================================================
        // get atlas element info from atlas database
        // ========================================================

        exAtlas editAtlas = null;
        int     editIndex = -1;

        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(editSprite.textureGUID);
        if (elInfo != null)
        {
            editAtlas = exEditorHelper.LoadAssetFromGUID <exAtlas>(elInfo.guidAtlas);
            editIndex = elInfo.indexInAtlas;
        }
        bool useAtlas = editAtlas != null && editIndex != -1;

        // get atlas and index from textureGUID
        if (!EditorApplication.isPlaying)
        {
            // if we don't use atlas and current edit target use atlas, clear it.
            if (editSprite.useAtlas != useAtlas)
            {
                editSprite.Clear();
            }

            // if we use atlas, check if the atlas,index changes
            if (useAtlas)
            {
                if (editAtlas != editSprite.atlas ||
                    editIndex != editSprite.index)
                {
                    editSprite.SetSprite(editAtlas, editIndex);
                    GUI.changed = true;
                }
            }

            // check if we are first time assignment
            if (useAtlas || editTexture != null)
            {
                if (isPrefab == false && editSprite.meshFilter.sharedMesh == null)
                {
                    needRebuild = true;
                }
            }
        }

        // ========================================================
        // get trimTexture
        // ========================================================

        GUI.enabled = !inAnimMode && !useAtlas;
        bool newTrimTexture = EditorGUILayout.Toggle("Trim Texture", editSprite.trimTexture);

        if (!useAtlas &&
            (textureChanged || newTrimTexture != editSprite.trimTexture))
        {
            editSprite.GetComponent <Renderer>().sharedMaterial = exEditorHelper.GetDefaultMaterial(editTexture, editTexture.name);
            editSprite.trimTexture = newTrimTexture;

            // get trimUV
            Rect trimUV = new Rect(0, 0, 1, 1);
            if (editTexture != null)
            {
                if (editSprite.trimTexture)
                {
                    if (exTextureHelper.IsValidForAtlas(editTexture) == false)
                    {
                        exTextureHelper.ImportTextureForAtlas(editTexture);
                    }
                    trimUV = exTextureHelper.GetTrimTextureRect(editTexture);
                    trimUV = new Rect(trimUV.x / editTexture.width,
                                      (editTexture.height - trimUV.height - trimUV.y) / editTexture.height,
                                      trimUV.width / editTexture.width,
                                      trimUV.height / editTexture.height);
                }

                if (editSprite.customSize == false)
                {
                    editSprite.width  = trimUV.width * editTexture.width;
                    editSprite.height = trimUV.height * editTexture.height;
                }
            }
            editSprite.trimUV       = trimUV;
            editSprite.updateFlags |= exPlane.UpdateFlags.UV;
            editSprite.updateFlags |= exPlane.UpdateFlags.Vertex;
        }
        GUI.enabled = true;

        // ========================================================
        // color
        // ========================================================

        editSprite.color = EditorGUILayout.ColorField("Color", editSprite.color);

        // ========================================================
        // atlas & index
        // ========================================================

        GUILayout.BeginHorizontal();
        GUI.enabled = false;
        EditorGUILayout.ObjectField("Atlas"
                                    , editSprite.atlas
                                    , typeof(exAtlas)
                                    , false
                                    );
        GUI.enabled = true;

        GUI.enabled = !inAnimMode;
        if (GUILayout.Button("Edit...", GUILayout.Width(40), GUILayout.Height(15)))
        {
            exAtlasEditor editor = exAtlasEditor.NewWindow();
            editor.Edit(editSprite.atlas);
        }
        GUI.enabled = true;
        GUILayout.EndHorizontal();

        GUI.enabled = false;
        EditorGUILayout.IntField("Index", editSprite.index);
        GUI.enabled = true;

        // ========================================================
        // custom size
        // ========================================================

        GUI.enabled           = !inAnimMode;
        editSprite.customSize = EditorGUILayout.Toggle("Custom Size", editSprite.customSize);
        GUI.enabled           = true;

        // ========================================================
        // width & height
        // ========================================================

        ++EditorGUI.indentLevel;
        GUI.enabled = !inAnimMode && editSprite.customSize;
        // width
        float newWidth = EditorGUILayout.FloatField("Width", editSprite.width);

        if (newWidth != editSprite.width)
        {
            if (newWidth < 1.0f)
            {
                newWidth = 1.0f;
            }
            editSprite.width = newWidth;
        }

        // height
        float newHeight = EditorGUILayout.FloatField("Height", editSprite.height);

        if (newHeight != editSprite.height)
        {
            if (newHeight < 1.0f)
            {
                newHeight = 1.0f;
            }
            editSprite.height = newHeight;
        }
        --EditorGUI.indentLevel;

        // ========================================================
        // Reset to original
        // ========================================================

        GUILayout.BeginHorizontal();
        GUILayout.Space(30);
        if (GUILayout.Button("Reset", GUILayout.Width(50)))
        {
            if (useAtlas)
            {
                exAtlas.Element el = editAtlas.elements[editIndex];
                editSprite.width  = el.trimRect.width;
                editSprite.height = el.trimRect.height;
            }
            else if (editTexture)
            {
                editSprite.width  = editSprite.trimUV.width * editTexture.width;
                editSprite.height = editSprite.trimUV.height * editTexture.height;
            }
            GUI.changed = true;
        }
        GUILayout.EndHorizontal();
        GUI.enabled = true;

        // ========================================================
        // Rebuild button
        // ========================================================

        GUI.enabled = !inAnimMode;
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Rebuild...", GUILayout.Height(20)))
        {
            needRebuild = true;
        }
        GUILayout.EndHorizontal();
        GUI.enabled = true;
        GUILayout.Space(5);

        // if dirty, build it.
        if (!EditorApplication.isPlaying && !AnimationUtility.InAnimationMode())
        {
            if (needRebuild)
            {
                EditorUtility.ClearProgressBar();
                editSprite.Build(editTexture);
            }
            else if (GUI.changed)
            {
                if (editSprite.meshFilter.sharedMesh != null)
                {
                    editSprite.UpdateMesh(editSprite.meshFilter.sharedMesh);
                }
                EditorUtility.SetDirty(editSprite);
            }
        }
    }