通过单击按钮更改雷达图像 [英] change radar image based on button click

查看:93
本文介绍了通过单击按钮更改雷达图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以提供一些帮助.我有一个显示物体的雷达.我正在尝试引入雷达扫描功能,以便在单击按钮时根据对象的标签更新雷达上的图像.我的代码没有错误,但是我无法使它正常工作,并希望这里的人可以发现问题所在.谢谢!!!!

I could do with some help. I have a radar which displays objects. I'm trying to introduce a radar scan feature so that when a button is clicked the image on the radar is updated based on the tag of the object. My code has no errors but I can't get it to work and was hoping someone here could spot what the problem is. Thanks!!!!

RadarScan脚本

RadarScan Script

public class RadarScan : MonoBehaviour {

public Image RadarImageToChange;

public void ChangeImage(Image UpdateImage)
{
    if(gameObject.tag == "Enemy")
    {
        UpdateImage = RadarImageToChange;
    }

}

雷达脚本

public class RadarObject
{
    public Image icon { get; set; }
    public GameObject owner { get; set; }
}

public class Radar : MonoBehaviour {

public Transform playerPos; //position of player
float mapScale = 0.1f; //scale radar size

public static List<RadarObject> radObjects = new List<RadarObject>();

//Registers Object to the radar
public static void RegisterRadarObject(GameObject o, Image i)
{
    Image image = Instantiate(i);
    radObjects.Add(new RadarObject() { owner = o, icon = image }); //adds to List
}

//It loops through the list looking for the owner existing in the list, when it finds the owner is detroys the icon
public static void RemoveRadarObject(GameObject o)
{
    //New list for destroyed objects
    List<RadarObject> newList = new List<RadarObject>();
    for (int i = 0; i < radObjects.Count; i++)
    {
        if (radObjects[i].owner == o)
        {
            Destroy(radObjects[i].icon);
            continue;
        }
         else
            newList.Add(radObjects[i]);
        }
    radObjects.RemoveRange(0, radObjects.Count);
    radObjects.AddRange(newList);
}


void DrawRadarDots()
{
    //loops through the list and for each Object it gets the owners transform position and determins the difference between it's
    //position and the players position, does calculations on the angle and distance and position on a circle using polar equations.
    foreach (RadarObject ro in radObjects)
    {
        Vector3 radarPos = (ro.owner.transform.position - playerPos.position);
        float distToObject = Vector3.Distance(playerPos.position, ro.owner.transform.position) * mapScale;
        float deltay = Mathf.Atan2(radarPos.x, radarPos.z) * Mathf.Rad2Deg - 270 - playerPos.eulerAngles.y;
        radarPos.x = distToObject * Mathf.Cos(deltay * Mathf.Deg2Rad) * -1;
        radarPos.z = distToObject * Mathf.Sin(deltay * Mathf.Deg2Rad);

        //grabs icon of players objects and make it a child of panel and set it's postion based on radarPos.x and radarPos.z
        ro.icon.transform.SetParent(this.transform);
        ro.icon.transform.position = new Vector3(radarPos.x, radarPos.z, 0) + this.transform.position;
    }
}

    //Update is called once per frame
    void Update ()
    {
        DrawRadarDots();
    }

}

MakeRadarObject脚本

MakeRadarObject Script

public class MakeRadarObject : MonoBehaviour {

public Image image;

// Use this for initialization
void Start () {
    Radar.RegisterRadarObject(this.gameObject, image);
}

void OnDestroy()
{
    Radar.RemoveRadarObject(this.gameObject); 
}

}

推荐答案

您没有将Image应用于游戏对象,仅应用于名为UpdateImage的变量.您需要获取游戏对象的图像组件,然后为其分配新图像.您还需要将Image更改为Sprite的形式,才能正常工作.

You aren't applying the Image to your gameobject, only to a variable named UpdateImage. You need to get the image component of your gameobject and then assign the new image to it. You will also need to change the Image to the form of a Sprite for this to work.

public Sprite RadarImageToChange;

public void ChangeImage(Sprite UpdateImage)
{
        if(gameObject.tag == "Enemy")
        {
            gameObject.GetComponent<Image>().sprite = RadarImageToChange;
        }
}

这篇关于通过单击按钮更改雷达图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆