Need help. I try to rewrite script from JavaScript to C#. I run my project but crosshair does not shows.
There is the JavaScript:
@script ExecuteInEditMode()
var crosshairPreset : preset = preset.none;
var showCrosshair : boolean = true;
var verticalTexture : Texture;
var horizontalTexture : Texture;
//Size of boxes
var cLength : float = 10;
var cWidth : float = 3;
//Spreed setup
var minSpread : float = 45.0;
var maxSpread : float = 250.0;
var spreadPerSecond : float = 150.0;
private var temp : Texture;
private var spread : float;
function Update(){
//Used just for test (weapon script should change spread).
if(Input.GetButton("Fire1")) spread += spreadPerSecond * Time.deltaTime;
else spread -= spreadPerSecond * 2 * Time.deltaTime;
}
function OnGUI(){
if(showCrosshair && verticalTexture && horizontalTexture){
var verticalT = GUIStyle();
var horizontalT = GUIStyle();
verticalT.normal.background = verticalTexture;
horizontalT.normal.background = horizontalTexture;
spread = Mathf.Clamp(spread, minSpread, maxSpread);
//Horizontal
GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
//Vertical
GUI.Box(Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
}
}
There is the C#:
using UnityEngine;
using System.Collections;
public class Proba : MonoBehaviour {
bool showCrosshair = true;
public Texture2D verticalTexture;
public Texture2D horizontalTexture;
public float cLength = 10;
public float cWidth = 3;
//Spreed setup
public float minSpread = 45.0f;
public float maxSpread = 250.0f;
public float spreadPerSecond = 150.0f;
private Texture temp;
private float spread;
public GUIStyle verticalT;
public GUIStyle horizontalT;
// Update is called once per frame
void Update () {
//Used just for test (weapon script should change spread).
if(Input.GetButton("Fire1")) spread += spreadPerSecond * Time.deltaTime;
else spread -= spreadPerSecond * 2 * Time.deltaTime;
}
void OnGUI(){
if(showCrosshair && verticalTexture && horizontalTexture){
//var verticalT = GUIStyle();
//var horizontalT = GUIStyle();
verticalT.normal.background = verticalTexture;
horizontalT.normal.background = horizontalTexture;
spread = Mathf.Clamp(spread, minSpread, maxSpread);
//Horizontal
GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
//Vertical
GUI.Box( new Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
GUI.Box( new Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
}
}
}