Unity (using GUI to make first-person mouse standard star)

Attributes

Introduction

method

Note: The crosshair is generally placed in the center of the screen, so the screen's ( width/height/2 ) can get the center point
screen width Screen.Width
screen height Screen.Height
rectangle width Variables declared by yourself are replaced by W here
rectangle height Variables declared by yourself are replaced by H here
front sight distance Self-declared variables are replaced by D here

explanatory diagram


write process

Because we want a dynamic crosshair, we plan to write one by ourselves instead of directly pasting it with pictures.

  1. First declare the width and height of the crosshair, and the spacing, followed by the crosshair map
  2. Declare two more variables GUIStyle (used to customize the GUI parameters) and Texture (the auxiliary parameters of the front sight background, which can also be understood as making up the number)
  3. Instantiate GUIStyle at the beginning of the game and assign the crosshair map to its normal.background property
  4. Use the GUI.Box method to draw four rectangles in the center of the screen to draw a crosshair
  5. Box (Rect, Image, GUIStyle) (Introduction to GUIStyle in Unity Documentation )
    1. Rect here means to create a rectangular frame ( coordinate x, coordinate y, rectangle width, rectangle height )
    2. Image (there is no substantive role in this method, but if you want to use this method, you must fill in Image, so create one but not assign it to make up the number )
    3. GUIStyle (the setting of our front sight is in this parameter, if we do not assign a value to this parameter, we will use the GuiStyle setting that comes with Unity)
  6. Here I declare the static crosshair. If it is dynamic, it is enough to change the coordinates, spacing and width and height during a certain action.

example

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FPS_CrossHair : MonoBehaviour
{
    [Header("准星的长度")]
    public float width;
    [Header("准星的高度")]
    public float height;
    [Header("上下(左右)两条准星之间的距离")]
    public float distance;
    [Header("准星背景图")]
    public Texture2D crosshairTexture;

    private GUIStyle lineStyle;     //  GUI自定义参数
    private Texture tex;            //  准星背景辅助参数

    private void Start()
    {
        lineStyle = new GUIStyle();                         //  游戏开始实例化背景图
        lineStyle.normal.background = crosshairTexture;     //  将背景图默认背景设为准星背景
    }

    private void OnGUI()
    {
        //  左准星
        GUI.Box(new Rect(Screen.width / 2 - distance / 2 - width, Screen.height / 2 - height / 2, width, height), tex, lineStyle);
        //  右准星
        GUI.Box(new Rect(Screen.width / 2 + distance / 2 , Screen.height / 2 - height / 2, width, height), tex, lineStyle);
        //  上准星
        GUI.Box(new Rect(Screen.width / 2 - height / 2, Screen.height / 2 - distance / 2 - width, height, width), tex, lineStyle);
        //  下准星
        GUI.Box(new Rect(Screen.width / 2 - height / 2, Screen.height / 2 + distance / 2, height, width), tex,lineStyle);
    }

}

Guess you like

Origin blog.csdn.net/qq_24977805/article/details/123681090