您的位置:首页 > 产品设计 > UI/UE

怪物攻击角色时,UI上箭头执行怪物的方向

2016-08-15 16:11 106 查看
下面是插件的原码:

using UnityEngine;

using System.Collections;

public class ArrowSet : MonoBehaviour

{

    public Texture arrow;

    private Camera cam;

    private float xCenter;

    private float yCenter;

    private float screenSlope;

    private float halfSize;

    public float size = 30;

    private bool errorless = false;

    public string tagToFind = "Enemy";

    public float hoverAngle = 270;

    public float distanceAbove = 30;

    public float blindSpot = 5.0f;

    public bool hoverOnScreen = true;

    void Start()

    {

        Camera theCam = gameObject.GetComponent<Camera>();

        if (gameObject.GetComponent<Camera>())

        {

            cam = theCam;

            if (arrow != null)

            {

                errorless = true;

            }

            else

            {

                Debug.Log("请附上一个箭头纹理雷达箭脚本。");

                errorless = false;

            }

        }

        else

        {

            Debug.Log("雷达箭头必须连接到一个摄像机。");

        }

    }

    void OnGUI()

    {

        if (Event.current.type.Equals(EventType.Repaint) && errorless)

        {

            xCenter = cam.pixelWidth / 2;

            yCenter = cam.pixelHeight / 2;

            screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;

            halfSize = size / 2;

            GameObject[] objects = GameObject.FindGameObjectsWithTag(tagToFind);

            foreach (GameObject ob in objects)

            {

                float angle = hoverAngle - 180;

                double rad = angle * (Mathf.PI / 180.0);

                Vector3 arrowPos = cam.transform.right * Mathf.Cos((float)rad) + cam.transform.up * Mathf.Sin((float)rad);

                Vector3 worldPos = ob.transform.position + (arrowPos * distanceAbove);

                //世界转视口 

                Vector3 pos = cam.WorldToViewportPoint(worldPos);

                //Debug.Log(pos);

                if (pos.z < 0)

                {

                    pos.x *= -1;

                    pos.y *= -1;

                }

                if (pos.z > 0 || (pos.z < 0 && (pos.x > 0.5f + (blindSpot / 2) || pos.x < 0.5f - (blindSpot / 2)) && (pos.y < 0.5f - (blindSpot / 2) || pos.y > 0.5f + (blindSpot / 2))))

                {

                    //找到新的位置

                    float newX = pos.x * cam.pixelWidth;

                    //相机坐标垂直翻转相比,像素坐标

                    float newY = cam.pixelHeight - pos.y * cam.pixelHeight;

                    ////如果对象是屏幕外

                    if (pos.z < 0 || newY < 0 || newY > cam.pixelHeight || newX < 0 || newX > cam.pixelWidth)

                    {

                        float a = CalculateAngle(cam.pixelWidth / 2, cam.pixelHeight / 2, newX, newY);

                        Vector2 coord = ProjectToEdge(newX, newY);

                        GUIUtility.RotateAroundPivot(a, coord);

                        Graphics.DrawTexture(new Rect(coord.x - halfSize, coord.y - halfSize, size, size), arrow);

                        GUIUtility.RotateAroundPivot(-a, coord);

                    }

                    else

                        if (hoverOnScreen)

                        {

                            Debug.Log(222);

                            float nh = Mathf.Sin((float)rad) * size;

                            float nw = Mathf.Cos((float)rad) * size;

                            //当在屏幕上,只需旋转90度,并绘制

                            GUIUtility.RotateAroundPivot(-angle + 180, new Vector2(newX + nw, newY - nh));

                            Graphics.DrawTexture(new Rect(newX + nw, newY - nh - halfSize, size, size), arrow, null);

                            GUIUtility.RotateAroundPivot(angle - 180, new Vector2(newX + nw, newY - nh));

                        }

                }

            }

        }

    }

    float CalculateAn
9c66
gle(float x1, float y1, float x2, float y2)

    {

        float xDiff = x2 - x1;

        float yDiff = y2 - y1;

        float rad = Mathf.Atan(yDiff / xDiff);

        float deg = rad * 180 / Mathf.PI;

        if (xDiff < 0)

        {

            deg += 180;

        }

        return deg;

    }

    Vector2 ProjectToEdge(float x2, float y2)

    {

        float xDiff = x2 - (cam.pixelWidth / 2);

        float yDiff = y2 - (cam.pixelHeight / 2);

        float slope = yDiff / xDiff;

        Vector2 coord = new Vector2(0, 0);

        float ratio;

        if (slope > screenSlope || slope < -screenSlope)

        {

            //project on top/bottom

            ratio = (yCenter - halfSize) / yDiff;

            if (yDiff < 0)

            {

                coord.y = halfSize;

                ratio *= -1;

            }

            else coord.y = cam.pixelHeight - halfSize;

            coord.x = xCenter + xDiff * ratio;

        }

        else

        {

            //project on left/right

            ratio = (xCenter - halfSize) / xDiff;

            if (xDiff < 0)

            {

                coord.x = halfSize;

                ratio *= -1;

            }

            else coord.x = cam.pixelWidth - halfSize;

            coord.y = yCenter + yDiff * ratio;

        }

        return coord;

    }
}

以前代码在 Unity4.6运行良好,但在Untiy5以后的版本就有BUG,上下方向的怪物好使,左右的不显示UI,经过查找发现Camera.pixelWidth和Camera.pixelHeight不再是float值,而是int类型,我们之需要将它们转成flaot就可以修复这个BUG 

screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: