ReviewImg用于显示图片,所挂脚本为ImageName
using UnityEngine;
using System.Collections;
public class ImageName : MonoBehaviour
{
public string _imageName;
}
GroupBtn是切换图片的Button,GroupBtn所挂脚本为ImagesChanged
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ImagesChanged : MonoBehaviour
{ //四个实现点击按钮进行图片切换的Button
public GameObject RollBtn;
public GameObject ScaraBtn;
public GameObject DeltaBtn;
public GameObject PaletBtn;
private object imgStr;
public Image iconImage;
void Start ()
{
EventTriggerListener.Get(RollBtn).onClick += changeImages;
EventTriggerListener.Get(ScaraBtn).onClick += changeImages;
EventTriggerListener.Get(DeltaBtn).onClick += changeImages;
EventTriggerListener.Get(PaletBtn).onClick += changeImages;
}
//点击按钮实现图片的切换
void changeImages(GameObject btn)
{
string imageName;
if (btn.GetComponent<ImageName>()._imageName != null)
{
//获取ImageName中的_imageName变量
imageName = btn.GetComponent<ImageName>()._imageName;
//获取icon文件夹下的图片精灵
iconImage.GetComponent<Image>().sprite = Resources.Load("icon/" + imageName, typeof(Sprite)) as Sprite;
}
}
//#region 点击空格键实现图片的切换
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeImageName("6G");
}
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeImageName("002");
}
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeImageName("003");
}
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeImageName("004");
}
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeImageName("005");
}
}
void ChangeImageName(string name)
{
// 图片需放在Resources/icon(所放图片的文件夹)
//获取图片的精灵
Sprite imgstr = Resources.Load("/icon" + name) as Sprite;
iconImage.sprite = imgstr;
}
#endregion
}
注:
各个切换图片按钮都要依次绑定ImageName脚本,用于获取对应的图片,
在Inspector中ImageName要依次绑定对应的图片,例如:名字为002的图片。