Beispiel #1
0
        /// <summary>
        /// 加载Asset资源
        /// </summary>
        /// <param name="path">StreamingAssets下相对路径</param>
        /// <param name="sourcename">资源名</param>
        /// <param name="cb">回调函数</param>
        /// <returns></returns>
        void LoadAsset(string path, string sourcename, UnityAction <Object> cb, bool forceloadAsyn = true)
        {
            if (mAssetBundleDic.ContainsKey(path))
            {
                AssetBundle ab = GetAssetBundle(path);
                if (forceloadAsyn)
                {
                    GameMain.Instance.StartCoroutine(LoadSourceEnumerator(ab, sourcename, cb));
                }
                else
                {
#if UNITY_EDITOR
                    Object go = Resources.Load(path + sourcename);
#else
                    Object go = ab.LoadAsset(sourcename);
#endif
                    if (go != null)
                    {
                        cb(go);
                    }
                    else
                    {
                        DebugMod.LogError("can't get assetresource from " + path);
                    }
                }
            }
            else
            {
                GameMain.Instance.StartCoroutine(LoadBundleEnumerator(path, sourcename, cb, forceloadAsyn));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 加载AssetBundle
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <param name="sourcename">资源名</param>
        /// <param name="cb">回调</param>
        /// <returns></returns>
        IEnumerator LoadBundleEnumerator(string path, string sourcename, UnityAction <Object> cb, bool forceloadAsyn = false)
        {
            using (WWW www = new WWW(path))
            {
                yield return(www);

                if (www.isDone)
                {
                    if (mAssetBundleDic.ContainsKey(path))
                    {
                        LoadAsset(path, sourcename, cb, forceloadAsyn);
                        yield break;
                    }

                    AssetBundle ab = www.assetBundle;
                    if (null == ab)
                    {
                        DebugMod.LogError("www.assetBundle is null when load:" + path);
                        yield break;
                    }

                    AddAssetBundle(path, ab);
                    LoadAsset(path, sourcename, cb, forceloadAsyn);
                }
                else
                {
                    DebugMod.LogError(www.error);
                }
            }
        }
Beispiel #3
0
        public static GameObject FindChild(Transform transform, string childname, bool lowercompare = true)
        {
            if (null == transform)
            {
                DebugMod.LogError("transform is null in FindChild");
                return(null);
            }

            int totalChildCount = transform.childCount;

            for (int i = 0; i < totalChildCount; i++)
            {
                string name = transform.GetChild(i).gameObject.name;
                if ((lowercompare?name.ToLowerInvariant():name) == childname.ToLowerInvariant())
                {
                    return(transform.GetChild(i).gameObject);
                }
            }

            for (int i = 0; i < totalChildCount; i++)
            {
                GameObject go = FindChild(transform.GetChild(i), childname);
                if (null != go)
                {
                    return(go);
                }
            }

            return(null);
        }
Beispiel #4
0
 public static T Clone <T>(Object sample) where T : Object
 {
     if (sample == null)
     {
         DebugMod.LogError("sample is null in:" + MethodBase.GetCurrentMethod().Name);
         return(null);
     }
     return(GameObject.Instantiate(sample) as T);
 }
Beispiel #5
0
        public static void AddChild(Object parent, Object child, bool usechildoriginaltransform = false)
        {
            if (child == null)
            {
                DebugMod.LogError("child is null in:" + MethodBase.GetCurrentMethod().Name);
                return;
            }

            Transform prt = null;
            Transform cld = null;

            if (parent != null)
            {
                if (parent is GameObject)
                {
                    prt = ((GameObject)parent).transform;
                }
                else if (parent is Transform)
                {
                    prt = (Transform)parent;
                }
                else
                {
                    DebugMod.LogError("parent type is unknown in:" + MethodBase.GetCurrentMethod().Name);
                }
            }


            if (child is GameObject)
            {
                cld = ((GameObject)child).transform;
            }
            else if (child is Transform)
            {
                cld = (Transform)child;
            }
            else
            {
                DebugMod.LogError("child type is unknown in:" + MethodBase.GetCurrentMethod().Name);
            }

            if (prt == null || cld == null)
            {
                return;
            }

            Vector3    localpos = cld.localPosition;
            Quaternion localrot = cld.localRotation;
            Vector3    localscl = cld.localScale;

            cld.parent        = prt;
            cld.localPosition = usechildoriginaltransform ? localpos : Vector3.zero;
            cld.localRotation = usechildoriginaltransform ? localrot : Quaternion.identity;
            cld.localScale    = usechildoriginaltransform ? localscl : Vector3.one;
        }
Beispiel #6
0
        public static T GetComponent <T>(GameObject go) where T : Component
        {
            if (go == null)
            {
                DebugMod.LogError("gameobject is null");
            }

            T t = go.GetComponent <T>();

            if (t == null)
            {
                t = go.AddComponent <T>();
            }
            return(t);
        }
Beispiel #7
0
        IEnumerator LoadSceneEnumerator(string path, string scenename, VoidDelegate loadfinishdo)
        {
            if (string.IsNullOrEmpty(scenename))
            {
                yield break;
            }

            _www = new WWW(path);
            yield return(_www);

            _sceneBundle = _www.assetBundle;
            _www.Dispose();
            _www = null;

            if (_sceneBundle != null)
            {
                _asyn = Application.LoadLevelAsync(scenename);
                yield return(_asyn);

                if (_asyn.isDone)
                {
                    yield return(1);

                    try
                    {
                        if (loadfinishdo != null)
                        {
                            loadfinishdo();
                        }
                    }
                    catch
                    {
                        DebugMod.LogError("Error occored in LoadSceneEnumerator callback delegate");
                    }


                    _asyn = null;
                }
            }
            else
            {
                DebugMod.LogError("can't load scene from: " + path);
            }
        }
Beispiel #8
0
        /// <summary>
        /// 加载AssetBundle中的资源
        /// </summary>
        /// <param name="ab">AssetBundle对象</param>
        /// <param name="sourcename">资源名</param>
        /// <param name="cb">回调</param>
        /// <returns></returns>
        IEnumerator LoadSourceEnumerator(AssetBundle ab, string sourcename, UnityAction <Object> cb)
        {
            AssetBundleRequest abr = ab.LoadAssetAsync(sourcename);

            Debug.Log("Source AssetBundleRequest");
            yield return(abr);

            if (abr.isDone)
            {
                if (abr.asset != null)
                {
                    Debug.Log("Source cb");
                    cb(abr.asset);
                }
                else
                {
                    DebugMod.LogError("Can't find res " + sourcename + "in assetbundle");
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// 加载AssetBundle
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <param name="sourcename">资源名</param>
        /// <param name="cb">回调</param>
        /// <returns></returns>
        IEnumerator LoadBundleEnumerator(string path, UnityAction <AssetBundle> cb, bool managercharge = true)
        {
            using (WWW www = new WWW(path))
            {
                yield return(www);

                if (www.isDone)
                {
                    AssetBundle ab = www.assetBundle;
                    if (null == ab)
                    {
                        DebugMod.LogError("www.assetBundle is null when load:" + path);
                        yield break;
                    }
                    if (managercharge)
                    {
                        AddAssetBundle(path, ab);
                        LoadAsset(path, cb);
                    }
                    else
                    {
                        try
                        {
                            cb(ab);
                        }
                        catch
                        {
                            DebugMod.LogError("Error occored in LoadBundleEnumerator callback delegate");
                        }
                    }
                }
                else
                {
                    DebugMod.LogError(www.error);
                }
            }
        }