IEnumerator LoadActorPrefabCoroutine(GameObject oldObj, PlayableDirector playableDirector, PlayableBinding playableBinding, Transform parent, uint rid, string gameObjectName) { if (mModelIds == null) { yield break; } uint modelId = mModelIds[(int)rid - 1]; ModelInfo modelInfo = ModelHelper.GetModel(modelId); if (modelInfo == null) { GameDebug.LogError("Load actor prefab error, can not find model:" + modelId); yield break; } string prefabPath = modelInfo.Model; SGameEngine.PrefabResource pr = new SGameEngine.PrefabResource(); yield return(SGameEngine.ResourceLoader.Instance.StartCoroutine(SGameEngine.ResourceLoader.Instance.load_prefab(string.Format("Assets/Res/{0}.prefab", prefabPath), pr))); GameObject obj = pr.obj_; if (obj == null) { GameDebug.LogError("Load actor prefab " + prefabPath + " error, can not load prefab!"); yield break; } obj.name = gameObjectName; OnActorPrefabLoadSuccess(oldObj, obj, playableDirector, playableBinding, parent, modelInfo); }
IEnumerator LoadPrefabImpl(string asset_path, System.Action <GameObject> call_back) { PrefabResource prefab_res = new PrefabResource(); yield return(StartCoroutine(load_prefab(asset_path, prefab_res))); if (call_back != null) { call_back(prefab_res.obj_); } }
IEnumerator LoadEffect(string path) { if (gameObject == null || this == null) { yield return(null); } SGameEngine.PrefabResource prefab = new SGameEngine.PrefabResource(); yield return(SGameEngine.ResourceLoader.Instance.StartCoroutine(SGameEngine.ResourceLoader.Instance.load_prefab(path, prefab))); if (mIsDestory == true) { if (prefab != null && prefab.obj_ != null) { GameObject.DestroyImmediate(prefab.obj_); } yield return(null); } if (mParentGameObject == null) { GameDebug.LogError("Load UI3DGameObject error, parent GameObject is null!!!"); GameObject.DestroyImmediate(prefab.obj_); yield return(null); } if (mIsDestory == false && prefab != null && prefab.obj_ != null) { try { mEffectGameObject = prefab.obj_; mEffectGameObject.transform.SetParent(mParentGameObject.transform); mEffectGameObject.transform.localScale = Vector3.one; mEffectGameObject.transform.localPosition = Vector3.zero; mEffectGameObject.transform.localRotation = Quaternion.identity; } catch (System.Exception e) { throw; } } }
IEnumerator LoadDropEffect(string path, uint loadId) { if (gameObject == null || this == null) { yield return(null); } SGameEngine.PrefabResource prefab = new SGameEngine.PrefabResource(); yield return(SGameEngine.ResourceLoader.Instance.StartCoroutine(SGameEngine.ResourceLoader.Instance.load_prefab(path, prefab))); if (m_IsDestory == true || loadId != mCurLoadEffectID) { if (prefab != null && prefab.obj_ != null) { GameObject.DestroyImmediate(prefab.obj_); } yield break; } if (prefab != null && prefab.obj_ != null) { try { var dropTrans = prefab.obj_.transform; dropTrans.parent = mParentTrans.Find("EffectGameObject"); dropTrans.localScale = Vector3.one; dropTrans.localPosition = Vector3.zero; dropTrans.localRotation = Quaternion.identity; xc.ui.ugui.UIHelper.SetLayer(dropTrans, mGroundLayer); } catch (System.Exception e) { throw; } } }
/// <summary> /// <para>加载prefab并进行实例化,实例化后的GameObject会添加到dic_gameobj_asset_中</para> /// <para>调用gc时如果GameObject实例已经销毁,则会自动减少对应AssetObject的引用计数</para> /// <para>创建后返回的GameObject最好不要再进行Clone,因为GameObject被销毁后,Clone的实例资源也可能丢失</para> /// </summary> public IEnumerator load_prefab(string _asset_path, PrefabResource _result, bool _dont_destroy_on_load = false, bool _cache_global_bundle = false, Transform parent = null) { if (Const.Language != LanguageType.SIMPLE_CHINESE) { LocalizeManager.Instance.LocalizePath(ref _asset_path); } if (is_destroyed_) { yield break; } string lower_case = _asset_path.ToLower(); if (!lower_case.EndsWith(CACHE_STR_PREFAB)) { Debug.LogError(string.Format("{0} is not a prefab!", lower_case)); yield break; } //1. lock the asset //do nothing //2. load asset and instantiate prefab AssetResource ar = new AssetResource(); yield return(StartCoroutine(load_asset(_asset_path, type_game_object_, ar))); if (ar.asset_ != null) { /*if(_asset_path.Contains("/Res/UI/") && !_asset_path.Contains("/Res/UI/Atlas/") && _asset_path.EndsWith(".prefab")) * { * yield return StartCoroutine(load_atlas(ar)); * }*/ _result.obj_ = instantiate_prefab_from_asset_object(ar, parent); if (_dont_destroy_on_load) { GameObject.DontDestroyOnLoad(_result.obj_); } //adjust the gameobject postion to not be seen directly (_result.obj_ as GameObject).transform.position = new Vector3(-100, -1000, -100); } //3.已经在instantiate_prefab_from_asset_object进行了实例化,可以释放AssetResource对AssetObject的引用 if (!_cache_global_bundle) { ar.destroy(); } else { if (mGlobalCache.ContainsKey(_asset_path)) { ar.destroy(); } else { mGlobalCache[_asset_path] = true; } } //4.unlock asset //do nothing }
/// <summary> /// 加载timeline prefab协程 /// </summary> /// <param name="id"></param> /// <param name="prefabPath"></param> /// <param name="finishCallback"></param> /// <param name="pos"></param> /// <param name="needCameraFollowInterpolationWhenFinished"></param> /// <param name="showLocalPlayer"></param> /// <returns></returns> IEnumerator LoadPrefabCoroutine(TimelineConfig config, System.Action finishCallback, AvatarProperty avatarProperty) { uint id = config.Id; string prefabPath = config.PrefabPath; Vector3 pos = config.Pos; Vector3 rotation = config.Rotation; bool needCameraFollowInterpolationWhenFinished = config.NeedCameraFollowInterpolationWhenFinished; bool showLocalPlayer = config.ShowLocalPlayer; bool noStopLocalPlayer = config.NoStopLocalPlayer; bool showUI = config.ShowUI; bool pauseMusic = config.PauseMusic; bool needGC = config.NeedGC; SGameEngine.PrefabResource pr = new SGameEngine.PrefabResource(); yield return(SGameEngine.ResourceLoader.Instance.StartCoroutine(SGameEngine.ResourceLoader.Instance.load_prefab(string.Format("Assets/Res{0}.prefab", prefabPath), pr))); // 延迟资源的加载,测试用 //yield return new WaitForSeconds(5f); // 恢复玩家操作 GameInput.Instance.EnableInput(true, true); GameObject obj = pr.obj_; if (obj == null) { if (finishCallback != null) { finishCallback(); } if (mLoadingTimelineIdList.Contains(id) == true) { mLoadingTimelineIdList.Remove(id); } GameDebug.LogError("Play timeline " + prefabPath + " error, can not load prefab!"); yield break; } PlayableDirector playableDirector = obj.GetComponent <PlayableDirector>(); if (playableDirector == null) { GameObject.DestroyImmediate(obj); if (finishCallback != null) { finishCallback(); } if (mLoadingTimelineIdList.Contains(id) == true) { mLoadingTimelineIdList.Remove(id); } GameDebug.LogError("Play timeline " + prefabPath + " error, can not find PlayableDirector component!"); yield break; } string relatedPrefabPath = config.RelatedPrefabPath; SGameEngine.PrefabResource relatedPrefabPr = null; if (string.IsNullOrEmpty(relatedPrefabPath) == false) { relatedPrefabPr = new SGameEngine.PrefabResource(); yield return(SGameEngine.ResourceLoader.Instance.StartCoroutine(SGameEngine.ResourceLoader.Instance.load_prefab(string.Format("Assets/Res{0}.prefab", relatedPrefabPath), relatedPrefabPr))); } obj.SetActive(false); obj.transform.position = pos; obj.transform.localScale = Vector3.one; obj.transform.localEulerAngles = rotation; TimelineInfo timelineInfo = new TimelineInfo(); playableDirector.time = 0; timelineInfo.Id = id; timelineInfo.PlayableDirector = playableDirector; timelineInfo.FinishCallback = finishCallback; timelineInfo.AvatarProperty = avatarProperty; timelineInfo.NeedCameraFollowInterpolationWhenFinished = needCameraFollowInterpolationWhenFinished; timelineInfo.ShowLocalPlayer = showLocalPlayer; timelineInfo.NoStopLocalPlayer = noStopLocalPlayer; timelineInfo.ShowUI = showUI; timelineInfo.PauseMusic = pauseMusic; timelineInfo.NeedGC = needGC; // 关联GameObject timelineInfo.RelatedGameObject = null; if (relatedPrefabPr != null) { if (relatedPrefabPr.obj_ != null) { timelineInfo.RelatedGameObject = relatedPrefabPr.obj_; timelineInfo.RelatedGameObject.SetActive(false); timelineInfo.RelatedGameObject.transform.position = config.RelatedPrefabPos; timelineInfo.RelatedGameObject.transform.localScale = Vector3.one; timelineInfo.RelatedGameObject.transform.localEulerAngles = Vector3.zero; } else { GameDebug.LogError("Play timeline " + id + " error, can not load related prefab " + relatedPrefabPath); } } // lua脚本 if (string.IsNullOrEmpty(config.BehaviorScript) == false) { if (LuaScriptMgr.Instance != null && LuaScriptMgr.Instance.IsLuaScriptExist(config.BehaviorScript)) { LuaMonoBehaviour com = obj.AddComponent <LuaMonoBehaviour>(); com.Init(config.BehaviorScript); } else { System.Type t = System.Type.GetType(config.BehaviorScript); if (t != null) { obj.AddComponent(t); } } } // 延迟资源的加载,测试用 //yield return new WaitForSeconds(10f); if (mPlayingTimeline != null) { mCacheTimelineList.Enqueue(timelineInfo); } else { PlayImpl(timelineInfo); } if (mLoadingTimelineIdList.Contains(id) == true) { mLoadingTimelineIdList.Remove(id); } //GameDebug.LogError("TimelineManager.LoadPrefabCoroutine: " + id); }