Example #1
0
    /// <summary>
    ///
    /// </summary>
    void EditorGUI()
    {
        if (!Application.isPlaying && Target != null && executeInEditMode)
        {
            Vector3 targetPoint = TargetPosition;
            Plane   plane       = new Plane(Transform.forward, Transform.position);
            float   size        = 4;
            //If the object is behind the camera, then don't draw it
            if (plane.GetSide(targetPoint) == false)
            {
                return;
            }

            //Calculate the 2D position of the position where the icon should be drawn
            Vector3 viewportPoint = CameraComponent.WorldToViewportPoint(targetPoint);

            //The viewportPoint coordinates are between 0 and 1, so we have to convert them into screen space here
            Vector2 drawPosition = new Vector2(viewportPoint.x * Screen.width, Screen.height * (1 - viewportPoint.y));

            float clampBorder = 12;

            //Clamp the position to the edge of the screen in case the icon would be drawn outside the screen
            drawPosition.x = Mathf.Clamp(drawPosition.x, clampBorder, Screen.width - clampBorder);
            drawPosition.y = Mathf.Clamp(drawPosition.y, clampBorder, Screen.height - clampBorder);

            GUI.color = Color.yellow;
            GUI.DrawTexture(new Rect(drawPosition.x - size * 0.5f, drawPosition.y - size * 0.5f, size, size), Texture2D.whiteTexture, ScaleMode.StretchToFill);
            GUI.color = Color.white;
        }
    }