GameManager

🤔


这片文章主要介绍之前的项目中使用过的各类管理器


组建单例模版类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
/// <summary>
/// 组建单例模版
/// </summary>
public class AD_MonoSingleten<T> : MonoBehaviour where T : MonoBehaviour{
private static T instance;
public static T Instance{
get{
if (instance == null){
GameObject go = new GameObject(typeof(T).Name);
instance = go.AddComponent<T>();
}
return instance;
}
set {
instance = value;
}
}

protected virtual void Awake(){
Instance = this as T;
}
}

预制体管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using UnityEngine;

/// <summary>
/// 文件名:AD_PrefabManager
/// 功能说明:预制体管理器
/// </summary>

public class AD_PrefabManager : AD_Singleton<AD_PrefabManager> {

/// <summary>
/// 实例化预制体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <returns></returns>
public GameObject Spawn(AD_PrefabType type, string name, Vector3 pos, Quaternion rotation, Transform parent)
{
string path = AD_ResourcesPath.GetPath(type, name);
GameObject obj = AD_ResourceFactory.Instance.Load<GameObject>(path);
if(obj != null)
{
obj = GameObject.Instantiate(obj) as GameObject;
obj.transform.SetParent(parent);
obj.transform.localPosition = pos;
obj.transform.localRotation = rotation;
}
else
{
Debug.LogError("perfab not find !");
}
return obj;
}


public GameObject Spawn(AD_PrefabType type, string name)
{
return Spawn(type, name, Vector3.zero, Quaternion.identity, null);
}

public GameObject Spawn(AD_PrefabType type, string name, Transform parent)
{
return Spawn(type, name, Vector3.zero, Quaternion.identity, parent);
}
}

音效管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnityEngine;

/// <summary>
/// 文件名:AD_AudioManager
/// 功能说明:音效管理器
/// </summary>

[RequireComponent(typeof(AudioSource))]
public class AD_AudioManager : AD_MonoSingleton<AD_AudioManager> {


private AudioSource mAudioSource;

protected override void Awake()
{
base.Awake();
mAudioSource = GetComponent<AudioSource>();
// 跨场景不销毁
DontDestroyOnLoad(gameObject);
}

/// <summary>
/// 播放背景音乐
/// </summary>
/// <param name="audioName"></param>
public void PlayBGAudio(string audioName)
{
// 加载背景音乐
AudioClip bg = AD_ResourceFactory.Instance.Load<AudioClip>(AD_ResourcesPath.Audio_Environment + audioName);
// 设置背景音乐
mAudioSource.clip = bg;
// 循环播放
mAudioSource.loop = true;
// 播放
mAudioSource.Play();
}

/// <summary>
/// 播放指定音乐
/// </summary>
/// <param name="audioName"></param>
public void PlayAudio(AD_AudioType type, string audioName)
{
string path = AD_ResourcesPath.GetPath(type, audioName);
AudioClip clip = AD_ResourceFactory.Instance.Load<AudioClip>(path);
AudioSource.PlayClipAtPoint(clip, transform.position);
}
}

场景加载管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 文件名:AD_Singleton
/// 功能说明:场景单例
/// </summary>
public class AD_Singleton<T> where T : new() {
/// <summary>
/// 场景单例
/// </summary>
private static readonly T instance = new T();
public static T Instance {
get{
return instance;
}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// 文件名:AD_ScenesManager
/// 功能说明:场景加载管理器
/// </summary>
public class AD_ScenesManager : AD_Singleton<AD_ScenesManager> {
private string sceneName; // 切换场景名
private AsyncOperation asyncOper;
private float progress; // 进度
public float Progress{
get{
return progress;
}
}
/// <summary>
/// 场景切换
/// </summary>
/// <param name="sceneName">Scene name.</param>
public void LoadScene(string sceneName){
this.sceneName = sceneName;
SceneManager.LoadScene (ScenesName.SceneLoading);
}
/// <summary>
/// 异步加载场景
/// </summary>
/// <returns>The scene async.</returns>
public IEnumerator LoadSceneAsync(){
asyncOper = SceneManager.LoadSceneAsync (sceneName);
//阻止场景自动跳转
AudioManager.Instance.PlayBG("Sound_SceneLoding");
asyncOper.allowSceneActivation = false;
//场景过度进度
while(asyncOper.progress < 0.9f){
progress += 0.005f;
yield return 0;
}
while (progress < 1) {
progress += 0.005f;
yield return 0;
}

AudioManager.Instance.PlayBG ("Sound_Background");
asyncOper.allowSceneActivation = true;

yield return 0;
}
}

有限状态机管理器对象池管理器 在之前有写过

宇 wechat
扫描二维码,订阅微信公众号