「Unity/Csharp/画像ロード」の版間の差分
提供: 初心者エンジニアの簡易メモ
細 (Admin がページ「Unity/CSharp/画像ロード」を「Unity/Csharp/画像ロード」に、リダイレクトを残さずに移動しました) |
(→slice画像ロード(SpriteRendererを使う方法)) |
||
(同じ利用者による、間の3版が非表示) | |||
行1: | 行1: | ||
− | ==画像ロード( | + | ==画像ロード(SpriteRendererを使う方法)== |
Sprite sp = Resources.Load<Sprite>("gazo"); | Sprite sp = Resources.Load<Sprite>("gazo"); | ||
GameObject gameObj = new GameObject(); | GameObject gameObj = new GameObject(); | ||
− | + | SpriteRenderer spriteRenderer = gameObj.AddComponent<SpriteRenderer>(); | |
− | + | spriteRenderer.sprite = sp; | |
gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | ||
+ | gameObj.transform.localScale = new Vector3 (83, 83, 0); | ||
gameObj.transform.name = "gazo"; | gameObj.transform.name = "gazo"; | ||
− | |||
− | ==slice画像ロード( | + | ==slice画像ロード(SpriteRendererを使う方法)== |
Sprite[] sprites = Resources.LoadAll<Sprite>("gazo"); | Sprite[] sprites = Resources.LoadAll<Sprite>("gazo"); | ||
Sprite sp = System.Array.Find<Sprite>( sprites, (sprite) => sprite.name.Equals("gazo_1")); | Sprite sp = System.Array.Find<Sprite>( sprites, (sprite) => sprite.name.Equals("gazo_1")); | ||
GameObject gameObj = new GameObject(); | GameObject gameObj = new GameObject(); | ||
− | + | SpriteRenderer spriteRenderer = gameObj.AddComponent<SpriteRenderer>(); | |
− | + | spriteRenderer.sprite = sp; | |
gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | ||
+ | gameObj.transform.localScale = new Vector3 (83, 83, 0); | ||
gameObj.transform.name = "gazo_1"; | gameObj.transform.name = "gazo_1"; | ||
− | |||
+ | sliceのやり方は以下確認 | ||
− | ==画像ロード( | + | [[Unity/Spliteアニメ]] [ショートカット] |
+ | |||
+ | ==画像ロード(Imageを使う方法)== | ||
Sprite sp = Resources.Load<Sprite>("gazo"); | Sprite sp = Resources.Load<Sprite>("gazo"); | ||
GameObject gameObj = new GameObject(); | GameObject gameObj = new GameObject(); | ||
− | + | Image image = gameObj.AddComponent<Image>(); | |
− | + | image.sprite = sp; | |
gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | ||
− | |||
gameObj.transform.name = "gazo"; | gameObj.transform.name = "gazo"; | ||
+ | gameObj.transform.localScale = new Vector3 (1f, 1f, 0); | ||
− | ==slice画像ロード( | + | ==slice画像ロード(Imageを使う方法)== |
Sprite[] sprites = Resources.LoadAll<Sprite>("gazo"); | Sprite[] sprites = Resources.LoadAll<Sprite>("gazo"); | ||
Sprite sp = System.Array.Find<Sprite>( sprites, (sprite) => sprite.name.Equals("gazo_1")); | Sprite sp = System.Array.Find<Sprite>( sprites, (sprite) => sprite.name.Equals("gazo_1")); | ||
GameObject gameObj = new GameObject(); | GameObject gameObj = new GameObject(); | ||
− | + | Image image = gameObj.AddComponent<Image>(); | |
− | + | image.sprite = sp; | |
gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | gameObj.transform.parent = FindObjectOfType<Canvas>().transform; | ||
− | |||
gameObj.transform.name = "gazo_1"; | gameObj.transform.name = "gazo_1"; | ||
+ | gameObj.transform.localScale = new Vector3 (1f, 1f, 0); | ||
+ | |||
+ | ==ImageのSourceImageをResourcesにある画像に切り替える== | ||
+ | Assets/Resourcesに画像を準備(例:neko/img1.png) | ||
+ | <pre> | ||
+ | Texture2D texture = Resources.Load("neko/img1") as Texture2D; | ||
+ | Image img = GameObject.Find("Canvas/Image1").GetComponent<Image>(); | ||
+ | img.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); | ||
+ | </pre> |
2021年5月9日 (日) 11:19時点における最新版
目次
画像ロード(SpriteRendererを使う方法)
Sprite sp = Resources.Load<Sprite>("gazo"); GameObject gameObj = new GameObject(); SpriteRenderer spriteRenderer = gameObj.AddComponent<SpriteRenderer>(); spriteRenderer.sprite = sp; gameObj.transform.parent = FindObjectOfType<Canvas>().transform; gameObj.transform.localScale = new Vector3 (83, 83, 0); gameObj.transform.name = "gazo";
slice画像ロード(SpriteRendererを使う方法)
Sprite[] sprites = Resources.LoadAll<Sprite>("gazo"); Sprite sp = System.Array.Find<Sprite>( sprites, (sprite) => sprite.name.Equals("gazo_1")); GameObject gameObj = new GameObject(); SpriteRenderer spriteRenderer = gameObj.AddComponent<SpriteRenderer>(); spriteRenderer.sprite = sp; gameObj.transform.parent = FindObjectOfType<Canvas>().transform; gameObj.transform.localScale = new Vector3 (83, 83, 0); gameObj.transform.name = "gazo_1";
sliceのやり方は以下確認
Unity/Spliteアニメ [ショートカット]
画像ロード(Imageを使う方法)
Sprite sp = Resources.Load<Sprite>("gazo"); GameObject gameObj = new GameObject(); Image image = gameObj.AddComponent<Image>(); image.sprite = sp; gameObj.transform.parent = FindObjectOfType<Canvas>().transform; gameObj.transform.name = "gazo"; gameObj.transform.localScale = new Vector3 (1f, 1f, 0);
slice画像ロード(Imageを使う方法)
Sprite[] sprites = Resources.LoadAll<Sprite>("gazo"); Sprite sp = System.Array.Find<Sprite>( sprites, (sprite) => sprite.name.Equals("gazo_1")); GameObject gameObj = new GameObject(); Image image = gameObj.AddComponent<Image>(); image.sprite = sp; gameObj.transform.parent = FindObjectOfType<Canvas>().transform; gameObj.transform.name = "gazo_1"; gameObj.transform.localScale = new Vector3 (1f, 1f, 0);
ImageのSourceImageをResourcesにある画像に切り替える
Assets/Resourcesに画像を準備(例:neko/img1.png)
Texture2D texture = Resources.Load("neko/img1") as Texture2D; Image img = GameObject.Find("Canvas/Image1").GetComponent<Image>(); img.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);