Created
December 14, 2017 13:56
-
-
Save MollerVictor/94ce3edc0ed56457b0506ad3d790a7d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class CrosshairType2 : MonoBehaviour | |
{ | |
[SerializeField] List<Image> _crosshairParts; | |
[SerializeField] List<Image> _crosshairOutlineParts; | |
[SerializeField] Image _crosshairDot; | |
[SerializeField] Image _crosshairDotOutline; | |
[SerializeField] protected int _crosshairLength = 20; | |
[SerializeField] protected int _crosshairGap = 8; | |
[SerializeField] protected int _crosshairThickness = 1; | |
[SerializeField] protected int _crosshairOutlineThickness = 1; | |
[SerializeField] protected Color _crosshairColor = Color.white; | |
[SerializeField] protected Color _crosshairOutlineColor = Color.black; | |
[SerializeField] protected int _dotSize = 1; | |
[SerializeField] protected int _dotOutlineThickness = 1; | |
[SerializeField] protected Color _dotColor = Color.white; | |
[SerializeField] protected Color _dotOutlineColor = Color.black; | |
[SerializeField] protected bool _renderCrosshairRight = true; | |
[SerializeField] protected bool _renderCrosshairLeft = true; | |
[SerializeField] protected bool _renderCrosshairTop = true; | |
[SerializeField] protected bool _renderCrosshairBottom = true; | |
// Use this for initialization | |
protected void Awake () | |
{ | |
UpdateCrosshair(); | |
} | |
protected void UpdateCrosshair() | |
{ | |
_crosshairParts[0].gameObject.SetActive(_renderCrosshairRight); | |
_crosshairParts[1].gameObject.SetActive(_renderCrosshairLeft); | |
_crosshairParts[2].gameObject.SetActive(_renderCrosshairTop); | |
_crosshairParts[3].gameObject.SetActive(_renderCrosshairBottom); | |
_crosshairOutlineParts[0].gameObject.SetActive(_renderCrosshairRight); | |
_crosshairOutlineParts[1].gameObject.SetActive(_renderCrosshairLeft); | |
_crosshairOutlineParts[2].gameObject.SetActive(_renderCrosshairTop); | |
_crosshairOutlineParts[3].gameObject.SetActive(_renderCrosshairBottom); | |
_crosshairDot.color = _dotColor; | |
_crosshairDotOutline.color = _dotOutlineColor; | |
_crosshairDot.rectTransform.sizeDelta = new Vector2(_dotSize * 2, _dotSize * 2); | |
_crosshairDotOutline.rectTransform.sizeDelta = new Vector2((_dotSize + _dotOutlineThickness) * 2, (_dotSize + _dotOutlineThickness) * 2); | |
OutLineParts(); | |
FillParts(); | |
} | |
void OutLineParts() | |
{ | |
Texture2D texture = new Texture2D(200, 100, TextureFormat.ARGB32, false, true); | |
texture.filterMode = FilterMode.Point; | |
Color outlineColor = _crosshairOutlineColor; | |
Color32[] newColors = Enumerable.Repeat(new Color32(0, 0, 0, 0), texture.width * texture.height).ToArray(); | |
texture.SetPixels32(newColors); | |
for (int x = -_crosshairOutlineThickness; x < _crosshairLength + _crosshairOutlineThickness; x++) | |
{ | |
for (int i = 0; i < _crosshairThickness + _crosshairOutlineThickness; i++) | |
{ | |
int renderX = texture.width - 1 - _crosshairGap - x; | |
int renderY1 = 49 - i; | |
int renderY2 = 50 + i; | |
if (renderX >= 0 && renderX < texture.width && renderY1 >= 0 && renderY1 < texture.height) | |
texture.SetPixel(renderX, renderY1, outlineColor); | |
if (renderX >= 0 && renderX < texture.width && renderY1 >= 0 && renderY2 < texture.height) | |
texture.SetPixel(renderX, renderY2, outlineColor); | |
} | |
} | |
texture.Apply(); | |
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); | |
foreach (var crosshairPart in _crosshairOutlineParts) | |
{ | |
crosshairPart.sprite = sprite; | |
} | |
} | |
void FillParts() | |
{ | |
Texture2D texture = new Texture2D(200, 100, TextureFormat.ARGB32, false, true); | |
texture.filterMode = FilterMode.Point; | |
Color color = _crosshairColor; | |
Color32[] newColors = Enumerable.Repeat(new Color32(0, 0, 0, 0), texture.width * texture.height).ToArray(); | |
texture.SetPixels32(newColors); | |
for (int x = 0; x < _crosshairLength; x++) | |
{ | |
for (int i = 0; i < _crosshairThickness; i++) | |
{ | |
int renderX = texture.width - 1 - _crosshairGap - x; | |
int renderY1 = 49 - i; | |
int renderY2 = 50 + i; | |
if (renderX >= 0 && renderX < texture.width && renderY1 >= 0 && renderY1 < texture.height) | |
texture.SetPixel(renderX, renderY1, color); | |
if (renderX >= 0 && renderX < texture.width && renderY1 >= 0 && renderY2 < texture.height) | |
texture.SetPixel(renderX, renderY2, color); | |
} | |
} | |
texture.Apply(); | |
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); | |
foreach (var crosshairPart in _crosshairParts) | |
{ | |
crosshairPart.sprite = sprite; | |
} | |
} | |
//These are for showing spread | |
void Grow() | |
{ | |
_crosshairParts[0].transform.position += Vector3.right; | |
_crosshairParts[1].transform.position -= Vector3.right; | |
_crosshairParts[2].transform.position += Vector3.up; | |
_crosshairParts[3].transform.position -= Vector3.up; | |
} | |
void UnGrow() | |
{ | |
_crosshairParts[0].transform.position -= Vector3.right; | |
_crosshairParts[1].transform.position += Vector3.right; | |
_crosshairParts[2].transform.position -= Vector3.up; | |
_crosshairParts[3].transform.position += Vector3.up; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment