12using System.Collections;
13using System.Collections.Generic;
19 public class Anchor : MonoBehaviour {
21 private static HashSet<Anchor> _allAnchors;
24 if (_allAnchors ==
null) {
25 _allAnchors =
new HashSet<Anchor>();
31 [Tooltip(
"Should this anchor allow multiple objects to be attached to it at the same time? "
32 +
"This property is enforced by AnchorGroups and AnchorableBehaviours.")]
35 [Tooltip(
"Should this anchor attempt to enable and disable the GameObjects of attached "
36 +
"AnchorableBehaviours when its own active state changes? If this setting is enabled, "
37 +
"the Anchor will deactivate the attached objects when its own GameObject is deactivated "
38 +
"or if its script is disabled, and similarly for becoming active or enabled.")]
41 private HashSet<AnchorGroup> _groups =
new HashSet<AnchorGroup>();
42 public HashSet<AnchorGroup>
groups {
get {
return _groups; } }
44 private HashSet<AnchorableBehaviour> _preferringAnchorables =
new HashSet<AnchorableBehaviour>();
46 private HashSet<AnchorableBehaviour> _anchoredObjects =
new HashSet<AnchorableBehaviour>();
50 public HashSet<AnchorableBehaviour>
anchoredObjects {
get {
return _anchoredObjects; } }
52 public bool isPreferred {
get {
return _preferringAnchorables.Count > 0; } }
97 foreach (var anchObj
in _anchoredObjects) {
98 anchObj.gameObject.SetActive(
true);
108 updateAnchorCallbacks();
111 void OnAnchorDisabled() {
113 foreach (var anchObj
in _anchoredObjects) {
114 anchObj.gameObject.SetActive(
false);
120 foreach (var group
in groups) {
127 #region Anchor Callbacks
130 _anchoredObjects.Add(anchObj);
132 if (_anchoredObjects.Count == 1) {
138 _anchoredObjects.Remove(anchObj);
140 if (_anchoredObjects.Count == 0) {
145 private void updateAnchorCallbacks() {
151 _preferringAnchorables.Add(anchObj);
153 if (_preferringAnchorables.Count == 1) {
159 _preferringAnchorables.Remove(anchObj);
161 if (_preferringAnchorables.Count == 0) {
172 void OnDrawGizmosSelected() {
173 Matrix4x4 origMatrix = Gizmos.matrix;
174 Gizmos.matrix = this.transform.localToWorldMatrix;
176 float radius = 0.015F;
178 drawWireSphereGizmo(Vector3.zero, radius);
180 drawSphereCirclesGizmo(5, Vector3.zero, radius, Vector3.forward);
182 Gizmos.matrix = origMatrix;
185 private static Vector3[] worldDirs =
new Vector3[] { Vector3.right, Vector3.up, Vector3.forward };
187 private void drawWireSphereGizmo(Vector3 pos,
float radius) {
188 foreach (var dir
in worldDirs) {
189 if (dir == Vector3.forward)
continue;
190 Utils.DrawCircle(pos, dir, radius,
AnchorGizmoColor, quality: 24, depthTest:
true);
194 private void drawSphereCirclesGizmo(
int numCircles, Vector3 pos,
float radius, Vector3 poleDir) {
195 float dTheta = 180F / numCircles;
196 float halfTheta = dTheta / 2F;
198 for (
int i = 0; i < numCircles; i++) {
199 float curTheta = (dTheta * i) + halfTheta;
200 Utils.DrawCircle(pos + poleDir * Mathf.Cos(curTheta * Mathf.Deg2Rad) * radius, poleDir, Mathf.Sin(curTheta * Mathf.Deg2Rad) * radius,
AnchorGizmoColor, quality: 16, depthTest:
true);
206 #region Unity Events (Internal)
209 private EnumEventTable _eventTable =
null;
220 private void initUnityEvents() {
229 private void setupCallback(ref Action action,
EventType type) {
230 action += () => _eventTable.Invoke((
int)type);
void NotifyAttached(AnchorableBehaviour anchObj)
HashSet< AnchorableBehaviour > anchoredObjects
Gets the set of AnchorableBehaviours currently attached to this anchor.
HashSet< AnchorGroup > groups
Action OnNoAnchorablesAttached
Called when there are no anchorables attached to this anchor.
static Color AnchorGizmoColor
Action OnAnchorNotPreferred
Called when no anchorable objects prefer this anchor any more.
Action OnAnchorablesAttached
Called as soon as any anchorables become attached to this anchor.
Action WhileAnchorPreferred
Called every Update() that an AnchorableBehaviour prefers this anchor.
Action OnAnchorPreferred
Called as soon as any anchorable objects prefer this anchor if they were to try to attach to an ancho...
bool matchActiveStateWithAttachedObjects
static HashSet< Anchor > allAnchors
void NotifyAnchorPreference(AnchorableBehaviour anchObj)
bool allowMultipleObjects
void NotifyEndAnchorPreference(AnchorableBehaviour anchObj)
void NotifyDetached(AnchorableBehaviour anchObj)
Action WhileAnchorablesAttached
Called every Update() that one or more AnchorableBehaviours is attached to this anchor.
AnchorableBehaviours mix well with InteractionBehaviours you'd like to be able to pick up and place i...