Exemple #1
0
        public void BuildNavigation(SelectableGroup group)
        {
            Vector3 position;
            Vector3 direction;
            Vector3 localDir;
            float   maxScore;

            foreach (SelectableOptionBase option in group.options)
            {
                Transform transform = option.GetTransform();

                for (int i = 0; i < option.navigationArray.Length; i++)
                {
                    maxScore = Mathf.NegativeInfinity;

                    direction = SelectableNavigationUtils.GetDirectionVector((SelectableNavigationDirection)i);
                    localDir  = Quaternion.Inverse(transform.rotation) * direction;
                    position  = transform.TransformPoint(SelectableNavigationUtils.GetPointOnRectEdge(transform as RectTransform, localDir));

                    SelectableOptionBase bestPick = null;
                    foreach (SelectableOptionBase other in group.options)
                    {
                        SelectableOptionBase sel = other;

                        if (sel == option || sel == null)
                        {
                            continue;
                        }

                        var     selRect   = sel.GetTransform() as RectTransform;
                        Vector3 selCenter = selRect != null ? (Vector3)selRect.rect.center : Vector3.zero;
                        Vector3 myVector  = sel.GetTransform().TransformPoint(selCenter) - position;

                        // Value that is the distance out along the direction.
                        float dot = Vector3.Dot(direction, myVector);

                        // Skip elements that are in the wrong direction or which have zero or too much distance.
                        // This also ensures that the scoring formula below will not have a division by zero error.
                        if (dot <= 0)
                        {
                            continue;
                        }

                        // This scoring function has two priorities:
                        // - Score higher for positions that are closer.
                        // - Score higher for positions that are located in the right direction.
                        // This scoring function combines both of these criteria.
                        float score = dot / myVector.sqrMagnitude;

                        if (score > maxScore)
                        {
                            maxScore = score;
                            bestPick = sel;
                        }
                    }

                    option.navigationArray[i] = bestPick;
                }
            }
        }
Exemple #2
0
        public void BuildNavigation(SelectableGroup group)
        {
            List <SelectableOptionBase> sortedOptions;

            if (axis == SORTING_AXIS.X)
            {
                sortedOptions = SelectableNavigationUtils.SortByXPosFirst(group.options);
            }
            else if (axis == SORTING_AXIS.Y)
            {
                sortedOptions = SelectableNavigationUtils.SortByYPosFirst(group.options);
            }
            else
            {
                //Use as a fallback if no axis is set
                sortedOptions = group.options;
            }

            SelectableNavigationUtils.BuildNavigationFromSortedList(sortedOptions, group.navigationType);
        }
Exemple #3
0
 public void BuildNavigation(SelectableGroup group)
 {
     SelectableNavigationUtils.BuildNavigationFromSortedList(group.options, group.navigationType);
 }
Exemple #4
0
        private static void BuildSmartNavigation(List <SelectableOptionBase> options, float[] maxSearchDistances)
        {
            List <BoxCollider2D> cachedColliders    = new List <BoxCollider2D>();
            List <RectTransform> cachedTransforms   = new List <RectTransform>();
            List <int>           cachedLayerIndices = new List <int>();

            //Attach BoxCollider2Ds to every option and set to to default layer to allow Raycasts to work
            foreach (SelectableOptionBase option in options)
            {
                BoxCollider2D collider            = option.gameObject.AddComponent <BoxCollider2D>();
                RectTransform optionRectTransform = option.GetComponent <RectTransform>();

                collider.size = optionRectTransform.sizeDelta;
                cachedColliders.Add(collider);
                cachedTransforms.Add(optionRectTransform);
                cachedLayerIndices.Add(option.gameObject.layer);

                option.gameObject.layer = 0;
            }

            //Raycast from each control border to a direction for the given distance for the direction
            for (int i = 0; i < options.Count; i++)
            {
                for (int j = 0; j < maxSearchDistances.Length; j++)
                {
                    SelectableNavigationDirection direction = (SelectableNavigationDirection)j;
                    RectTransform optionRectTransform       = cachedTransforms[i];

                    RaycastHit2D hit = Physics2D.Raycast(GetRaycastOrigin(direction, optionRectTransform), SelectableNavigationUtils.GetDirectionVector(direction), maxSearchDistances[j]);

                    if (hit.transform != null)
                    {
                        SelectableOptionBase hitOption = hit.transform.GetComponent <SelectableOptionBase>();
                        if (hitOption != null)
                        {
                            options[i].SetNextOption(direction, hitOption);
                        }
                    }
                }
            }

            //Destroy the previously created BoxColliders and restore previous layer
            for (int i = 0; i < options.Count; i++)
            {
                if (Application.isPlaying)
                {
                    Object.Destroy(cachedColliders[i]);
                }
                else
                {
                    Object.DestroyImmediate(cachedColliders[i]);
                }
                options[i].gameObject.layer = cachedLayerIndices[i];
            }
        }
Exemple #5
0
 public void BuildNavigation(SelectableGroup group)
 {
     BuildSmartNavigation(SelectableNavigationUtils.SortByYPosFirst(group.options), maxSearchDistances);
 }