"Learn Unity3d with Me" cut the picture in the code and load the frame sequence animation

Source: Internet
Author: User

In COCOS2DX, the processing of the large map has encapsulated a set of its own API, but in Unity3d there seems to be no similar API, or I did not find. But this is also reasonable, after all, Unity3d is to do 3D, to cut the picture of the place is still very few.

Because I use Unity3d is mainly used to do 2D games (PS: Very egg hurts it?) I also feel that), so you have to consider the cut diagram and play sequence frame two common features on the 2D, the following nonsense not much to say. My task is to cut this graph into 16 pieces and play them in the sequence of the animation.

In the process of checking Unity3d's manual, I found a class: Texture2d, which is inherited texture, is mainly used to create 2D textures, and is very much in line with the needs of transduction.

First of all, we need to load a large map, loading a large map has a very simple way, is to create a public texture2d class member variable, and then in the editor to drag directly up to give him a value.

Of course, you can also use the dynamic loading of image resources, this method is more troublesome, you need to convert the picture into a binary stream, and then assign to Texture2d

Load picture resource void Loadtexture () {using (FileStream file = File.Open (Application.datapath + "/textures/player.png", FileMode.Open) {using (BinaryReader reader = new BinaryReader (file)) {M_texplayer = new texture2d (192, TextureFormat . ARGB4444, false); texture. LoadImage (reader. Readbytes ((int) file. Length));}}}

After loading will be cut, the main idea is that, two for loop, one for the line, one for the column, and then cycle through each pixel, the color of each pixel point inside the copy to the cut texture2d, and finally the texture2d into a 4x4 matrix array.

Here's the first step:

        for (int i = 0, i < m_iminpiccolumncount; ++i)        {for            (int j = 0; j < M_iminpicrowcount; ++j)                depacktextu Re (I, j);        }

The final processing above invokes a depacktexture, which is used for the actual cut.

    transduction    void depacktexture (int i, int j)    {        int cur_x = i * m_iminpicwidth;        int cur_y = J * M_iminpicheight;        Texture2d newtexture = new Texture2d (m_iminpicwidth, m_iminpicheight);        for (int m = cur_y, M < cur_y + m_iminpicheight; ++m)        {for            (int n = cur_x; n < cur_x + m_iminpicwidth; ++n)            {                newtexture.setpixel (n-cur_x, m-cur_y, M_texplayer.getpixel (n, m));            }        }        Newtexture.apply ();        M_texplayers[i, j] = Newtexture;    }
Transduction noteworthy is two points, a point is to find a good position, the other is to execute after the setpixel operation must be executed, otherwise there is no effect.

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------

Here is the frame sequence animation, the frame sequence animation is actually the picture in a certain order loaded up, it is worth noting that all the GUI operation must be put into the ongui inside.

    void Drawanimation (texture[,] Tex, rect rect)    {        //draws the current frame        GUI. Drawtexture (Rect, Tex[m_icurfram, M_icuranimation], Scalemode.stretchtofill, true, 0.0f);        Calculate the time limit frame        m_ftime + = Time.deltatime;        Over limit frame Toggle Map        if (m_ftime >= 1.0/m_ffps && m_bstop = False)        {            //frame sequence Toggle            M_icurfram = ++m_icurf Ram% M_iminpicrowcount;            Limit frame emptying            m_ftime = 0;            The total number of frame animations starts from frame No. 0            if (M_icurfram >= Tex. Length)            {                M_icurfram = 0;            }        }    }

Then there is nothing, the code is still very simple. The following is the full code, this I made a small demo, containing the animation of the start and pause function, but also the animation frame-rate adjustment function. (The address of the demo will be attached at the end)

Using unityengine;using system.collections;using System;public class ctexture:monobehaviour{//Big picture of people public Texture    M_texplayer;    Small figure of the person private texture2d[,] m_texplayers;    The current frame private int m_icurfram;    Current animation private int m_icuranimation;    Limit the time of the frame private float m_ftime = 0;    The width and height of the small graph public int m_iminpicwidth = 48;    public int m_iminpicheight = 64;    How many small graphs public int m_iminpicrowcount = 4 in a row;    A column of how many small figure public int m_iminpiccolumncount = 4;    Animation control//Pause private bool M_bstop = false;    How many frames a second private float m_ffps = 4;    private string m_sfps = "";        void Start () {m_texplayers = new texture2d[4, 4];m_icuranimation = 0;        M_sfps = M_ffps.tostring ();//Load Picture resource loadtexture ();                for (int i = 0, i < M_iminpiccolumncount; ++i) {for (int j = 0; j < M_iminpicrowcount; ++j)        Depacktexture (i, j);        }} void Update () {if (Input.getkeydown (Keycode.a)) {    M_icuranimation = 2;        } if (Input.getkeydown (KEYCODE.S)) {m_icuranimation = 3;        } if (Input.getkeydown (KEYCODE.W)) {m_icuranimation = 0;        } if (Input.getkeydown (KEYCODE.D)) {m_icuranimation = 1;                }} void Ongui () {drawanimation (m_texplayers, new Rect (+, M_iminpicwidth, m_iminpicheight)); if (GUI.        button (new Rect (200,20,80,50), "Start/Pause")) {m_bstop = M_bstop = = False? True:false; } M_sfps = GUI.        TextField (new Rect), m_sfps); if (GUI. button (new Rect (200, 150, 50, 40), "apply")) {M_ffps = float.        Parse (M_sfps); }}//Load Picture resource void Loadtexture () {using (FileStream file = File.Open (Application.datapath + "/textures/player.png", Filemo De. Open) {using (BinaryReader reader = new BinaryReader (file)) {M_texplayer = new texture2d (192, Up, textureformat.argb4444 , false); texture. LoaDIMAGE (reader. Readbytes ((int) file.    Length));}}}        transduction void depacktexture (int i, int j) {int cur_x = i * m_iminpicwidth;        int cur_y = J * M_iminpicheight;        Texture2d newtexture = new Texture2d (m_iminpicwidth, m_iminpicheight); for (int m = cur_y, M < cur_y + m_iminpicheight; ++m) {for (int n = cur_x; n < cur_x + M_IMINPICW Idth;            ++n) {newtexture.setpixel (n-cur_x, m-cur_y, M_texplayer.getpixel (n, m));        }} newtexture.apply ();    M_texplayers[i, j] = Newtexture; } void Drawanimation (texture[,] Tex, rect rect) {//draws the current frame GUI.        Drawtexture (Rect, Tex[m_icurfram, M_icuranimation], Scalemode.stretchtofill, true, 0.0f);        Calculate the time limit frame m_ftime + = Time.deltatime; Over limit frame Toggle Map if (m_ftime >= 1.0/m_ffps && m_bstop = = False) {//Frame sequence Toggle M_ic            Urfram = ++m_icurfram% 4; Limit frame emptying M_ftime = 0; The total number of frame animations starts from frame No. 0 if (M_icurfram >= Tex.            Length) {m_icurfram = 0; }        }    }}

Demo Address: Http://pan.baidu.com/s/1qWpyf5E


"Learn Unity3d with Me" cut the picture in the code and load the frame sequence animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.