Skip to content

Commit 8790f48

Browse files
committed
Add TriggerTrigger, Add Target.FocusStay()
1 parent 8ae295d commit 8790f48

11 files changed

Lines changed: 147 additions & 17 deletions

File tree

125 KB
Binary file not shown.

Testie/Assets/main.unity

1.87 KB
Binary file not shown.

Testie/Assets/seyahdoo/crosshair/Crosshair.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
///Attach to any camera or pointer and this will trigger CrosshairAware objects
1414

1515
///Dependancies:
16-
///None
16+
///seyahdoo.crosshair.Target (For it to be useful of course!?)
1717

1818

1919
using UnityEngine;
@@ -26,16 +26,30 @@ namespace seyahdoo.crosshair
2626
/// </summary>
2727
public class Crosshair : MonoBehaviour
2828
{
29-
29+
/// <summary>
30+
/// Am i setted up?
31+
/// </summary>
32+
private static bool IsSettedUp
33+
{
34+
get
35+
{
36+
if (GameObject.FindObjectOfType<Crosshair>())
37+
return true;
38+
else
39+
return false;
40+
}
41+
}
3042

3143
/// <summary>
3244
/// Call this if you are not sure crosshair is not setted up
3345
/// </summary>
3446
public static void SetupCrosshair(Camera camera)
3547
{
36-
37-
//Maybe setup a grosshair image? (i just found this typo... and i wont fix it :D)
38-
SetupCrosshair(camera.gameObject);
48+
//Maybe setup a grosshair image? <- (i just found this typo... and i wont fix it :D)
49+
if (!camera)
50+
Debug.LogError(typeof(Crosshair).Name + " -> SetupCrosshair(Camera) -> camera cant be null");
51+
else
52+
SetupCrosshair(camera.gameObject);
3953

4054
}
4155

@@ -44,22 +58,30 @@ public static void SetupCrosshair(Camera camera)
4458
/// </summary>
4559
public static void SetupCrosshair(GameObject go)
4660
{
47-
go.AddComponent<Crosshair>();
61+
if (!go)
62+
Debug.LogError(typeof(Crosshair).Name + " -> SetupCrosshair(GameObject) -> gameobject cant be null");
63+
else
64+
go.AddComponent<Crosshair>();
4865
}
4966

5067
/// <summary>
5168
/// Call this if you are not sure crosshair is not setted up
5269
/// </summary>
5370
public static void SetupCrosshair()
5471
{
55-
foreach (Camera cam in Camera.allCameras)
56-
{
57-
SetupCrosshair(cam);
58-
}
59-
72+
if (!FindObjectOfType<Crosshair>())
73+
SetupCrosshair(Camera.main);
74+
else
75+
Debug.Log("Crosshair is already setted up?");
6076
}
6177

78+
/// <summary>
79+
/// target that im focused
80+
/// </summary>
6281
private Target _target;
82+
/// <summary>
83+
/// collider that im focused
84+
/// </summary>
6385
private Collider _lastCollider;
6486

6587
/// <summary>
@@ -132,3 +154,5 @@ void focusToCollider(Collider collider)
132154

133155

134156
}
157+
158+

Testie/Assets/CrosshairRenderer.cs renamed to Testie/Assets/seyahdoo/crosshair/CrosshairRenderer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using UnityEngine;
44

55
/// <summary>
6-
/// sets Canvas's Camera for camera space resizing
6+
/// sets Canvas's Camera to MainCamera for camera space resizing
77
/// </summary>
88
public class CrosshairRenderer : MonoBehaviour {
99

@@ -14,8 +14,8 @@ void Awake()
1414
Canvas canvas = GetComponent<Canvas>();
1515
if (canvas)
1616
{
17-
canvas.worldCamera = Camera.allCameras[0];
18-
canvas.planeDistance = Camera.allCameras[0].nearClipPlane + .01f;
17+
canvas.worldCamera = Camera.main;
18+
canvas.planeDistance = Camera.main.nearClipPlane + .01f;
1919
}
2020

2121
}
File renamed without changes.

Testie/Assets/seyahdoo/crosshair/Target.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,28 @@ virtual protected void FocusOn() { }
6262
/// Usage: target.FocusOffEvent.AddListener(MethodName);
6363
/// </summary>
6464
public UnityEvent FocusOffEvent;
65-
65+
6666
/// <summary>
6767
/// Override me
6868
/// </summary>
6969
virtual protected void FocusOff() { }
7070

71+
/// <summary>
72+
/// object just get unfocused event
73+
/// Usage: target.FocusOffEvent.AddListener(MethodName);
74+
/// </summary>
75+
public UnityEvent FocusStayEvent;
76+
77+
/// <summary>
78+
/// Override me
79+
/// </summary>
80+
virtual protected void FocusStay() { }
81+
82+
83+
/// <summary>
84+
/// Sets the focus to a certain value. Written for Crosshair script's usage.
85+
/// </summary>
86+
/// <param name="value">true->focus is on me</param>
7187
public void setFocus(bool value)
7288
{
7389
//Nothing to change? Cool.
@@ -79,6 +95,8 @@ public void setFocus(bool value)
7995
//trigger events
8096
if (value)
8197
{
98+
justnotfocused = false; //jusfocused = true;
99+
82100
//internal event
83101
FocusOn();
84102

@@ -96,7 +114,33 @@ public void setFocus(bool value)
96114
}
97115

98116
}
99-
117+
118+
private bool justnotfocused = false;
119+
120+
/// <summary>
121+
/// Carefully override this, or else FocusStay events wont work!
122+
/// use base.Update(); as the first line of overriden function
123+
/// </summary>
124+
virtual protected void Update()
125+
{
126+
if (_hasFocus)
127+
{
128+
if (justnotfocused)
129+
{
130+
//internal event
131+
FocusStay();
132+
133+
//external events
134+
FocusStayEvent.Invoke();
135+
}
136+
else
137+
{
138+
justnotfocused = false;
139+
}
140+
141+
}
142+
}
143+
100144
}
101145

102146

Testie/Assets/seyahdoo/crosshair/examples/ColorCubeDerived.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ColorCubeDerived : Target
1313
{
1414

1515
Material m;
16-
16+
1717
void Awake()
1818
{
1919
m = GetComponent<MeshRenderer>().material;
@@ -28,6 +28,7 @@ protected override void FocusOff()
2828
m.color = Color.red;
2929
}
3030

31+
3132

3233
}
3334

Binary file not shown.

Testie/Assets/seyahdoo/triggertrigger.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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.Events;
5+
6+
namespace seyahdoo.triggertrigger
7+
{
8+
9+
10+
public class TriggerTrigger : MonoBehaviour
11+
{
12+
13+
public List<string> invokeTags;
14+
15+
public UnityEvent TriggerEnter;
16+
public UnityEvent TriggerStay;
17+
public UnityEvent TriggerExit;
18+
19+
20+
void OnTriggerEnter(Collider other)
21+
{
22+
if (invokeTags.Contains(other.tag))
23+
TriggerEnter.Invoke();
24+
}
25+
void OnTriggerStay(Collider other)
26+
{
27+
if (invokeTags.Contains(other.tag))
28+
TriggerStay.Invoke();
29+
}
30+
void OnTriggerExit(Collider other)
31+
{
32+
if (invokeTags.Contains(other.tag))
33+
TriggerExit.Invoke();
34+
}
35+
36+
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)