Skip to content

Commit 502885a

Browse files
committed
Added Crosshair v1
1 parent e5f4f45 commit 502885a

17 files changed

Lines changed: 562 additions & 2 deletions
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
///Author: seyahdoo(Seyyid Ahmed Doğan)
2+
///github: github.com/seyahdoo
3+
///email : contact@seyahdoo.com
4+
///
5+
///see my other reusable unity codes at
6+
///https://github.com/seyahdoo/Unity-Code-Repo
7+
8+
///Changelog:
9+
///30.03.2017 -> Created by seyahdoo
10+
11+
///usage:
12+
///Attach to any camera or pointer and this will trigger CrosshairAware objects
13+
14+
///Dependancies:
15+
///None
16+
17+
18+
using UnityEngine;
19+
20+
namespace seyahdoo.crosshair
21+
{
22+
23+
/// <summary>
24+
/// Attach this to a camera, and it will do its stuff
25+
/// </summary>
26+
public class Crosshair : MonoBehaviour
27+
{
28+
29+
30+
/// <summary>
31+
/// Call this if you are not sure crosshair is not setted up
32+
/// </summary>
33+
public static void SetupCrosshair(Camera camera)
34+
{
35+
36+
//Maybe setup a grosshair image? (i just found this typo... and i wont fix it :D)
37+
SetupCrosshair(camera.gameObject);
38+
39+
}
40+
41+
/// <summary>
42+
/// Call this if you are not sure crosshair is not setted up
43+
/// </summary>
44+
public static void SetupCrosshair(GameObject go)
45+
{
46+
go.AddComponent<Crosshair>();
47+
}
48+
49+
/// <summary>
50+
/// Call this if you are not sure crosshair is not setted up
51+
/// </summary>
52+
public static void SetupCrosshair()
53+
{
54+
foreach (Camera cam in Camera.allCameras)
55+
{
56+
SetupCrosshair(cam);
57+
}
58+
59+
}
60+
61+
private CrosshairAware _crosshairAware;
62+
private Collider _lastCollider;
63+
64+
void Update()
65+
{
66+
//do the raycasting
67+
RaycastHit hit;
68+
69+
//if i found something
70+
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
71+
{
72+
//did i found a new thing?
73+
if(_lastCollider != hit.collider)
74+
{
75+
//i should focus to that
76+
focusToCollider(hit.collider);
77+
}
78+
}
79+
else
80+
{
81+
//im not hitting anything, i should focus to nothing
82+
focusToCollider(null);
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Change current focus
88+
/// </summary>
89+
/// <param name="collider">collider?</param>
90+
void focusToCollider(Collider collider)
91+
{
92+
//if i was focused to anything im not focused anymore
93+
if (_crosshairAware)
94+
{
95+
///Dont bug me about its being obsolete! i maid it!
96+
#pragma warning disable 612, 618
97+
_crosshairAware.setFocus(false);
98+
#pragma warning restore 612, 618
99+
100+
_crosshairAware = null;
101+
}
102+
103+
//im not hitting anything
104+
_lastCollider = collider;
105+
106+
//if im not looking to nothingness
107+
if (collider)
108+
{
109+
//im checking if this collider is Crosshair Aware?
110+
_crosshairAware = collider.gameObject.GetComponent<CrosshairAware>();
111+
112+
//if so, im focused to that
113+
if (_crosshairAware)
114+
{
115+
///Dont bug me about its being obsolete! i maid it!
116+
#pragma warning disable 612, 618
117+
_crosshairAware.setFocus(true);
118+
#pragma warning restore 612, 618
119+
}
120+
}
121+
122+
}
123+
124+
}
125+
126+
127+
128+
}

NewAPI/seyahdoo/crosshair/Crosshair.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
///Author: seyahdoo(Seyyid Ahmed Doğan)
2+
///github: github.com/seyahdoo
3+
///email : contact@seyahdoo.com
4+
///
5+
///see my other reusable unity codes at
6+
///https://github.com/seyahdoo/Unity-Code-Repo
7+
8+
///Changelog:
9+
///30.03.2017 -> Created by seyahdoo
10+
11+
///usage:
12+
///Attach to any object and then you can watch HasFocus property or subscribe to FocusOnEvent and FocusOffEvent
13+
14+
///Dependancies:
15+
///seyahdoo.crosshair.Crosshair
16+
17+
using UnityEngine;
18+
19+
namespace seyahdoo.crosshair
20+
{
21+
/// <summary>
22+
/// Just add this to object you wanted to act according to crosshair.
23+
/// Then you can watch HasFocus property.
24+
/// Or you can subscribe to FocusOnEvent and FocusOffEvent.
25+
/// You can also derive a class from this and use as such.
26+
/// </summary>
27+
[RequireComponent (typeof (Collider))]
28+
public class CrosshairAware : MonoBehaviour
29+
{
30+
31+
private bool _hasFocus = false;
32+
33+
/// <summary>
34+
/// Is crosshair looking to me?
35+
/// </summary>
36+
public bool HasFocus
37+
{
38+
get
39+
{
40+
return _hasFocus;
41+
}
42+
}
43+
44+
public delegate void VoidDelegate();
45+
46+
/// <summary>
47+
/// object just get focused event
48+
/// Usage: crosshairAware.FocusOnEvent += MethodToSubscribe;
49+
/// </summary>
50+
public event VoidDelegate FocusOnEvent;
51+
52+
/// <summary>
53+
/// Override me
54+
/// </summary>
55+
virtual protected void FocusOn() { }
56+
57+
/// <summary>
58+
/// object just get unfocused event
59+
/// Usage: crosshairAware.FocusOffEvent += MethodToSubscribe;
60+
/// </summary>
61+
public event VoidDelegate FocusOffEvent;
62+
63+
/// <summary>
64+
/// Override me
65+
/// </summary>
66+
virtual protected void FocusOff() { }
67+
68+
[System.Obsolete("Do not use setFocus method. It's internal. It is not ment to be used publicly")]
69+
internal void setFocus(bool value)
70+
{
71+
//Nothing to change? Cool.
72+
if (_hasFocus == value) return;
73+
74+
//update focus
75+
_hasFocus = value;
76+
77+
//trigger events
78+
if (value)
79+
{
80+
//internal event
81+
FocusOn();
82+
83+
//external events
84+
if (FocusOnEvent != null)
85+
FocusOnEvent();
86+
}else
87+
{
88+
//internal event
89+
FocusOff();
90+
91+
//external events
92+
if (FocusOffEvent != null)
93+
FocusOffEvent();
94+
}
95+
96+
}
97+
98+
}
99+
100+
101+
}

NewAPI/seyahdoo/crosshair/CrosshairAware.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NewAPI/seyahdoo/crosshair/examples.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace seyahdoo.crosshair.examples
6+
{
7+
8+
/// <summary>
9+
/// Crosshair example cube script, will go blue if stared by crosshair
10+
/// </summary>
11+
[RequireComponent (typeof (CrosshairAware))]
12+
[RequireComponent (typeof (MeshRenderer))]
13+
public class ColorChangingCube : MonoBehaviour
14+
{
15+
16+
private CrosshairAware ca;
17+
private Material m;
18+
19+
void Awake()
20+
{
21+
22+
m = GetComponent<MeshRenderer>().material;
23+
ca = GetComponent<CrosshairAware>();
24+
25+
ca.FocusOnEvent += ColorBlue;
26+
ca.FocusOffEvent += ColorRed;
27+
}
28+
29+
void ColorBlue()
30+
{
31+
m.color = Color.blue;
32+
}
33+
34+
void ColorRed()
35+
{
36+
m.color = Color.red;
37+
}
38+
39+
}
40+
41+
42+
}
43+
44+
45+

NewAPI/seyahdoo/crosshair/examples/ColorChangingCube.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using seyahdoo.crosshair;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
namespace seyahdoo.crosshair.examples
7+
{
8+
/// <summary>
9+
/// Simple Crosshair Aware cube, will go green if stared by Crosshair
10+
/// </summary>
11+
[RequireComponent (typeof (MeshRenderer))]
12+
public class ColorCubeDerived : CrosshairAware
13+
{
14+
15+
Material m;
16+
17+
void Awake()
18+
{
19+
m = GetComponent<MeshRenderer>().material;
20+
}
21+
22+
protected override void FocusOn()
23+
{
24+
m.color = Color.green;
25+
}
26+
protected override void FocusOff()
27+
{
28+
m.color = Color.red;
29+
}
30+
31+
32+
}
33+
34+
35+
36+
}

NewAPI/seyahdoo/crosshair/examples/ColorCubeDerived.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NewAPI/seyahdoo/crosshair/icons.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)