unity简单资源管理
Posted 左右...
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity简单资源管理相关的知识,希望对你有一定的参考价值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
//资源管理器
public class SFxResMananger : MonoBehaviour
private static SFxResMananger _instance;
public static SFxResMananger Instance
get
return _instance;
private Dictionary<string, GameObject> resDictionary = new Dictionary<string, GameObject>();
private GameObject panelLevel;
private GameObject windowLevel;
private GameObject alertLevel;
public float screenWRate;
public float screenHRate;
private void Awake()
_instance = this;
// Start is called before the first frame update
void Start()
//构建ui结构
GameObject UI = new GameObject("UI");
Canvas canvas = UI.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
UI.AddComponent<CanvasScaler>();
UI.AddComponent<GraphicRaycaster>();
panelLevel = new GameObject("panelLevel");
panelLevel.transform.SetParent(UI.transform);
panelLevel.transform.localPosition = Vector3.zero;
windowLevel = new GameObject("windowLevel");
windowLevel.transform.SetParent(UI.transform);
windowLevel.transform.localPosition = Vector3.zero;
alertLevel = new GameObject("alertLevel");
alertLevel.transform.SetParent(UI.transform);
alertLevel.transform.localPosition = Vector3.zero;
//计算适配
float resScreenW = 1920;
float resScreenH = 1080;
float curScreenW = Screen.width;
float curScreenH = Screen.height;
screenWRate = curScreenW / resScreenW;
screenHRate = curScreenH / resScreenH;
public GameObject ShowPanel(string path)
return LoadRes(path, panelLevel.transform);
public GameObject ShowWindow(string path)
return LoadRes(path, windowLevel.transform);
public GameObject ShowAlert(string path)
return LoadRes(path, alertLevel.transform);
public GameObject ShowItem(string path,Transform parent)
return LoadRes(path, parent);
private GameObject LoadRes(string path, Transform parent)
if (resDictionary.ContainsKey(path))
GameObject go = Instantiate(resDictionary[path]);
go.transform.SetParent(parent);
go.transform.localScale = new Vector3(screenWRate, screenHRate, 1);
go.transform.localPosition = Vector3.zero;
return go;
else
GameObject res = Resources.Load(path) as GameObject;
resDictionary.Add(path, res);
return LoadRes(path, parent);
以上是关于unity简单资源管理的主要内容,如果未能解决你的问题,请参考以下文章