[伊利丹·怒风] Unity3D 绘制矩形(DrawRect)及可视化调试

来源:互联网 发布:淘宝美工用色 编辑:程序博客网 时间:2024/05/18 03:59


背景

由于要做Native2D的A* 算法寻路,所以必须进行Scene的网格划分,而进行了网格划分需要进行可视化的调试,需求就是这个逻辑。具体是实现如下效果,这里绿色的区域就是角色不能进入的区域(或者是走不进去的地方,大家玩网游都遇到过吧)

3

按照这个思路基本是两个问题,1、如何实现可视化调试;2、Unity如何绘制网格

实现

1、Unity如何实现可视化调试

本文的实现完全围绕需求来走,就是需要在2D环境中实现可配置网格显示。先按照背景中的思路学习轮子,百度下基本下篇大牛的总结比较好,比较全

【风宇冲】图形化调试 http://blog.sina.com.cn/s/blog_471132920101gxzf.html

这里可耻的转载一下

Unity中图形化调试主要4种
Debug.Draw
Gizmos.Draw
Graphic.DrawMesh
GL
只需在Scene窗口显示的调试图像
     一直显示的 OnDrawGizmos + Gizmos.Draw
     选中显示的 OnDrawGizmosSelected + Gizmos.Draw
     脚本控制的 Update + Debug.Draw
需要在实际设备屏幕显示的调试图像
    Update+Graphic.DrawMesh
    OnRenderObject+GL
 
Graphic.DrawMesh和Debug.Draw   调用一致,都是在Update系里
Graphic.DrawMesh和GL       显示类似,都在各个窗口显示,并且可以设置材质。

 

这里根据我自己的要求,只要能在Editor模式下的Scene和Game中显示就可以了。

2、Unity如何绘制网格

在1中 大牛已经给出了方向和简单的列子,下面就是自己实现了,刚开始比较懒,还是想一步到位,直接找到一个类似DrawRect的函数直接在中绘制矩形,结果果然失败,找了很久也没有这样的函数和或者别人分享的。下面分享下自己搜集的方案

A、使用Mesh网格进行绘制

Mesh

说实话我觉得这种方法对于我的需求是没什么实际作用的,而且看起来有些虚,最主要的问题是网格不可控制。

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
publicclass Grid : MonoBehaviour
{
    [ SerializeField ] privateTransform _transform;
    [ SerializeField ] privateMaterial _material;
    [ SerializeField ] privateVector2 _gridSize;
    [ SerializeField ] privateint _rows;
    [ SerializeField ] privateint _columns;
    voidStart()
    {
        UpdateGrid();
    }
    publicvoid UpdateGrid()
    {
        _transform.localScale = newVector3( _gridSize.x, _gridSize.y, 1.0f );
        _material.SetTextureScale( \"_MainTex\", newVector2( _columns, _rows ) );
    }
}

B、第二个方法就是使用GL

GL

这种方法其实还是比较底层的,竟然还用到了传说中的Shader,用于拔高也是不错的列子,而且看起来也不虚,但最主要的问题是网格不可控制。再说我也觉得用GL有点杀鸡用牛刀的感觉。

[C#] 纯文本查看 复制代码
?
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
usingUnityEngine;
usingSystem.Collections;
//http://answers.unity3d.com/questions/482128/draw-grid-lines-in-game-view.html?page=2&pageSize=5&sort=votes
 
 
publicclass ShowGrid : MonoBehaviour {
 
    publicbool showMain = true;
    publicbool showSub = false;
 
    publicfloat gridSizeX;
    publicfloat gridSizeY;
 
    publicfloat smallStep;
    publicfloat largeStep;
 
    publicfloat startX;
    publicfloat startY;
 
    publicfloat offsetY;
    publicfloat offsetX;
 
    privateMaterial lineMaterial;
 
    privateColor mainColor = newColor(0f, 1f, 0f, 1f);
    privateColor subColor = newColor(0f, 0.5f, 0f, 1f);
 
    voidStart()
    {
 
        gridSizeX *= smallStep;
        gridSizeY *= smallStep;
    }
 
    voidUpdate()
    {
    }
 
    voidCreateLineMaterial()
    {
 
        if(!lineMaterial)
        {
            lineMaterial = newMaterial(\"Shader \\"Lines/Colored Blended\\" {\" +
                \"SubShader { Pass { \" +
                \"    Blend SrcAlpha OneMinusSrcAlpha \" +
                \"    ZWrite Off Cull Off Fog { Mode Off } \" +
                \"    BindChannels {\" +
                \"      Bind \\"vertex\\", vertex Bind \\"color\\", color }\" +
                \"} } }\");
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
        }
    }
 
    voidOnRenderObject()
    {
        CreateLineMaterial();
        // set the current material
        lineMaterial.SetPass(0);
        GL.Begin(GL.LINES);
 
        if(showSub)
        {
            GL.Color(subColor);
 
            //Layers
            for(floatj = 0; j <= gridSizeY; j += smallStep)
            {
                //X axis lines
                GL.Vertex3(startX + offsetX, j + offsetY, 0);
                GL.Vertex3(gridSizeX + offsetX, j + offsetY, 0);
            }
 
            //Y axis lines
            for(floatk = 0; k <= gridSizeX; k += smallStep)
            {
                GL.Vertex3(startX + k + offsetX, startY + offsetY, 0);
                GL.Vertex3(startX + k + offsetX, gridSizeY + offsetY, 0);
            }
 
        }
 
        if(showMain)
        {
            GL.Color(mainColor);
 
            //Layers
            for(floatj = 0; j <= gridSizeY; j += largeStep)
            {
                //X axis lines
                GL.Vertex3(startX + offsetX, j + offsetY, 0);
                GL.Vertex3(gridSizeX + offsetX, j + offsetY, 0);
            }
 
            //Y axis lines
            for(floatk = 0; k <= gridSizeX; k += largeStep)
            {
                GL.Vertex3(startX + k + offsetX, startY + offsetY, 0);
                GL.Vertex3(startX + k + offsetX, gridSizeY + offsetY, 0);
            }
        }
        GL.End();
    }
}

C、第三个方法Gizmos.Draw

这也是我最终使用的方法,实现效果如下所示,其实Gizmos的功能很强大,可以绘制Mesh和Cude,在3D调试中用处不能小视。

drawGrid

这里主要遇到的问题是Gizmos没有提供DrawRect函数需要自己实现,这里其实本身没有什么技术难度,因为已经提供了DrawLine函数。自己比较懒,本来想看是否有现成的直接剽窃下,后来Seach未果,只能自己实现了,下面是实现函数,同时提供DrawPath和DrawLine函数这里是从itween里提取出来的

[C#] 纯文本查看 复制代码
?
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/// <summary>
   /// When called from an OnDrawGizmos() function it will draw a curved path through the provided array of Vector3s.
   /// </summary>
   /// <param name="\"path\"">
   /// A <see cref="\"Vector3s[]\"/">
   ///
   /// <param name="\"color\"">
   /// A <see cref="\"Color\"/">
   /// 
   publicstatic void DrawPath(Vector3[] path, Color color)
   {
       if(path.Length > 0)
       {
           DrawPathHelper(path, color, \"gizmos\");
       }
   }
 
   /// <summary>
   /// When called from an OnDrawGizmos() function it will draw a line through the provided array of Vector3s.
   /// </summary>
   /// <param name="\"line\"">
   /// A <see cref="\"Vector3s[]\"/">
   ///
   /// <param name="\"color\"">
   /// A <see cref="\"Color\"/">
   /// 
   publicstatic void DrawLine(Vector3[] line, Color color)
   {
       if(line!=null&& line.Length > 0)
       {
           DrawLineHelper(line, color, \"gizmos\");
       }
   }
 
   publicstatic void DrawRect( Rect rect, Color color)
   {
       Vector3[] line =newVector3[5];
       line[0] = newVector3(rect.x,rect.y,0);
       line[1] = newVector3(rect.x+rect.width, rect.y, 0);
       line[2] = newVector3(rect.x + rect.width, rect.y + rect.height, 0);
       line[3] = newVector3(rect.x, rect.y + rect.height, 0);
       line[4] = newVector3(rect.x, rect.y, 0);
       if(line != null&& line.Length > 0)
       {
           DrawLineHelper(line, color, \"gizmos\");
       }
   }  
 
   privatestatic void DrawLineHelper(Vector3[] line, Color color, stringmethod)
   {
       Gizmos.color = color;
       for(inti = 0; i < line.Length - 1; i++)
       {
           if(method == \"gizmos\")
           {
               Gizmos.DrawLine(line[i], line[i + 1]); ;
           }
           elseif (method == \"handles\")
           {
               Debug.LogError(\"iTween Error: Drawing a line with Handles istemporarily disabled because of compatability issues with Unity 2.6!\");
               //UnityEditor.Handles.DrawLine(line[i], line[i+1]);
           }
       }
   }
 
   privatestatic void DrawPathHelper(Vector3[] path, Color color, stringmethod)
   {
       Vector3[] vector3s = PathControlPointGenerator(path);
 
       //Line Draw:
       Vector3 prevPt = Interp(vector3s, 0);
       Gizmos.color = color;
       intSmoothAmount = path.Length * 20;
       for(inti = 1; i <= SmoothAmount; i++)
       {
           floatpm = (float)i / SmoothAmount;
           Vector3 currPt = Interp(vector3s, pm);
           if(method == \"gizmos\")
           {
               Gizmos.DrawLine(currPt, prevPt);
           }
           elseif (method == \"handles\")
           {
               Debug.LogError(\"iTween Error: Drawing a path with Handles istemporarily disabled because of compatability issues with Unity 2.6!\");
               //UnityEditor.Handles.DrawLine(currPt, prevPt);
           }
           prevPt = currPt;
       }
   }
   /// <summary>
   /// 三点计算抛物线.
   /// </summary>
   /// <returns>组成抛物线的点.</returns>
   /// <param name="\"path\"">确定抛物线的三个点或者更多的点的数组.
   publicstatic List<vector3> DrawPathHelper(Vector3[] path)
   {
       List<vector3> array = newList<vector3>(177);
       Vector3[] vector3s = PathControlPointGenerator(path);
       //Line Draw:
       Vector3 prevPt = Interp(vector3s, 0);
       intSmoothAmount = path.Length * 20;
       for(inti = 1; i <= SmoothAmount; i++)
       {
           floatpm = (float)i / SmoothAmount;
           Vector3 currPt = Interp(vector3s, pm);
           array.Add(currPt);
           prevPt = currPt;
       }
       returnarray;
   }
   privatestatic Vector3[] PathControlPointGenerator(Vector3[] path)
   {
       Vector3[] suppliedPath;
       Vector3[] vector3s;
       //create and store path points:
       suppliedPath = path;
       //populate calculate path;
       intoffset = 2;
       vector3s = newVector3[suppliedPath.Length + offset];
       System.Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);
       //populate start and end control points:
       //vector3s[0] = vector3s[1] - vector3s[2];
       vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
       vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);
       //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
       if(vector3s[1] == vector3s[vector3s.Length - 2])
       {
           Vector3[] tmpLoopSpline = newVector3[vector3s.Length];
           System.Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
           tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
           tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
           vector3s = newVector3[tmpLoopSpline.Length];
           System.Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
       }
       return(vector3s);
   }
   //andeeee from the Unity forum's steller Catmull-Rom class ( http://forum.unity3d.com/viewtopic.php?p=218400#218400 ):
   privatestatic Vector3 Interp(Vector3[] pts, floatt)
   {
       intnumSections = pts.Length - 3;
       intcurrPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
       floatu = t * (float)numSections - (float)currPt;
       Vector3 a = pts[currPt];
       Vector3 b = pts[currPt + 1];
       Vector3 c = pts[currPt + 2];
       Vector3 d = pts[currPt + 3];
       return.5f * (
           (-a + 3f * b - 3f * c + d) * (u * u * u)
           + (2f * a - 5f * b + 4f * c - d) * (u * u)
           + (-a + c) * u
           + 2f * b
           );
   }</vector3></vector3></vector3></see></see></see></see>

总结

程序员都懒总想找现成的,这次我提供个现成的给你哦。如果真对你有用别忘记给我投一票。

2016年度游戏蛮牛最受欢迎专栏作家评选,为他们投票! 别忘记我是恶魔猎手哦(wind2006)

3.jpg (153.87 KB, 下载次数: 0)

3.jpg

背景

由于要做Native2D的A* 算法寻路,所以必须进行Scene的网格划分,而进行了网格划分需要进行可视化的调试,需求就是这个逻辑。具体是实现如下效果,这里绿色的区域就是角色不能进入的区域(或者是走不进去的地方,大家玩网游都遇到过吧)

3

按照这个思路基本是两个问题,1、如何实现可视化调试;2、Unity如何绘制网格

实现

1、Unity如何实现可视化调试

本文的实现完全围绕需求来走,就是需要在2D环境中实现可配置网格显示。先按照背景中的思路学习轮子,百度下基本下篇大牛的总结比较好,比较全

【风宇冲】图形化调试 http://blog.sina.com.cn/s/blog_471132920101gxzf.html

这里可耻的转载一下

Unity中图形化调试主要4种
Debug.Draw
Gizmos.Draw
Graphic.DrawMesh
GL
只需在Scene窗口显示的调试图像
     一直显示的 OnDrawGizmos + Gizmos.Draw
     选中显示的 OnDrawGizmosSelected + Gizmos.Draw
     脚本控制的 Update + Debug.Draw
需要在实际设备屏幕显示的调试图像
    Update+Graphic.DrawMesh
    OnRenderObject+GL
 
Graphic.DrawMesh和Debug.Draw   调用一致,都是在Update系里
Graphic.DrawMesh和GL       显示类似,都在各个窗口显示,并且可以设置材质。

 

这里根据我自己的要求,只要能在Editor模式下的Scene和Game中显示就可以了。

2、Unity如何绘制网格

在1中 大牛已经给出了方向和简单的列子,下面就是自己实现了,刚开始比较懒,还是想一步到位,直接找到一个类似DrawRect的函数直接在中绘制矩形,结果果然失败,找了很久也没有这样的函数和或者别人分享的。下面分享下自己搜集的方案

A、使用Mesh网格进行绘制

Mesh

说实话我觉得这种方法对于我的需求是没什么实际作用的,而且看起来有些虚,最主要的问题是网格不可控制。

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
publicclass Grid : MonoBehaviour
{
    [ SerializeField ] privateTransform _transform;
    [ SerializeField ] privateMaterial _material;
    [ SerializeField ] privateVector2 _gridSize;
    [ SerializeField ] privateint _rows;
    [ SerializeField ] privateint _columns;
    voidStart()
    {
        UpdateGrid();
    }
    publicvoid UpdateGrid()
    {
        _transform.localScale = newVector3( _gridSize.x, _gridSize.y, 1.0f );
        _material.SetTextureScale( \"_MainTex\", newVector2( _columns, _rows ) );
    }
}

B、第二个方法就是使用GL

GL

这种方法其实还是比较底层的,竟然还用到了传说中的Shader,用于拔高也是不错的列子,而且看起来也不虚,但最主要的问题是网格不可控制。再说我也觉得用GL有点杀鸡用牛刀的感觉。

[C#] 纯文本查看 复制代码
?
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
usingUnityEngine;
usingSystem.Collections;
//http://answers.unity3d.com/questions/482128/draw-grid-lines-in-game-view.html?page=2&pageSize=5&sort=votes
 
 
publicclass ShowGrid : MonoBehaviour {
 
    publicbool showMain = true;
    publicbool showSub = false;
 
    publicfloat gridSizeX;
    publicfloat gridSizeY;
 
    publicfloat smallStep;
    publicfloat largeStep;
 
    publicfloat startX;
    publicfloat startY;
 
    publicfloat offsetY;
    publicfloat offsetX;
 
    privateMaterial lineMaterial;
 
    privateColor mainColor = newColor(0f, 1f, 0f, 1f);
    privateColor subColor = newColor(0f, 0.5f, 0f, 1f);
 
    voidStart()
    {
 
        gridSizeX *= smallStep;
        gridSizeY *= smallStep;
    }
 
    voidUpdate()
    {
    }
 
    voidCreateLineMaterial()
    {
 
        if(!lineMaterial)
        {
            lineMaterial = newMaterial(\"Shader \\"Lines/Colored Blended\\" {\" +
                \"SubShader { Pass { \" +
                \"    Blend SrcAlpha OneMinusSrcAlpha \" +
                \"    ZWrite Off Cull Off Fog { Mode Off } \" +
                \"    BindChannels {\" +
                \"      Bind \\"vertex\\", vertex Bind \\"color\\", color }\" +
                \"} } }\");
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
        }
    }
 
    voidOnRenderObject()
    {
        CreateLineMaterial();
        // set the current material
        lineMaterial.SetPass(0);
        GL.Begin(GL.LINES);
 
        if(showSub)
        {
            GL.Color(subColor);
 
            //Layers
            for(floatj = 0; j <= gridSizeY; j += smallStep)
            {
                //X axis lines
                GL.Vertex3(startX + offsetX, j + offsetY, 0);
                GL.Vertex3(gridSizeX + offsetX, j + offsetY, 0);
            }
 
            //Y axis lines
            for(floatk = 0; k <= gridSizeX; k += smallStep)
            {
                GL.Vertex3(startX + k + offsetX, startY + offsetY, 0);
                GL.Vertex3(startX + k + offsetX, gridSizeY + offsetY, 0);
            }
 
        }
 
        if(showMain)
        {
            GL.Color(mainColor);
 
            //Layers
            for(floatj = 0; j <= gridSizeY; j += largeStep)
            {
                //X axis lines
                GL.Vertex3(startX + offsetX, j + offsetY, 0);
                GL.Vertex3(gridSizeX + offsetX, j + offsetY, 0);
            }
 
            //Y axis lines
            for(floatk = 0; k <= gridSizeX; k += largeStep)
            {
                GL.Vertex3(startX + k + offsetX, startY + offsetY, 0);
                GL.Vertex3(startX + k + offsetX, gridSizeY + offsetY, 0);
            }
        }
        GL.End();
    }
}

C、第三个方法Gizmos.Draw

这也是我最终使用的方法,实现效果如下所示,其实Gizmos的功能很强大,可以绘制Mesh和Cude,在3D调试中用处不能小视。

drawGrid

这里主要遇到的问题是Gizmos没有提供DrawRect函数需要自己实现,这里其实本身没有什么技术难度,因为已经提供了DrawLine函数。自己比较懒,本来想看是否有现成的直接剽窃下,后来Seach未果,只能自己实现了,下面是实现函数,同时提供DrawPath和DrawLine函数这里是从itween里提取出来的

[C#] 纯文本查看 复制代码
?
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/// <summary>
   /// When called from an OnDrawGizmos() function it will draw a curved path through the provided array of Vector3s.
   /// </summary>
   /// <param name="\"path\"">
   /// A <see cref="\"Vector3s[]\"/">
   ///
   /// <param name="\"color\"">
   /// A <see cref="\"Color\"/">
   /// 
   publicstatic void DrawPath(Vector3[] path, Color color)
   {
       if(path.Length > 0)
       {
           DrawPathHelper(path, color, \"gizmos\");
       }
   }
 
   /// <summary>
   /// When called from an OnDrawGizmos() function it will draw a line through the provided array of Vector3s.
   /// </summary>
   /// <param name="\"line\"">
   /// A <see cref="\"Vector3s[]\"/">
   ///
   /// <param name="\"color\"">
   /// A <see cref="\"Color\"/">
   /// 
   publicstatic void DrawLine(Vector3[] line, Color color)
   {
       if(line!=null&& line.Length > 0)
       {
           DrawLineHelper(line, color, \"gizmos\");
       }
   }
 
   publicstatic void DrawRect( Rect rect, Color color)
   {
       Vector3[] line =newVector3[5];
       line[0] = newVector3(rect.x,rect.y,0);
       line[1] = newVector3(rect.x+rect.width, rect.y, 0);
       line[2] = newVector3(rect.x + rect.width, rect.y + rect.height, 0);
       line[3] = newVector3(rect.x, rect.y + rect.height, 0);
       line[4] = newVector3(rect.x, rect.y, 0);
       if(line != null&& line.Length > 0)
       {
           DrawLineHelper(line, color, \"gizmos\");
       }
   }  
 
   privatestatic void DrawLineHelper(Vector3[] line, Color color, stringmethod)
   {
       Gizmos.color = color;
       for(inti = 0; i < line.Length - 1; i++)
       {
           if(method == \"gizmos\")
           {
               Gizmos.DrawLine(line[i], line[i + 1]); ;
           }
           elseif (method == \"handles\")
           {
               Debug.LogError(\"iTween Error: Drawing a line with Handles istemporarily disabled because of compatability issues with Unity 2.6!\");
               //UnityEditor.Handles.DrawLine(line[i], line[i+1]);
           }
       }
   }
 
   privatestatic void DrawPathHelper(Vector3[] path, Color color, stringmethod)
   {
       Vector3[] vector3s = PathControlPointGenerator(path);
 
       //Line Draw:
       Vector3 prevPt = Interp(vector3s, 0);
       Gizmos.color = color;
       intSmoothAmount = path.Length * 20;
       for(inti = 1; i <= SmoothAmount; i++)
       {
           floatpm = (float)i / SmoothAmount;
           Vector3 currPt = Interp(vector3s, pm);
           if(method == \"gizmos\")
           {
               Gizmos.DrawLine(currPt, prevPt);
           }
           elseif (method == \"handles\")
           {
               Debug.LogError(\"iTween Error: Drawing a path with Handles istemporarily disabled because of compatability issues with Unity 2.6!\");
               //UnityEditor.Handles.DrawLine(currPt, prevPt);
           }
           prevPt = currPt;
       }
   }
   /// <summary>
   /// 三点计算抛物线.
   /// </summary>
   /// <returns>组成抛物线的点.</returns>
   /// <param name="\"path\"">确定抛物线的三个点或者更多的点的数组.
   publicstatic List<vector3> DrawPathHelper(Vector3[] path)
   {
       List<vector3> array = newList<vector3>(177);
       Vector3[] vector3s = PathControlPointGenerator(path);
       //Line Draw:
       Vector3 prevPt = Interp(vector3s, 0);
       intSmoothAmount = path.Length * 20;
       for(inti = 1; i <= SmoothAmount; i++)
       {
           floatpm = (float)i / SmoothAmount;
           Vector3 currPt = Interp(vector3s, pm);
           array.Add(currPt);
           prevPt = currPt;
       }
       returnarray;
   }
   privatestatic Vector3[] PathControlPointGenerator(Vector3[] path)
   {
       Vector3[] suppliedPath;
       Vector3[] vector3s;
       //create and store path points:
       suppliedPath = path;
       //populate calculate path;
       intoffset = 2;
       vector3s = newVector3[suppliedPath.Length + offset];
       System.Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);
       //populate start and end control points:
       //vector3s[0] = vector3s[1] - vector3s[2];
       vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
       vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);
       //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
       if(vector3s[1] == vector3s[vector3s.Length - 2])
       {
           Vector3[] tmpLoopSpline = newVector3[vector3s.Length];
           System.Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
           tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
           tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
           vector3s = newVector3[tmpLoopSpline.Length];
           System.Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
       }
       return(vector3s);
   }
   //andeeee from the Unity forum's steller Catmull-Rom class ( http://forum.unity3d.com/viewtopic.php?p=218400#218400 ):
   privatestatic Vector3 Interp(Vector3[] pts, floatt)
   {
       intnumSections = pts.Length - 3;
       intcurrPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
       floatu = t * (float)numSections - (float)currPt;
       Vector3 a = pts[currPt];
       Vector3 b = pts[currPt + 1];
       Vector3 c = pts[currPt + 2];
       Vector3 d = pts[currPt + 3];
       return.5f * (
           (-a + 3f * b - 3f * c + d) * (u * u * u)
           + (2f * a - 5f * b + 4f * c - d) * (u * u)
           + (-a + c) * u
           + 2f * b
           );
   }</vector3></vector3></vector3></see></see></see></see>

总结

程序员都懒总想找现成的,这次我提供个现成的给你哦。如果真对你有用别忘记给我投一票。

2016年度游戏蛮牛最受欢迎专栏作家评选,为他们投票! 别忘记我是恶魔猎手哦(wind2006)

3.jpg (153.87 KB, 下载次数: 0)

3.jpg
0 0
原创粉丝点击