protected virtual bool CheckUnload(LevelSegment l) { bool did = false; switch (direction2D) { case Direction2D.down: { Vector3 mpoint = traceCamera.ScreenToWorldPoint(new Vector3(0, Screen.height, 0)); did = l.gameObject.transform.position.y - l.segmentLength > mpoint.y; if (!did) { //check if load too far away from postion when camera move back float lead = GetCommingLead(l.gameObject.transform.position); if (lead > 2 * loadSegmentLead) { Log.Debug("unloaded too far. {0}", l); did = true; } } break; } default: throw new System.Exception(String.Format("have not implement for direction {0}", direction2D)); } return(did); }
void PauseChildren(bool pause) { foreach (SegmentKV skv in _loadedSegments) { LevelSegment l = skv.level; // Log.Debug ("pause {0}",l.gameObject.name); PauseLevelSegment(l, pause); } }
void PauseLevelSegment(LevelSegment l, bool pause) { PauseAble[] ps = l.GetComponentsInChildren <PauseAble>(); if (null == ps) { return; } foreach (PauseAble item in ps) { if (pause) { item.OnPause(); } else { item.OnResume(); } } }
void LoadedRsSegment(LevelSegment lseg, LevelSegmentMeta meta) { if (meta.state == LevelSegmentMeta.LoadState.loaded) { Log.Debug("ignore lated segment load. {0}", meta); return; } lseg = Instantiate(lseg); meta.state = LevelSegmentMeta.LoadState.loaded; lseg.gameObject.transform.position = meta.position; lseg.gameObject.transform.SetParent(copyParent); SegmentKV skv = new SegmentKV(); skv.level = lseg; skv.meta = meta; _loadedSegments.Add(skv); Log.Debug("LOAD SEGMENT:{0}", lseg); }
public void ManageSegment() { if (null == metas) { metas = new List <LevelSegmentMeta>(); } // Log.Debug("ManageSegment {0} -- {1}", metas.Count, metaCount); //async loaded foreach (RequestKV rkv in _aysncLoadRequest) { ResourceRequest rr = rkv.request; if (!rr.isDone) { continue; } _needRemoveRequest.Add(rkv); UnityEngine.Object obj = rr.asset; Log.Debug("{0} async load done {1}.", rkv.meta.prefPath, obj); // LevelSegment lseg = Instantiate (obj) as LevelSegment; LoadedRsSegment(obj as LevelSegment, rkv.meta); } foreach (RequestKV l in _needRemoveRequest) { _aysncLoadRequest.Remove(l); } _needRemoveRequest.Clear(); //start load foreach (LevelSegmentMeta meta in metas) { if (meta.state == LevelSegmentMeta.LoadState.loaded) { // Log.Debug("haveloaded segment 1.1 {0}", meta); continue; } float lead = GetCommingLead(meta.position); // Log.Debug ("{0} lead is {1}",meta,lead); if (lead < 0) { lead = GetLeavingLead(meta.position, meta.length); if (lead >= 0) { //have left screen continue; } else { //need force load Log.Debug("force load segment 1 {0}", meta); LevelSegment seg = Resources.Load <LevelSegment>(meta.prefPath); LoadedRsSegment(seg, meta); } } else if (Math.Abs(lead) < Mathf.Epsilon) { //need force load Log.Debug("force load segment 2 {0}", meta); LevelSegment seg = Resources.Load <LevelSegment>(meta.prefPath); LoadedRsSegment(seg, meta); } else if (meta.state == LevelSegmentMeta.LoadState.not || meta.state == LevelSegmentMeta.LoadState.unloaded) { if (lead <= loadSegmentLead) { ResourceRequest req = Resources.LoadAsync <LevelSegment>(meta.prefPath); RequestKV rkv = new RequestKV(); rkv.request = req; rkv.meta = meta; _aysncLoadRequest.Add(rkv); meta.state = LevelSegmentMeta.LoadState.loading; } } else if (meta.state == LevelSegmentMeta.LoadState.loading) { //force load if (lead <= forceLoadSegmentLead) { Log.Debug("force load segment 3 {0}", meta); LevelSegment seg = Resources.Load <LevelSegment>(meta.prefPath); // seg = Instantiate (seg); LoadedRsSegment(seg, meta); } } } //remove foreach (SegmentKV skv in _loadedSegments) { LevelSegment l = skv.level; if (CheckUnload(l)) { Log.Info("segment {0}:{1} need unload.", l.gameObject.name, l.gameObject.transform.position); _needRemoveSegments.Add(skv); } } bool needClean = _needRemoveSegments.Count > 0; foreach (SegmentKV l in _needRemoveSegments) { // Resources.UnloadAsset (l.meta.loadedAsset); _loadedSegments.Remove(l); UnityEngine.Object.Destroy(l.level.gameObject); l.meta.state = LevelSegmentMeta.LoadState.unloaded; Log.Debug("UNLOAD SEGMENT:{0}", l.meta); } _needRemoveSegments.Clear(); if (needClean) { Resources.UnloadUnusedAssets(); } }