Beispiel #1
0
        /// <summary>
        /// Gets the nearest object.
        /// </summary>
        /// <returns>The nearest object.</returns>
        /// <param name="_objects">Objects.</param>
        /// <param name="_position">Position.</param>
        /// <param name="_distance">Distance.</param>
        public static GameObject GetNearestObject(GameObject[] _objects, Vector3 _position, float _distance)
        {
            if (_objects == null || _objects.Length == 0)
            {
                return(null);
            }

            if (_distance == 0)
            {
                _distance = Mathf.Infinity;
            }

            GameObject _best_object   = null;
            float      _best_distance = _distance;

            for (int i = 0; i < _objects.Length; i++)
            {
                GameObject _object = _objects[i];

                if (_object != null && _object.activeInHierarchy && _object.transform.position != _position)
                {
                    float _tmp_distance = PositionTools.Distance(_position, _object.transform.position);

                    if (_tmp_distance < _best_distance)
                    {
                        _best_distance = _tmp_distance;
                        _best_object   = _object;
                    }
                }
            }

            return(_best_object);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the nearest object by name, position and distance.
        /// </summary>
        /// <returns>The nearest object by name.</returns>
        /// <param name="_name">Name.</param>
        /// <param name="_position">Position.</param>
        /// <param name="_distance">Distance.</param>
        public static GameObject GetNearestObjectByName(string _name, Vector3 _position, float _distance)
        {
            if (_name.Trim() == "")
            {
                return(null);
            }

            if (_distance == 0)
            {
                _distance = Mathf.Infinity;
            }

            GameObject _best_object   = null;
            float      _best_distance = _distance;

            GameObject[] _objects = GameObject.FindObjectsOfType <GameObject>();

            foreach (GameObject _tmp_object in _objects)
            {
                if (_tmp_object != null && _tmp_object.name == _name && _tmp_object.transform.position != _position && _tmp_object.activeInHierarchy)
                {
                    float _tmp_distance = PositionTools.Distance(_position, _tmp_object.transform.position);

                    if (_tmp_distance < _best_distance)
                    {
                        _best_distance = _tmp_distance;
                        _best_object   = _tmp_object;
                    }
                }
            }

            return(_best_object);
        }
        public static void ZickZackCircle(Vector3 center, float radius, string _text, bool _show_text)
        {
            float step = GetCircleDegrees(radius);



            Vector3 _text_pos = Vector3.zero;

            bool _draw = false;

            for (float _angle = 0; _angle <= 360; _angle += step)
            {
                //	_angle += space;
                float a1 = _angle * Mathf.PI / 180f;
                float a2 = (_angle + step) * Mathf.PI / 180f;

                Vector3 _inner_pos_1 = center + new Vector3(Mathf.Sin(a1) * radius, 0, Mathf.Cos(a1) * radius);
                Vector3 _inner_pos_2 = center + new Vector3(Mathf.Sin(a2) * radius, 0, Mathf.Cos(a2) * radius);
                float   lenght       = PositionTools.Distance(_inner_pos_1, _inner_pos_2);

                Vector3 _outer_pos_1 = center + new Vector3(Mathf.Sin(a1) * (radius - lenght), 0, Mathf.Cos(a1) * (radius - lenght));
                Vector3 _outer_pos_2 = center + new Vector3(Mathf.Sin(a2) * (radius - lenght), 0, Mathf.Cos(a2) * (radius - lenght));

                Vector3 _outer_pos_3 = Vector3.Lerp(_outer_pos_1, _outer_pos_2, 0.5f);


                Gizmos.DrawLine(_inner_pos_1, _outer_pos_3);
                Gizmos.DrawLine(_outer_pos_3, _inner_pos_2);

                if (_text_pos == Vector3.zero)
                {
                    _text_pos = _inner_pos_1;
                }

                if (_draw)
                {
                    _draw = false;
                }
                else
                {
                    _draw = true;
                }
            }

            if (_show_text && _text != "")
            {
                Color _color = Gizmos.color;
                _color.a = 255;
                Text(GUI.skin, _text, _text_pos, _color, 12, -50, 200);
                //Gizmos.color = _org_color;
            }
        }
        public static Vector3 GetPathSection(Vector3 pos_1, Vector3 pos_2, float lenght, bool inverse)
        {
            float f        = 1;
            float distance = PositionTools.Distance(pos_1, pos_2);

            if (inverse)
            {
                lenght = distance - lenght;
            }

            if (lenght < distance)
            {
                f = lenght / (distance / 100) / 100;
            }

            return(Vector3.Lerp(pos_1, pos_2, f));
        }
        public static void Text(string _text, Vector3 _world_position, Color _color, float _font_size = 14, FontStyle _font_style = FontStyle.Normal)
        {
                        #if UNITY_EDITOR
            UnityEditor.Handles.BeginGUI();

            UnityEditor.SceneView view = UnityEditor.SceneView.currentDrawingSceneView;
            Vector3 _screen_position   = view.camera.WorldToScreenPoint(_world_position);
            Vector2 _size = GUI.skin.label.CalcSize(new GUIContent(_text));

            GUIStyle _style = new GUIStyle();
            _style.normal.textColor = _color;
            float _distance   = (int)PositionTools.Distance(view.camera.transform.position, _world_position);
            float _normalized = 1 - MathTools.Normalize(_distance, 0, 250);
            if (_normalized > 0.1f && _normalized < 1)
            {
                _style.fontSize  = (int)(_font_size * _normalized);
                _style.fontStyle = _font_style;
                GUI.Label(new Rect(_screen_position.x - (_size.x / 2), -_screen_position.y + view.position.height + 4, _size.x, _size.y), _text, _style);
            }
            UnityEditor.Handles.EndGUI();
                        #endif
        }