欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity3d ugui 实现image代码换图

程序员文章站 2022-03-20 10:51:37
核心脚本代码 Image IMGE = transform.Find("IMGE").GetComponent();Sprite sprite1 = Resources.Load("Textrues/yutujing", typeof(Sprite)) as Sprite;IMGE.s ......

核心脚本代码

image imge = transform.find("imge").getcomponent<image>();
sprite sprite1 = resources.load("textrues/yutujing", typeof(sprite)) as sprite;
imge.sprite = sprite1;

需要注意的是 resources.load 会读取assets中resources目录下的资源, resources目录需要手动创建
或者

public sprite[] sprites;

public sprite[] sprite1;
void update ()
    {

        if (a>=200)
        {
            int random1 = random.range(0,11);
            for (int i = 0; i < 11; i++)
            {
                if (random1==i)
            {
                image1 = rt.transform.getchild(0).transform.getchild(0).getcomponent<image>();

                image2 = rt.transform.getchild(0).transform.getchild(1).getcomponent<image>();

                image1.sprite=sprites[random1];

                image2.sprite=sprite1[random1];

            }
            }
            
            instantiate(rt,new vector3 ( random.range (-860,850), 1, random.range (-717,-500)), quaternion.euler(new vector3(0,0,0)));

            a=1;
            
        }else
        {
            a=a+1;
        }

        debug.log(a);

    }
}