-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFocusableObject.cs
More file actions
147 lines (127 loc) · 3.94 KB
/
FocusableObject.cs
File metadata and controls
147 lines (127 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class FocusableObject : MonoBehaviour
{
[Header("Focus Settings")]
[SerializeField] private Transform cameraTargetTransform;
[Tooltip("Drag and drop the transform where you want the camera to go")]
[SerializeField] private Transform lookAtTarget;
[Tooltip("Optional: Transform for camera to look at while focused. Leave empty for free rotation.")]
[Header("Visual Feedback")]
[SerializeField] private Color hoverColor = Color.yellow;
[SerializeField] private bool enableHover = true;
private bool isHovered = false;
private Material[] originalMaterials;
private Renderer[] renderers;
void Start()
{
SetupMaterials();
}
private void SetupMaterials()
{
renderers = GetComponentsInChildren<Renderer>();
originalMaterials = new Material[renderers.Length];
for (int i = 0; i < renderers.Length; i++)
{
originalMaterials[i] = renderers[i].material;
}
}
void OnMouseEnter()
{
if (enableHover && !isHovered)
{
RTSCameraController cameraController = FindObjectOfType<RTSCameraController>();
if (cameraController != null && !cameraController.IsTransitioning)
{
SetHovered(true);
}
}
}
void OnMouseExit()
{
if (isHovered)
{
SetHovered(false);
}
}
void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
RTSCameraController cameraController = FindObjectOfType<RTSCameraController>();
if (cameraController != null && !cameraController.IsTransitioning)
{
cameraController.FocusOnObject(this);
}
}
}
private void SetHovered(bool hovered)
{
isHovered = hovered;
for (int i = 0; i < renderers.Length; i++)
{
if (hovered)
{
Material hoverMat = new Material(originalMaterials[i]);
hoverMat.color = hoverColor;
renderers[i].material = hoverMat;
}
else
{
renderers[i].material = originalMaterials[i];
}
}
}
public Vector3 GetFocusPosition()
{
return transform.position;
}
public Transform GetCameraTargetTransform()
{
// First try the manually assigned transform
if (cameraTargetTransform != null)
{
return cameraTargetTransform;
}
// If no transform assigned, find child named "Focus"
Transform focusTransform = FindChildByName("Focus");
if (focusTransform != null)
{
return focusTransform;
}
Debug.LogWarning("No camera target transform found on " + name + ". Assign one manually or create a child named 'Focus'");
return null;
}
private Transform FindChildByName(string childName)
{
// Search through all children recursively to find the "Focus" transform
return SearchChildren(transform, childName);
}
private Transform SearchChildren(Transform parent, string name)
{
// Check direct children first
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (child.name == name)
{
return child;
}
}
// Then search grandchildren recursively
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
Transform result = SearchChildren(child, name);
if (result != null)
{
return result;
}
}
return null;
}
public Transform GetLookAtTarget()
{
return lookAtTarget;
}
}