【问题标题】:change radar image based on button click根据按钮单击更改雷达图像
【发布时间】:2017-11-15 11:32:22
【问题描述】:

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

雷达扫描脚本

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 脚本

public class MakeRadarObject : MonoBehaviour {

public Image image;

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

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

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您没有将图像应用到您的游戏对象,而只是应用到一个名为 UpdateImage 的变量。您需要获取游戏对象的图像组件,然后将新图像分配给它。您还需要将 Image 更改为 Sprite 的形式才能正常工作。

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

    【讨论】:

    • 仍然不适用于该更改。我是否也必须将其他脚本更改为精灵?单击按钮时出现错误“NullReferenceException:对象引用未设置为对象的实例”。
    • 带有 RadarScan 脚本的游戏对象是带有您的图像的游戏对象吗?否则,您需要在具有图像的游戏对象上获取组件。
    • 我有一个 Enemy 对象,它具有使其出现在雷达上的雷达脚本和将图像更改为新图像的扫描雷达脚本?
    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 2015-05-09
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2012-07-07
    相关资源
    最近更新 更多