Unity给 曲面游戏物体 换图片


在这里插入图片描述

需求

  一个曲面模型,它显示的图案是模型上材质球的主贴图,我们要给它换成需要的图。

原理

  改变模型上材质球的主贴图

一、手动换

  找到目标材质球(图一),对材质球的主贴图做修改(图二,给红框处拖入一张新的图片)
在这里插入图片描述
在这里插入图片描述

二、用代码换

  ChangeMainTextureOfMaterial.cs脚本放在模型“123”上,如红箭头指示,把模型和目标图片拖给脚本
在这里插入图片描述

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

public class ChangeMainTextureOfMaterial : MonoBehaviour
{
    public GameObject model_123;//名字叫“123”的曲面模型
    public Texture targetTexture;//目标图片

    void Start()
    {
        ChangeMaterial_MainTexture();
    }

    /// <summary>
    /// 换材质球的主贴图
    /// </summary>
    public void ChangeMaterial_MainTexture()
    {
        //获取材质球 
        Material[] mats = model_123.GetComponent<MeshRenderer>().materials; //(复杂模型上可能有多个材质球,因而获取的是一个数组)

        //换材质球的主贴图
        for(int i=0;i<mats.Length;i++)
        {
            mats[i].mainTexture = targetTexture;
        }
    }
}
三、用代码换 存在于服务器上的图
第一步,下载图片

在这里插入图片描述

(如上图)下载图片脚本,放在了游戏物体“123”上
注意此时下载的是Texture2D格式,需强转成Texture格式——把它赋值给我们定义的Texture类型变量“targetTexture”(代码贴在了文章下方处)

请添加图片描述

第二步,给材质球换主贴图

在这里插入图片描述

(如上图)给材质球换图片的脚本,放在了游戏物体“Script_SetTexture”上(此游戏物体事先是隐藏的,它作为变量出现在“123”的脚本里,目的是让图片从服务器下载完后,再执行给材质球换图片的操作,且此脚本做了略微改动)
两个脚本如下

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

public class DownloadTextureFromServer_quMian : MonoBehaviour
{
    public static DownloadTextureFromServer_quMian instance;
    public Texture targetTexture;
    public GameObject setTexture;
    //public RawImage myRawImage;
    void Start()
    {
        instance = this;
        //异步加载
        StartCoroutine(DownloadImage());
    }


    void Update()
    {

    }
    IEnumerator DownloadImage()
    {

        UnityEngine.Networking.UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://47.96.106.49/filetest/test1.jpg");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            targetTexture = (Texture)(((DownloadHandlerTexture)www.downloadHandler).texture);
            setTexture.SetActive(true);
            //myRawImage.texture = targetTexture;

            //Sprite createSprite = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0, 0));
            //gameObject.GetComponent<Image>().sprite = createSprite;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeMainTextureOfMaterial : MonoBehaviour
{
    public GameObject model_123;//名字叫“123”的曲面模型
    private Texture targetTexture;//目标图片

    void Start()
    {
        targetTexture = DownloadTextureFromServer_quMian.instance.targetTexture;
        ChangeMaterial_MainTexture();
    }

    /// <summary>
    /// 换材质球的主贴图
    /// </summary>
    public void ChangeMaterial_MainTexture()
    {
        //获取材质球 
        Material[] mats = model_123.GetComponent<MeshRenderer>().materials; //(复杂模型上可能有多个材质球,因而获取的是一个数组)

        //换材质球的主贴图
        for(int i=0;i<mats.Length;i++)
        {
            mats[i].mainTexture = targetTexture;
        }
    }
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值