unity视图扩展的一些用法

最近总结了unity视图编辑的一些用法:

一.划线

     1.有任何脚本中添加public void OnDrawGizmos ()这个方法,然后在方法中Gizmos.DrawLine调用这个方法可以在Scene视图中划线条或是其它图形

     2.在运行时Game视图,调用_LineRenderer.SetVertexCount(_Nav.path.corners.Length)这个方法也可以划线条

二.有关GUI相关知识

    1.在任何脚本中添加这个private void OnGUI()方法,在这个方法中可以调用GUI相关的组件,可以在Game视图看到相关的组件,  例如

      GUIStyle style = new GUIStyle

        {

            border = new RectOffset(10, 10, 10, 10),

            fontSize = 50,

            fontStyle = FontStyle.BoldAndItalic,

        };

        // normal:Rendering settings for when the component is displayed normally.

        style.normal.textColor = new Color(200/255f, 180/255f, 150/255f);    // 需要除以255,因为范围是0-1

        GUI.Label(new Rect(100, 100, 200, 80), "aaa", style);

        GUI.Label(new Rect(Screen.width - 100, Screen.height - 100, 200, 80),

            "<color=#00ff00><size=30>"+"aaa"+"</size></color>", style);

   2.继承Editor类,添加标签([CustomEditor(typeof(TileMousePosition))],[ExecuteInEditMode]),在类中重        写 public override void OnInspectorGUI() 方法可以自定义insprector中的组件:test.mRectValue = EditorGUILayout.RectField("窗口坐标", test.mRectValue),方法 void OnSceneGUI()可以在Scene视图中绘制GUI组件:

         Handles.BeginGUI();

        GUIStyle style = new GUIStyle(); 

        style.normal.textColor = Color.red; 

        //规定GUI显示区域

        GUILayout.BeginArea(new Rect(0, 0, 500, 100));

        //GUI绘制文本框

        GUILayout.Label("世界坐标:" + "X: " + worldPosition.x + " Y: " + -worldPosition.y*2 + " Z: " + worldPosition.z,style);  

        GUILayout.Label("格子坐标:" + "X: " + cellPosition.x + " Y: " + -cellPosition.y + " Z: " + cellPosition.z,style);   

        GUILayout.EndArea();

    

        Handles.EndGUI();

猜你喜欢

转载自blog.csdn.net/u011105442/article/details/102943764