加载模型 音效

//加载模型

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum modetype
{
    goods,
    headcar,
    bodycar,
}
public class Model :MonoBehaviour
{
    [SerializeField] GameObject Platform;
    private static Model instance;
    public GameObject objModel;
  
    public static Model Instance()
    {
        return instance;
    }
 


    private void Awake()
    {
        instance = this;
        Platform = GameObject.Find("Showbox");
    }
    private GameObject InitSfxResources(string path,string name)
    {
        
        object[] g_array = Resources.LoadAll(path);

        foreach (Object obj in g_array)
        {
            if (typeof(GameObject) == obj.GetType())
            {
                if(obj.name == name)
                {
                    return obj as GameObject;
                }
            }
                
        }
        g_array.Clone();
        return null;

 

 

//通过名字加载

//path = Mode/bodycar/CarBodyImgPath1    

  string loadPath = path + "/" + name;
        Object g_array = Resources.Load(loadPath, typeof(GameObject));
        if(g_array != null)
        {          
            return g_array as GameObject;
        }
    }

    //拿取模型
    public void GetMode(string path,string name)
    {
       
        objModel = Instantiate(modelValue(path, name), new Vector3(0,6.3f,0), Quaternion.identity);
        objModel.transform.localScale = new Vector3(1,1,1);
        objModel.transform.SetParent(Platform.transform);
    }
    public GameObject modelValue(string path, string name)
    {      
        GameObject obj = InitSfxResources(path, name);
        if (obj == null)
        {
            Debug.Log("没有找到匹配模型");
            return null;
        }
        return obj;
    }
   
    public void ShowDestroy()
    {
        Destroy(objModel);
    }
}

Resources加载完毕并且返回以后及时清空,释放内存

 

//加载音效

 

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

public class SoundManager : MonoBehaviour
{

    private static SoundManager instance;

    public static SoundManager Instance
    {
        get
        {
            return instance;
        }
    }

    public AudioSource sourceVoice;// 对话

    // 音效池
    public List<AudioClip> voices;

    public float MusicVolume
    {
        get
        {
            return sourceMusic.volume;
        }
        set
        {
            sourceMusic.volume = value;
        }
    }

    public float EffectVolume
    {
        get
        {
            return sourceEffect.volume;
        }
        set
        {
            sourceEffect.volume = value;
            sourceVoice.volume = value;
        }
    }

    // Use this for initialization
    void Awake()
    {
        if (null != instance)
        {
            Destroy(instance);
        }
        instance = this;
        DontDestroyOnLoad(instance);

        // 音量调节
  
        sourceVoice.volume = 1;

        InitSfxResources();
    }

    private void InitSfxResources()
    {
       

        string voicePath = "Sound/Voice/";

   

        // 对话
        Object[] m_array3 = Resources.LoadAll(voicePath);
        foreach (Object obj in m_array3)
        {
            if (typeof(AudioClip) != obj.GetType())
                continue;
            AudioClip sound = obj as AudioClip;
            voices.Add(sound);
        }
    }

 

 

    // 获得语音
    public AudioClip GetVoice(string name)
    {
        foreach (AudioClip obj in voices)
        {
            if (!obj.name.Equals(name))
                continue;
            return obj;
        }
        return null;
    }

 

    // 播放语音
    public void PlayVoice(string name)
    {
        AudioClip sound = GetVoice(name);
        if (sound == null)
        {
            Debug.LogError(name + "该音乐找不到");
            return;
        }
        sourceVoice.clip = sound;
        sourceVoice.Play();
    }
}
此种加载利用了对象池,会占用内存

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值