替换材质图片

在做粒子效果时,想替换材质的图片,但是材质没有获取图片名称的属性,只能将图片转换成纹理付给材质的mainTexture,,写了个脚本,每两秒换材质图片,一分钟后销毁粒子效果,如:


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

public class EffectForFestival : MonoBehaviour 
{
    public List<GameObject> mChildrens;

    float mDeltaTime = 0;
    float mTotalTime = 2;
    float mDeltaDestroyTime = 0;
    float mDestroyTime = 60;
    private GameObject balloonEffctMgr;

    void Start()
    {
        Object o = Resources.Load("GUI/Effect/BalloonEffectMgr");
        balloonEffctMgr = Instantiate(o) as GameObject;
    }

    //----------------------------------------------------------------------------------------------------------------------
    public void ChangeTexture(int num)
    {
        foreach (GameObject item in mChildrens)
        {
            if (num>18)
            {
                num = 1;
            }
            item.GetComponent<ParticleSystem>().renderer.material.mainTexture = (Texture2D)Resources.Load("Effect/image/" + change(num));//换材质
            num += 1;
        }
    }

    //----------------------------------------------------------------------------------------------------------------------
    string  change(int num)
    {
        string id = "";
        if (num<10)id = "0" + num;
        else if (num > 18)id = "01";
        else id = num.ToString();
        return id;
    }

    //----------------------------------------------------------------------------------------------------------------------
    void Update () 
    {
        mDeltaTime+=Time.deltaTime;
        mDeltaDestroyTime += Time.deltaTime;
        if (mDeltaDestroyTime>=mDestroyTime)
        {//3分钟销毁
            
            Destroy(this.gameObject);
        }
        if (mDeltaTime > mTotalTime)
        {//两秒换材质
            int number = UnityEngine.Random.Range(1, 18);
            ChangeTexture(number);
            mDeltaTime = 0;
        }
	}

    void OnDestroy()
    {
        if (balloonEffctMgr != null)
        {
            balloonEffctMgr.GetComponent<BalloonEffectMgr>().DestoryObject();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值