Unity Editor Extensions – Handle 和Gizmos私人定制

发表于2017-10-17
评论0 2.9k浏览

今天要和大家介绍下Handle和Gizmos私人定制,先贴一个 Grid网格,可以标记一个对象的正方形范围等

拖拽到指定的对象就OK了。

  1. using UnityEngine;  
  2. using System.Collections;  
  3.    
  4. // DrawGizmoGrid.cs  
  5. // draws a useful reference grid in the editor in Unity.   
  6. // 09/01/15 - Hayden Scott-Baron  
  7. // twitter.com/docky   
  8. // no attribution needed, but please tell me if you like it ^_^  
  9.    
  10. public class DrawGizmoGrid : MonoBehaviour  
  11. {  
  12.     // universal grid scale  
  13.     public float gridScale = 1f;   
  14.    
  15.     // extents of the grid  
  16.     public int minX = -15;   
  17.     public int minY = -15;   
  18.     public int maxX = 15;   
  19.     public int maxY = 15;   
  20.    
  21.     // nudges the whole grid rel  
  22.     public Vector3 gridOffset = Vector3.zero;   
  23.    
  24.     // is this an XY or an XZ grid?  
  25.     public bool topDownGrid = true;   
  26.    
  27.     // choose a colour for the gizmos  
  28.     public int gizmoMajorLines = 5;   
  29.     public Color gizmoLineColor = new Color (0.4f, 0.4f, 0.3f, 1f);    
  30.    
  31.     // rename   centre the gameobject upon first time dragging the script into the editor.   
  32.     void Reset ()  
  33.     {  
  34.         if (name == "GameObject")  
  35.             name = "~~ GIZMO GRID ~~";   
  36.    
  37.         transform.position = Vector3.zero;   
  38.     }  
  39.    
  40.     // draw the grid :)   
  41.     void OnDrawGizmos ()  
  42.     {  
  43.         // orient to the gameobject, so you can rotate the grid independently if desired  
  44.         Gizmos.matrix = transform.localToWorldMatrix;  
  45.    
  46.         // set colours  
  47.         Color dimColor = new Color(gizmoLineColor.r, gizmoLineColor.g, gizmoLineColor.b, 0.25f* gizmoLineColor.a);   
  48.         Color brightColor = Color.Lerp (Color.white, gizmoLineColor, 0.75f);   
  49.    
  50.         // draw the horizontal lines  
  51.         for (int x = minX; x < maxX 1; x )  
  52.         {  
  53.             // find major lines  
  54.             Gizmos.color = (x % gizmoMajorLines == 0 ? gizmoLineColor : dimColor);   
  55.             if (x == 0)  
  56.                 Gizmos.color = brightColor;  
  57.    
  58.             Vector3 pos1 = new Vector3(x, minY, 0) * gridScale;    
  59.             Vector3 pos2 = new Vector3(x, maxY, 0) * gridScale;    
  60.    
  61.             // convert to topdown/overhead units if necessary  
  62.             if (topDownGrid)  
  63.             {  
  64.                 pos1 = new Vector3(pos1.x, 0, pos1.y);   
  65.                 pos2 = new Vector3(pos2.x, 0, pos2.y);   
  66.             }  
  67.    
  68.             Gizmos.DrawLine ((gridOffset   pos1), (gridOffset   pos2));   
  69.         }  
  70.    
  71.         // draw the vertical lines  
  72.         for (int y = minY; y < maxY 1; y )  
  73.         {  
  74.             // find major lines  
  75.             Gizmos.color = (y % gizmoMajorLines == 0 ? gizmoLineColor : dimColor);   
  76.             if (y == 0)  
  77.                 Gizmos.color = brightColor;  
  78.    
  79.             Vector3 pos1 = new Vector3(minX, y, 0) * gridScale;    
  80.             Vector3 pos2 = new Vector3(maxX, y, 0) * gridScale;    
  81.    
  82.             // convert to topdown/overhead units if necessary  
  83.             if (topDownGrid)  
  84.             {  
  85.                 pos1 = new Vector3(pos1.x, 0, pos1.y);   
  86.                 pos2 = new Vector3(pos2.x, 0, pos2.y);   
  87.             }  
  88.    
  89.             Gizmos.DrawLine ((gridOffset   pos1), (gridOffset   pos2));   
  90.         }  
  91.     }  
  92. }  


3Layingthe groundwork for our handles为我们处理奠定基础

4Creatinga handle

5Drawinglines in the scene view

6Coloringhandles

7Dynamicallysizing handles

  1. public class TerrainPiece : MonoBehaviour   
  2. {  
  3.   
  4.                 public SpriteRenderer spriteRenderer;  
  5.                 public Vector3 mountPoint;  
  6.                 void Awake ()   
  7.                 {  
  8.                                 if (spriteRenderer == null)  
  9.   
  10.                                                 spriteRenderer = GetComponentInChildren<SpriteRenderer>();  
  11.                 }  
  12. }  

 

下面就来定制这个类:

  1. [CustomEditor(typeof(TerrainPiece))]  
  2.   
  3. public class TerrainPieceEditor : Editor  
  4.   
  5. {  
  6.   
  7.                 SerializedProperty mountPointProp;              // TerrainPiece. mountPoint  
  8.   
  9.                 TerrainPiece terrainPiece;                                                                                               // TerrainPiece  
  10.   
  11.      
  12.   
  13.                 void OnEnable()  
  14.   
  15.                 {  
  16.   
  17.                                 terrainPiece = (TerrainPiece) target;  
  18.   
  19.                                 mountPointProp = serializedObject.FindProperty("mountPoint");  
  20.   
  21.                 }  
  22.    
  23.                 void OnSceneGUI()  
  24.   
  25.                 {  
  26.   
  27.                                 serializedObject.Update();  
  28.   
  29.    
  30.   
  31.                                 Vector3 worldMountPt = terrainPiece.transform.TransformPoint(mountPointProp.vector3Value);     // 转成世界坐标系  
  32.   
  33.    
  34.   
  35.                                 float sizeFactor = HandleUtility.GetHandleSize(worldMountPt) * 0.25f;            // 这样就不会随着scene面板的远近而动态改变大小,一直不变。  
  36.   
  37.    
  38.   
  39.                                 Handles.color = Color.magenta;                                     // 设置颜色  
  40.   
  41.                                 worldMountPt = Handles.FreeMoveHandle(worldMountPt, Quaternion.identity, sizeFactor * 0.2f, Vector3.zero, Handles.RectangleCap);     // 拖动handle来改变值  
  42.   
  43.    
  44.   
  45.                                 Handles.DrawLine(worldMountPt - Vector3.up * sizeFactor, worldMountPt   Vector3.up * sizeFactor);  
  46.   
  47.                                 Handles.DrawLine(worldMountPt - Vector3.right * sizeFactor, worldMountPt   Vector3.right * sizeFactor);  
  48.   
  49.    
  50.   
  51.                                 Vector3 mountPointLocal = terrainPiece.transform.InverseTransformPoint(worldMountPt);     // 转成相对父级的本地坐标  
  52.   
  53.                                 mountPointLocal.z = 0;  
  54.   
  55.                                 mountPointProp.vector3Value = mountPointLocal;  
  56.   
  57.                                    
  58.   
  59.                                 serializedObject.ApplyModifiedProperties();       
  60.   
  61.                 }  
  62.   
  63. }  


 

 

8Previewingthe status bar

9Sizinga status bar with handles

10Snappinghandles

  1. public class Statusbar : MonoBehaviour   
  2.   
  3. {  
  4.   
  5.                 publicstring barTextureName = "statusbar";             //The name of our bar texture (must be located in Resources folder)  
  6.   
  7.                 publicGradient colorGrad;  
  8.   
  9.                 publicVector2 offset;  
  10.   
  11.                 publicVector2 drawSize = new Vector2(5f, 1f);  
  12.   
  13.    
  14.   
  15.                 protectedTexture2D barTexture;  
  16.   
  17.                 protectedfloat targetPercent = 1f;                                //The normalized percentage the bar is to represent  
  18.   
  19.                 protectedfloat displayPercent = 1f;              //The value actually used to render the bar, allows us to animate the bar towardthe targetPercent value  
  20.   
  21.                 protectedRect srcRect;                                                                    //The rect of the area to use from the source texture  
  22.   
  23.                 protectedRect scrExtents;                                                              //The rect that defines the screen area extents  
  24.   
  25.    
  26.   
  27.                 protectedVector2 size;  
  28.   
  29. 。。。。。。。。。。。。。。。。。。。。。。  
  30.   
  31. }  

---- Editor 的定制显示?:

  1. [CustomEditor(typeof(Statusbar))]  
  2.   
  3. public class StatusbarEditor : Editor   
  4.   
  5. {  
  6.   
  7.                 SerializedPropertyoffsetProp;  
  8.   
  9.                 SerializedPropertydrawSizeProp;  
  10.   
  11.                 Statusbarbar;                                                                                                     //要定制的类  
  12.   
  13.                 staticTexture2D barTexture;                                                           //用于显示加载的图片  
  14.   
  15.    
  16.   
  17.                 voidOnEnable()  
  18.   
  19.                 {  
  20.   
  21.                                 bar= (Statusbar) target;  
  22.   
  23.    
  24.   
  25.                                 offsetProp= serializedObject.FindProperty("offset");  
  26.   
  27.                                 drawSizeProp= serializedObject.FindProperty("drawSize");  
  28.   
  29.    
  30.   
  31.                                 barTexture= Resources.Load(bar.barTextureName, typeof(Texture2D)) as Texture2D;  
  32.   
  33.                 }  
  34.   
  35.    
  36.   
  37.                 voidOnSceneGUI()  
  38.   
  39.                 {  
  40.   
  41.                                 serializedObject.Update();  
  42.   
  43.    
  44.   
  45.                                 DrawBar();  
  46.   
  47.                                 DrawHandles();  
  48.   
  49.    
  50.   
  51.                                 serializedObject.ApplyModifiedProperties();  
  52.   
  53.                 }  
  54.   
  55.    
  56.   
  57.                 voidDrawBar()  
  58.   
  59.                 {  
  60.   
  61.                                 Vector2pos = HandleUtility.WorldToGUIPoint(bar.transform.position  (Vector3)bar.offset);                                     // 坐标转换  
  62.   
  63.    
  64.   
  65.                                 Vector2size = bar.drawSize / bar.GetWorldUnitsPerPixel(Camera.current);                      //大小  
  66.   
  67.    
  68.   
  69.                                 RectscreenRect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f,  
  70.   
  71.                                                            size.x, size.y);                                           // 显示位置  
  72.   
  73.    
  74.   
  75.                                 Handles.BeginGUI();  
  76.   
  77.                                 GUI.DrawTexture(screenRect,barTexture);                                                                //显示图片  
  78.   
  79.                                 Handles.EndGUI();  
  80.   
  81.                 }  
  82.   
  83.    
  84.   
  85.                 voidDrawHandles()  
  86.   
  87.                 {  
  88.   
  89.                                 Handles.matrix= bar.transform.localToWorldMatrix;      // 矩阵变换初值  
  90.   
  91.    
  92.   
  93.                                 Vector3barPos = offsetProp.vector2Value;                                                 //位置初值  
  94.   
  95.                                 floathandleSize = HandleUtility.GetHandleSize(barPos) * 0.1f;                            // 大小初值  
  96.   
  97.    
  98.   
  99.                                 Handles.color= Color.green;                                                                           //设置颜色  
  100.   
  101.    
  102.   
  103.                                 //Bar position/offset:                                                                                        //计算位置,这是中间的圆handle  
  104.   
  105.                                 barPos= Handles.FreeMoveHandle(barPos, Quaternion.identity, handleSize, Vector3.zero,Handles.CircleCap);  
  106.   
  107.    
  108.   
  109.                                 //Save new offset:  
  110.   
  111.                                 offsetProp.vector2Value= barPos;  
  112.   
  113.    
  114.   
  115.                                 // 通过拖拽改变大小  
  116.   
  117.    
  118.   
  119.                                 //Top handle:  
  120.   
  121.                                 Vector3handlePt = barPos   Vector3.up * bar.drawSize.y * 0.5f;  
  122.   
  123.                                 Vector3newPos = Handles.FreeMoveHandle(handlePt, Quaternion.identity, handleSize,Vector3.zero, Handles.RectangleCap);  
  124.   
  125.                                 Vector2delta = new Vector2(0, newPos.y - handlePt.y);  
  126.   
  127.                                 drawSizeProp.vector2Value = delta;  
  128.   
  129.    
  130.   
  131.                                 //Bottom handle:  
  132.   
  133.                                 handlePt= barPos   Vector3.down * bar.drawSize.y * 0.5f;  
  134.   
  135.                                 newPos= Handles.FreeMoveHandle(handlePt, Quaternion.identity, handleSize,Vector3.zero, Handles.RectangleCap);  
  136.   
  137.                                 delta= new Vector2(0, newPos.y - handlePt.y);  
  138.   
  139.                                 drawSizeProp.vector2Value-= delta;  
  140.   
  141.    
  142.   
  143.                                 //Left handle:  
  144.   
  145.                                 handlePt= barPos   Vector3.left * bar.drawSize.x * 0.5f;  
  146.   
  147.                                 newPos= Handles.FreeMoveHandle(handlePt, Quaternion.identity, handleSize, Vector3.zero,Handles.RectangleCap);  
  148.   
  149.                                 delta= new Vector2(newPos.x - handlePt.x, 0);  
  150.   
  151.                                 drawSizeProp.vector2Value-= delta;  
  152.   
  153.    
  154.   
  155.                                 //Right handle:  
  156.   
  157.                                 handlePt= barPos   Vector3.right * bar.drawSize.x * 0.5f;  
  158.   
  159.                                 newPos= Handles.FreeMoveHandle(handlePt, Quaternion.identity, handleSize,Vector3.zero, Handles.RectangleCap);  
  160.   
  161.                                 delta= new Vector2(newPos.x - handlePt.x, 0);  
  162.   
  163.                                 drawSizeProp.vector2Value = delta;  
  164.   
  165.                 }  
  166.   
  167. }  

 

运行效果:

 

 

11Executingscripts in edit  mode

  1. [ExecuteInEditMode]  
  2.   
  3. public class Statusbar : MonoBehaviour   
  4.   
  5. {  }  

这样Statusbar中的代码就会执行了,在Game面板中就可以看到ship的上方有两个血条。

如果我拖动Scene面板中的血条或者大小,下方就会跟着改变。

 

还想这样改变大小怎么办:

添加如下:

  1. [ExecuteInEditMode]  
  2.   
  3. public class Statusbar : MonoBehaviour   
  4.   
  5. {  
  6.   
  7.                 voidOnGUI()  
  8.   
  9.                 {  
  10.   
  11. ………………………………………………..  
  12.  
  13. #if UNITY_EDITOR  
  14.   
  15.                                 if(!Application.isPlaying)  
  16.   
  17.                                                 size= drawSize / GetWorldUnitsPerPixel(Camera.main);  
  18.  
  19. #endif  
  20.   
  21. ………………………………………………..  
  22.   
  23.                 }  

 

 

12Drawinga detection range indicator

// 可以在Scene中动态改变坦克的攻击范围:

  1. [CustomEditor(typeof(GroundTurretAI))]  
  2.   
  3. public class GroundTurretAIEditor : Editor   
  4.   
  5. {  
  6.   
  7.                 GroundTurretAIai;  
  8.   
  9.                 SerializedPropertyrangeProp;  
  10.   
  11.    
  12.   
  13.                 voidOnEnable()  
  14.   
  15.                 {  
  16.   
  17.                                 ai= (GroundTurretAI) target;          // 要定制的脚本  
  18.   
  19.                                 rangeProp= serializedObject.FindProperty("detectionRange");      // 得到其中的范围属性  
  20.   
  21.                 }  
  22.   
  23.    
  24.   
  25.                 voidOnSceneGUI()  
  26.   
  27.                 {  
  28.   
  29.                                 serializedObject.Update();  
  30.   
  31.    
  32.   
  33.                                 Handles.color= Color.green;  
  34.   
  35.    
  36.   
  37. // RadiusHandle来拖来改变值  
  38.   
  39.                                 rangeProp.floatValue= Handles.RadiusHandle(Quaternion.identity, ai.transform.position,rangeProp.floatValue);  
  40.   
  41.    
  42.   
  43.                                 serializedObject.ApplyModifiedProperties();  
  44.   
  45.                 }  
  46.   
  47. }  

不过通过 拖动scene的视图可以发现,这个东西是3D的球形。

13、中单独做一个2D

 

13Visualizingour range indicator for 2D games

  1. [CustomEditor(typeof(GroundTurretAI))]  
  2.   
  3. public class GroundTurretAIEditor : Editor   
  4.   
  5. {  
  6.   
  7.                 GroundTurretAIai;  
  8.   
  9.                 SerializedPropertyrangeProp;  
  10.   
  11.    
  12.   
  13.                 void OnEnable()  
  14.   
  15.                 {  
  16.   
  17.                                 ai= (GroundTurretAI) target;  
  18.   
  19.                                 rangeProp= serializedObject.FindProperty("detectionRange");  
  20.   
  21.                 }  
  22.   
  23.    
  24.   
  25.                 voidOnSceneGUI()  
  26.   
  27.                 {  
  28.   
  29.                                 serializedObject.Update();  
  30.   
  31.    
  32.   
  33.                                 Handles.color= Color.green;  
  34.   
  35.    
  36.   
  37.                                 Vector3aiPos = ai.transform.position;  
  38.   
  39.                                 floathandleSize = HandleUtility.GetHandleSize(aiPos) * 0.15f;  
  40.   
  41.    
  42.   
  43.                                 //Left handle:  
  44.   
  45.                                 Vector3handlePos = aiPos   Vector3.left * rangeProp.floatValue;  
  46.   
  47.                                 handlePos= Handles.FreeMoveHandle(handlePos, Quaternion.identity, handleSize,Vector3.zero, Handles.SphereCap);  
  48.   
  49.                                 rangeProp.floatValue= Vector3.Distance(aiPos, handlePos);  
  50.   
  51.    
  52.   
  53.                                 //Right handle:  
  54.   
  55.                                 handlePos= aiPos   Vector3.right * rangeProp.floatValue;  
  56.   
  57.                                 handlePos= Handles.FreeMoveHandle(handlePos, Quaternion.identity, handleSize,Vector3.zero, Handles.SphereCap);  
  58.   
  59.                                 rangeProp.floatValue= Vector3.Distance(aiPos, handlePos);  
  60.   
  61.    
  62.   
  63.                                 //Top handle:  
  64.   
  65.                                 handlePos= aiPos   Vector3.up * rangeProp.floatValue;  
  66.   
  67.                                 handlePos= Handles.FreeMoveHandle(handlePos, Quaternion.identity, handleSize,Vector3.zero, Handles.SphereCap);  
  68.   
  69.                                 rangeProp.floatValue= Vector3.Distance(aiPos, handlePos);  
  70.   
  71.    
  72.   
  73.                                 //Bottom handle:  
  74.   
  75.                                 handlePos= aiPos   Vector3.down * rangeProp.floatValue;  
  76.   
  77.                                 handlePos= Handles.FreeMoveHandle(handlePos, Quaternion.identity, handleSize,Vector3.zero, Handles.SphereCap);  
  78.   
  79.                                 rangeProp.floatValue= Vector3.Distance(aiPos, handlePos);  
  80.   
  81.    
  82.   
  83.                                 //Detection area:  
  84.   
  85.                                 Handles.color= new Color(0, 1f, 0, 0.15f);  
  86.   
  87.                                 Handles.DrawSolidDisc(aiPos,Vector3.back, rangeProp.floatValue);  
  88.   
  89.    
  90.   
  91.                                 serializedObject.ApplyModifiedProperties();  
  92.   
  93.                 }  
  94.   
  95. }  

 

看看效果:

通过拖动四个中的任何一个绿色的小圆,就可以改变范围了。

 

 

14Drawingthe turret field of fire handles

  1. [CustomEditor(typeof(GroundTurret))]  
  2.   
  3. public class GroundTurretEditor : Editor  
  4.   
  5. {  
  6.   
  7.                 constfloat arcRadius = 12f;  
  8.   
  9.                 SerializedPropertyminAngleProp;  
  10.   
  11.                 SerializedPropertymaxAngleProp;  
  12.   
  13.    
  14.   
  15.                 GroundTurretturret;  
  16.   
  17.    
  18.   
  19.                 voidOnEnable()  
  20.   
  21.                 {  
  22.   
  23.                                 turret= (GroundTurret) target;   // 要定制的脚本  
  24.   
  25.                                                                                                                                                 //目标属性  
  26.   
  27.                                 minAngleProp= serializedObject.FindProperty("minAngle");  
  28.   
  29.                                 maxAngleProp= serializedObject.FindProperty("maxAngle");  
  30.   
  31.                 }  
  32.   
  33.    
  34.   
  35.                 voidOnSceneGUI()  
  36.   
  37.                 {  
  38.   
  39.                                 serializedObject.Update();  
  40.   
  41.    
  42.   
  43.                                 //Set the handle color:  
  44.   
  45.                                 Handles.color= Color.red;  
  46.   
  47.    
  48.   
  49.                                 //Draw handle controllings the minimum angle:  
  50.   
  51.                                 Vector3handlePos = turret.transform.position  turret.transform.TransformDirection(HandleHelper.GetVectorFromAngle(turret.minAngle))* arcRadius;  
  52.   
  53.                                 floathandleSize = HandleUtility.GetHandleSize(handlePos) * 0.1f;  
  54.   
  55.                                 handlePos= Handles.FreeMoveHandle(handlePos, Quaternion.identity, handleSize,Vector3.zero, Handles.CircleCap);  
  56.   
  57.    
  58.   
  59.                                 //Calculate the angle from the new handle position:  
  60.   
  61.                                 minAngleProp.floatValue= HandleHelper.GetAngleFromHandlePos(handlePos, turret.transform);  
  62.   
  63.    
  64.   
  65.    
  66.   
  67.                                 //Draw handle controllings the maximum angle:  
  68.   
  69.                                 handlePos= turret.transform.position  turret.transform.TransformDirection(HandleHelper.GetVectorFromAngle(turret.maxAngle))* arcRadius;  
  70.   
  71.                                 handleSize= HandleUtility.GetHandleSize(handlePos) * 0.1f;  
  72.   
  73.                                 handlePos= Handles.FreeMoveHandle(handlePos, Quaternion.identity, handleSize,Vector3.zero, Handles.CircleCap);  
  74.   
  75.                                   
  76.   
  77.                                 //Calculate the angle from the new handle position:  
  78.   
  79.                                 maxAngleProp.floatValue= HandleHelper.GetAngleFromHandlePos(handlePos, turret.transform);  
  80.   
  81.    
  82.   
  83.                                 serializedObject.ApplyModifiedProperties();  
  84.   
  85.                 }  
  86.   
  87. }  

通过拖动两个红色的小圆圈,就确定了,炮筒的攻击视野了。

 

 

15Visualizingthe turret field of fire

 

就在14、中的最后添加如下的内容:

  1. //Calculate the angle from the new handle position:  
  2.   
  3. maxAngleProp.floatValue= HandleHelper.GetAngleFromHandlePos(handlePos, turret.transform);  
  4.   
  5.   
  6.   
  7. //Draw field-of-fire arc:  
  8.   
  9. Handles.color= new Color(1f, 0, 0, 0.1f);  
  10.   
  11. Vector3startVec = turret.transform.TransformDirection(HandleHelper.GetVectorFromAngle(minAngleProp.floatValue));  
  12.   
  13. Handles.DrawSolidArc(turret.transform.position,Vector3.forward, startVec, maxAngleProp.floatValue - minAngleProp.floatValue,arcRadius);  
  14.   
  15.   
  16.   
  17. serializedObject.ApplyModifiedProperties();  

 

看看效果呢?

 

16IntroducingGizmos

来到 Gun.cs的脚本中添加如下的函数:

例如:

  1. voidOnDrawGizmos ()       // 函数会一直执行  
  2.   
  3. {  
  4.   
  5.                 Gizmos.DrawSphere(transform.position, 3f);  

 


  1. void OnDrawGizmosSelected()                      // 函数只有脚本所在对象被选择时执行  
  2.   
  3. {  
  4.   
  5.                 Vector3shotVector = Vector3.zero;  
  6.   
  7.                 Vector3arrowTip;  
  8.   
  9.                 Vector3arrowLeft = Vector3.zero;  
  10.   
  11.                 Vector3arrowRight = Vector3.zero;  
  12.   
  13.                 floatarrowLength = 5f;  
  14.   
  15.                   
  16.   
  17.                 switch(direction)  
  18.   
  19.                 {  
  20.   
  21.                 caseShotDirection.Up:  
  22.   
  23.                                 shotVector= Vector3.up;  
  24.   
  25.                                 arrowLeft= Vector3.left * arrowLength * 0.2f;  
  26.   
  27.                                 arrowRight= -arrowLeft;  
  28.   
  29.                                 break;  
  30.   
  31.                 caseShotDirection.Down:  
  32.   
  33.                                 shotVector= Vector3.down;  
  34.   
  35.                                 arrowLeft= Vector3.right * arrowLength * 0.2f;  
  36.   
  37.                                 arrowRight= -arrowLeft;  
  38.   
  39.                                 break;  
  40.   
  41.                 caseShotDirection.Left:  
  42.   
  43.                                 shotVector= Vector3.left;  
  44.   
  45.                                 arrowLeft= Vector3.down * arrowLength * 0.2f;  
  46.   
  47.                                 arrowRight= -arrowLeft;  
  48.   
  49.                                 break;  
  50.   
  51.                 caseShotDirection.Right:  
  52.   
  53.                                 shotVector= Vector3.right;  
  54.   
  55.                                 arrowLeft= Vector3.up * arrowLength * 0.2f;  
  56.   
  57.                                 arrowRight= -arrowLeft;  
  58.   
  59.                                 break;  
  60.   
  61.                 }  
  62.   
  63.                   
  64.   
  65.                 arrowTip= shotVector * arrowLength;  
  66.   
  67.                 arrowLeft = shotVector * arrowLength * 0.7f;  
  68.   
  69.                 arrowRight = shotVector * arrowLength * 0.7f;  
  70.   
  71.                   
  72.   
  73.                 Gizmos.color= Color.yellow;  
  74.   
  75.                 Gizmos.matrix= transform.localToWorldMatrix;  
  76.   
  77.                   
  78.   
  79.                 Gizmos.DrawLine(arrowTip,Vector3.zero);  
  80.   
  81.                 Gizmos.DrawLine(arrowTip,arrowLeft);  
  82.   
  83.                 Gizmos.DrawLine(arrowTip,arrowRight);  
  84.   
  85. }  


 

 

17DrawingGizmos in a custom editor to complete our course

16中,把相当于Editor的内容写在逻辑脚本中很不合适。

所以:

  1. [CustomEditor(typeof(Gun))]  
  2.   
  3. public class GunEditor : Editor   
  4.   
  5. {  
  6.   
  7.                 [DrawGizmo(GizmoType.SelectedOrChild)]  
  8.   
  9.                 staticvoid DrawDirection(Gun gun, GizmoType gizmoType)  
  10.   
  11.                 {  
  12.   
  13.                                 if(GizmoType.Selected == (gizmoType & GizmoType.Selected))   // 脚本所在对象被选择就不执行,父对象没有问题:  
  14.   
  15.                                                 return;  
  16.   
  17.    
  18.   
  19.                                 Vector3shotVector = Vector3.zero;  
  20.   
  21.                                 Vector3arrowTip;  
  22.   
  23.                                 Vector3arrowLeft = Vector3.zero;  
  24.   
  25.                                 Vector3arrowRight = Vector3.zero;  
  26.   
  27.                                 floatarrowLength = 5f;  
  28.   
  29.                                   
  30.   
  31.                                 switch(gun.direction)  
  32.   
  33.                                 {  
  34.   
  35.                                 caseGun.ShotDirection.Up:  
  36.   
  37.                                                 shotVector= Vector3.up;  
  38.   
  39.                                                 arrowLeft= Vector3.left * arrowLength * 0.2f;  
  40.   
  41.                                                 arrowRight= -arrowLeft;  
  42.   
  43.                                                 break;  
  44.   
  45.                                 caseGun.ShotDirection.Down:  
  46.   
  47.                                                 shotVector= Vector3.down;  
  48.   
  49.                                                 arrowLeft= Vector3.right * arrowLength * 0.2f;  
  50.   
  51.                                                 arrowRight= -arrowLeft;  
  52.   
  53.                                                 break;  
  54.   
  55.                                 caseGun.ShotDirection.Left:  
  56.   
  57.                                                 shotVector= Vector3.left;  
  58.   
  59.                                                 arrowLeft= Vector3.down * arrowLength * 0.2f;  
  60.   
  61.                                                 arrowRight= -arrowLeft;  
  62.   
  63.                                                 break;  
  64.   
  65.                                 caseGun.ShotDirection.Right:  
  66.   
  67.                                                 shotVector= Vector3.right;  
  68.   
  69.                                                 arrowLeft= Vector3.up * arrowLength * 0.2f;  
  70.   
  71.                                                 arrowRight= -arrowLeft;  
  72.   
  73.                                                 break;  
  74.   
  75.                                 }  
  76.   
  77.                                   
  78.   
  79.                                 arrowTip= shotVector * arrowLength;  
  80.   
  81.                                 arrowLeft = shotVector * arrowLength * 0.7f;  
  82.   
  83.                                 arrowRight = shotVector * arrowLength * 0.7f;  
  84.   
  85.                                   
  86.   
  87.                                 Gizmos.color= Color.yellow;  
  88.   
  89.                                 Gizmos.matrix= gun.transform.localToWorldMatrix;  
  90.   
  91.                                   
  92.   
  93.                                 Gizmos.DrawLine(arrowTip,Vector3.zero);  
  94.   
  95.                                 Gizmos.DrawLine(arrowTip,arrowLeft);  
  96.   
  97.                                 Gizmos.DrawLine(arrowTip,arrowRight);  
  98.   
  99.                 }  
  100.   
  101. }  

 

 

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论