根据技能冷却的时间 求出每个三角形所需要的时间

来源:互联网 发布:mac卸载office2016 编辑:程序博客网 时间:2024/05/29 13:08
转自:
基本思路 根据技能冷却的时间 求出每个三角形所需要的时间

根据流逝时间 获取当前操作的三角形索引
最终根据三角形索引进行 三角形顶点位移

代码如下,请根据注释来进行每一步的理解

[code]

001using UnityEngine;
002using System.Collections;
003 
004 
005[ExecuteInEditMode]
006[AddComponentMenu("Ace/Skill/Mask")]
007public classMyMask : MonoBehaviour {
008 
009    privateMeshFilter meshFilter;
010 
011    privateVector3[] verts = new Vector3[10];
012    privatefloat halfWidth = 0.5f;
013    privatefloat halfHeight = 0.5f;
014    privatefloat start;//开始时间
015    privatefloat total;//技能冷却时间
016    privatefloat once;
017    publicColor color;
018 
019    privatebool play = false;
020 
021    void Start () {
022        meshFilter=GetComponent<MeshFilter>();
023        meshFilter.sharedMesh =new Mesh();
024        ResetVertices();
025        SetMeshVertices(meshFilter.sharedMesh);
026         
027        setTriangles(meshFilter.sharedMesh);
028        setColor(meshFilter.sharedMesh);
029        startMovice(30);
030    }
031 
032    void OnMouseUp() {
033        startMovice(60);
034    }
035 
036    publicvoid startMovice(float time) {
037        if(play) return;
038        total = time;
039        once = total / 8;//这里用8个三角形 总时间算出每个三角形需要耗时
040        renderer.enabled = true;//每次开始让描绘对象可见
041        ResetVertices();//每次开始重置顶点数据
042        play = true;
043    }
044 
045    privatevoid ResetVertices()
046    {
047        verts[0] =new Vector3(0.0f, 0.0f, 0.0f);
048        verts[1] =new Vector3(0.0f, halfHeight, 0.0f);
049        verts[2] =new Vector3(halfWidth, halfHeight, 0.0f);
050        verts[3] =new Vector3(halfWidth, 0.0f, 0.0f);
051        verts[4] =new Vector3(halfWidth, -halfHeight, 0.0f);
052        verts[5] =new Vector3(0.0f, -halfHeight, 0.0f);
053        verts[6] =new Vector3(-halfWidth, -halfHeight, 0.0f);
054        verts[7] =new Vector3(-halfWidth, 0.0f, 0.0f);
055        verts[8] =new Vector3(-halfWidth, halfHeight, 0.0f);
056        verts[9] =new Vector3(0.0f, halfHeight, 0.0f);
057    }
058 
059    // 设置Mesh顶点数据
060    privatevoid SetMeshVertices(Mesh mesh)
061    {
062        Vector3[] vertices =new Vector3[verts.Length];
063        for(int i = 0; i < verts.Length; ++i)
064        {
065            vertices[i] = verts[i];
066        }
067        mesh.vertices = vertices;
068    }
069 
070    privatevoid setColor(Mesh mesh)
071    {
072        Color[] colors =new Color[mesh.vertices.Length];
073        for(int i = 0; i < colors.Length; ++i)
074        {
075            colors[i] = color;
076        }
077 
078        mesh.colors = colors;
079    }
080 
081    privatevoid setTriangles(Mesh mesh)
082    {
083        mesh.triangles =new int[]{
084            0,1,2,
085            0,2,3,
086            0,3,4,
087            0,4,5,
088            0,5,6,
089            0,6,7,
090            0,7,8,
091            0,8,9
092        };
093    }
094     
095    // Update is called once per frame
096    void FixedUpdate()
097    {
098        if(!play) return;
099        UpdateEffect(meshFilter.sharedMesh);
100    }
101 
102    publicvoid UpdateEffect(Mesh mesh)
103    {
104        float now = Time.time - start;
105        if(now >=total) { renderer.enabled = false; return; }
106        // 更新顶点数据
107        Vector3[] vertices = mesh.vertices;
108        float percent = (now % once) / once; // 每个三角面遮罩的百分比
109        int index = (int)(now / once) + 1;// 当前修改的顶点索引
110        for(int i = index; i >0; --i)
111        {
112            switch(index)
113            {
114                case1:
115                    vertices[i].x = halfWidth * percent;
116                    break;
117 
118                case2:
119                    vertices[i].y = halfHeight - halfHeight * percent;
120                    break;
121 
122                case3:
123                    vertices[i].y = -halfHeight * percent;
124                    break;
125 
126                case4:
127                    vertices[i].x = halfWidth * (1 - percent);
128                    break;
129 
130                case5:
131                    vertices[i].x = -halfWidth * percent;
132                    break;
133 
134                case6:
135                    vertices[i].y = -halfHeight * (1 - percent);
136                    break;
137 
138                case7:
139                    vertices[i].y = halfHeight * percent;
140                    break;
141 
142                case8:
143                    vertices[i].x = -halfWidth + halfWidth * percent;
144                    break;
145            }
146        }
147 
148        mesh.vertices = vertices;
149    }
150}
0 0
原创粉丝点击