[Unity] 다양한 이미지를 붙여넣는 방법

Cube 또는 Image 또는 이미지의 스크립트로 붙여 넣는 방법



거의 자신 용입니다.

이번에는 여러 가지 고려하여 이미지 링크에서 이미지를 붙여 갑니다.
PC 내의 png 파일 등에서 직접 읽는 경우는 아래의 기사를 참고해 보세요.
htps : // m / r-gtm / ms / 6c f25643 a 1 a 6 to 82 a 6c


Cube(3D 오브젝트)



아래 이미지를 붙이고 싶은 객체에 첨부하는 스크립트

Test_Cube.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Cube : MonoBehaviour
{
    //画像リンクから画像をテクスチャにする
    Texture texture;
    //テクスチャをマテリアル化するので生成しておく
    [SerializeField]Material material;
    //画像リンク
    string url = "https://touhoucannonball.com/assets/img/character/img_008.jpg";
    void Start()
    {
        //先にマテリアルのシェーダを変更しておく
        string shader = "Legacy Shaders/Diffuse";
        material.shader = Shader.Find(shader);
        StartCoroutine(Connect());
}

    //テクスチャを読み込む
    private IEnumerator Connect()
    {
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);

        yield return www.SendWebRequest();

        if (www.isNetworkError ||www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            //textureに画像が入るよ
            texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
            //textureをマテリアルにセット
            material.SetTexture("_MainTex", texture);

            gameObject.GetComponent<Renderer>().material = material;
        }
    }
}


이런 느낌으로 할 수 있을까 생각합니다.
Cube뿐만 아니라 Sphere, Capsule, Cylineder, Plane, Quad도 가능합니다.


Image (아이에게 Image가 있는 것도)



Sprite를 사용하여 이미지를 붙여넣을 수 있습니다.
아래 이미지를 붙이고 싶은 객체에 첨부하는 스크립트

Test_Image.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Test_Image : MonoBehaviour
{
    //imageはスプライトを使って描画しているので
    Sprite sprite;
    //画像リンクから画像をテクスチャにする
    Texture2D texture;
    //画像リンク
    string url = "https://touhoucannonball.com/assets/img/character/img_008.jpg";

    void Start()
    {
        StartCoroutine(Connect());
    }

    //テクスチャを読み込む
    private IEnumerator Connect()
    {
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);

        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            //textureに画像格納
            texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
            //textureからspriteに変換
            sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), Vector2.zero);
            //Imageにspriteを張り付ける
            gameObject.GetComponent<Image>().sprite = sprite;
        }
    }
}




할 수 있었습니다. GUI의 녀석은 기본 이것으로 괜찮다고 생각합니다.

RawImage



여기에서는 Texture를 사용하여 이미지를 붙여넣습니다.
용도에 따라 구분합시다.

Test_RawImage.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Test_RawImage : MonoBehaviour
{
    //画像リンクから画像をテクスチャにする
    Texture texture;
    //画像リンク
    string url = "https://touhoucannonball.com/assets/img/character/img_008.jpg";

    void Start()
    {
        StartCoroutine(Connect());
    }

    //テクスチャを読み込む
    private IEnumerator Connect()
    {
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);

        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            //textureに画像格納
            texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
            //そのまま貼り付け
            gameObject.GetComponent<RawImage>().texture = texture;
        }
    }
}




보기는 변하지 않습니다.



3D 객체는 Material
이미지는 스프라이트
RawImage는 Texture
를 변경하여 이미지를 변경합니다.
그 밖에도 여러가지 방법 있다고 생각합니다만 우선 이것으로 표시는 할 수 있다고 생각합니다.

좋은 웹페이지 즐겨찾기