如何 get/change Image Source name with code in Unity?
How to get/change Image Source name with code in Unity?
我们做了一个脚本,需要获取图片名,下面用比较笨拙的方式实现了这个任务,但还是想知道有没有直接获取图片名的方法?
public class CubeManager
{
static public Dictionary<CubeType, string> cubeTypeDict = new Dictionary<CubeType, string>();
static private Dictionary<string, GameObject> cubeDict = new Dictionary<string, GameObject>();
static CubeManager()
{
cubeTypeDict.Add(CubeType.Wall01, "Wall01");
cubeTypeDict.Add(CubeType.Wall02, "Wall02");
cubeTypeDict.Add(CubeType.Wall03, "Wall03");
cubeTypeDict.Add(CubeType.Wall04, "Wall04");
cubeTypeDict.Add(CubeType.Wall05, "Wall05");
cubeTypeDict.Add(CubeType.Wall06, "Wall06");
cubeTypeDict.Add(CubeType.Wall07, "Wall07");
cubeTypeDict.Add(CubeType.Wall08, "Wall08");
cubeTypeDict.Add(CubeType.Wall09, "Wall09");
cubeTypeDict.Add(CubeType.Wall10, "Wall10");
cubeTypeDict.Add(CubeType.Wall11, "Wall11");
cubeTypeDict.Add(CubeType.Wall12, "Wall12");
cubeTypeDict.Add(CubeType.Wall13, "Wall13");
cubeTypeDict.Add(CubeType.Wall14, "Wall14");
cubeTypeDict.Add(CubeType.Wall15, "Wall15");
cubeTypeDict.Add(CubeType.Wall16, "Wall16");
cubeTypeDict.Add(CubeType.Wall17, "Wall17");
cubeTypeDict.Add(CubeType.Wall18, "Wall18");
cubeTypeDict.Add(CubeType.Wall19, "Wall19");
cubeTypeDict.Add(CubeType.Wall20, "Wall20");
cubeTypeDict.Add(CubeType.Wall21, "Wall21");
cubeTypeDict.Add(CubeType.Wall22, "Wall22");
cubeTypeDict.Add(CubeType.Wall23, "Wall23");
cubeTypeDict.Add(CubeType.Wall24, "Wall24");
cubeTypeDict.Add(CubeType.Wall25, "Wall25");
cubeTypeDict.Add(CubeType.Wall26, "Wall26");
cubeTypeDict.Add(CubeType.Wall27, "Wall27");
cubeTypeDict.Add(CubeType.Wall28, "Wall28");
cubeTypeDict.Add(CubeType.Wall29, "Wall29");
}
是的,你可以,
Image img;
Debug.Log("Image Name: " + img.sprite.name);
此外,您还可以通过给定 Texture2D
来更改精灵
img.sprite = Sprite.Create (spr.texture, img.sprite.rect, new Vector2 (0.5f, 0.5f));
这里spr
是一个精灵,可以直接使用Texture2D
。
this.transform.gameobject.GetComponent<Image> ().name;
我们做了一个脚本,需要获取图片名,下面用比较笨拙的方式实现了这个任务,但还是想知道有没有直接获取图片名的方法?
public class CubeManager
{
static public Dictionary<CubeType, string> cubeTypeDict = new Dictionary<CubeType, string>();
static private Dictionary<string, GameObject> cubeDict = new Dictionary<string, GameObject>();
static CubeManager()
{
cubeTypeDict.Add(CubeType.Wall01, "Wall01");
cubeTypeDict.Add(CubeType.Wall02, "Wall02");
cubeTypeDict.Add(CubeType.Wall03, "Wall03");
cubeTypeDict.Add(CubeType.Wall04, "Wall04");
cubeTypeDict.Add(CubeType.Wall05, "Wall05");
cubeTypeDict.Add(CubeType.Wall06, "Wall06");
cubeTypeDict.Add(CubeType.Wall07, "Wall07");
cubeTypeDict.Add(CubeType.Wall08, "Wall08");
cubeTypeDict.Add(CubeType.Wall09, "Wall09");
cubeTypeDict.Add(CubeType.Wall10, "Wall10");
cubeTypeDict.Add(CubeType.Wall11, "Wall11");
cubeTypeDict.Add(CubeType.Wall12, "Wall12");
cubeTypeDict.Add(CubeType.Wall13, "Wall13");
cubeTypeDict.Add(CubeType.Wall14, "Wall14");
cubeTypeDict.Add(CubeType.Wall15, "Wall15");
cubeTypeDict.Add(CubeType.Wall16, "Wall16");
cubeTypeDict.Add(CubeType.Wall17, "Wall17");
cubeTypeDict.Add(CubeType.Wall18, "Wall18");
cubeTypeDict.Add(CubeType.Wall19, "Wall19");
cubeTypeDict.Add(CubeType.Wall20, "Wall20");
cubeTypeDict.Add(CubeType.Wall21, "Wall21");
cubeTypeDict.Add(CubeType.Wall22, "Wall22");
cubeTypeDict.Add(CubeType.Wall23, "Wall23");
cubeTypeDict.Add(CubeType.Wall24, "Wall24");
cubeTypeDict.Add(CubeType.Wall25, "Wall25");
cubeTypeDict.Add(CubeType.Wall26, "Wall26");
cubeTypeDict.Add(CubeType.Wall27, "Wall27");
cubeTypeDict.Add(CubeType.Wall28, "Wall28");
cubeTypeDict.Add(CubeType.Wall29, "Wall29");
}
是的,你可以,
Image img;
Debug.Log("Image Name: " + img.sprite.name);
此外,您还可以通过给定 Texture2D
img.sprite = Sprite.Create (spr.texture, img.sprite.rect, new Vector2 (0.5f, 0.5f));
这里spr
是一个精灵,可以直接使用Texture2D
。
this.transform.gameobject.GetComponent<Image> ().name;