public static GameObject LoadRes(Transform parent, string path)
    {
        if (CheckResInDic(path))
        {
            if (GetResInDic(path) != null)
            {
                return(GetResInDic(path));
            }
            else
            {
                LoadResDic.Remove(path);
            }
        }

        GameObject objLoad = null;

        //加载资源
        ResourceItem objUnit = ResourcesManager.Instance.loadImmediate(path, ResourceType.PREFAB);

        if (objUnit == null || objUnit.Asset == null)
        {
            Debug.LogError("load unit failed" + path);
            return(null);
        }
        objLoad = GameObject.Instantiate(objUnit.Asset) as GameObject;
        objLoad.transform.parent        = parent;
        objLoad.transform.localScale    = Vector3.one;
        objLoad.transform.localPosition = Vector3.zero;
        LoadResDic.Add(path, objLoad);
        return(objLoad);
    }
示例#2
0
    public static GameObject LoadRes(Transform parent, string path, bool cache = true)
    {
        if (cache == true && CheckResInDic(path))
        {
            if (GetResInDic(path) != null)
            {
                return(GetResInDic(path));
            }
            else
            {
                LoadResDic.Remove(path);
            }
        }

        GameObject objLoad = null;

        GameObject asset = (GameObject)Resources.Load(path);

        if (asset == null)
        {
            Debug.LogError("load unit failed " + path);
            return(null);
        }
        objLoad = GameObject.Instantiate(asset) as GameObject;
        objLoad.transform.localScale    = Vector3.one;
        objLoad.transform.localPosition = Vector3.zero;

        //Vector3 anchorPos = Vector3.zero;
        //Vector2 sizeDel = Vector2.zero;
        //Vector3 scale = Vector3.one;
        //RectTransform rect = objLoad.GetComponent<RectTransform>();
        //if(rect != null)
        //{
        //    anchorPos = rect.anchoredPosition;
        //    sizeDel = rect.sizeDelta;
        //    scale = rect.localScale;
        //}

        objLoad.transform.SetParent(parent, false);
        //if (rect != null)
        //{
        //    rect.anchoredPosition = anchorPos;
        //    rect.sizeDelta = sizeDel;
        //    rect.localScale = scale;
        //}
        if (cache)
        {
            LoadResDic.Add(path, objLoad);
        }
        return(objLoad);
    }
    public static void DestroyLoad(string path)
    {
        if (LoadResDic == null || LoadResDic.Count == 0)
        {
            return;
        }
        GameObject obj = null;

        if (LoadResDic.TryGetValue(path, out obj) && obj != null)
        {
            GameObject.DestroyImmediate(obj);
            LoadResDic.Remove(path);
            //System.GC.Collect();
        }
    }
 public static void DestroyLoad(GameObject obj)
 {
     if (LoadResDic == null || LoadResDic.Count == 0)
     {
         return;
     }
     if (obj == null)
     {
         return;
     }
     foreach (string key in LoadResDic.Keys)
     {
         GameObject objLoad;
         if (LoadResDic.TryGetValue(key, out objLoad) && objLoad == obj)
         {
             GameObject.DestroyImmediate(obj);
             LoadResDic.Remove(key);
             break;
         }
     }
 }