//加载模型
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();
}
}
此种加载利用了对象池,会占用内存