// 2013-10 : l'instanciation d'objet passe par ici (pas swap ni copy) public IEnumerator cloneObject(OSLibObject oslObj, Vector3 position) { mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(true); yield return new WaitForEndOfFrame(); Transform newObj; position.y = 100; OSLib objLib = oslObj.GetLibrary (); //Debug.Log("avant www"); Montage.assetBundleLock = true; WWW www = WWW.LoadFromCacheOrDownload (objLib.GetAssetBundlePath (), objLib.GetVersion ()); yield return www; AssetBundle assetBundle = www.assetBundle; //Debug.Log("Apres www"); Object original = assetBundle.LoadAsset (oslObj.GetModel ().GetPath (), typeof(GameObject)); Vector3 wordPos = new Vector3(); Ray ray=Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height-180.0f,1.0f)); RaycastHit hit; if(Physics.Raycast(ray,out hit,1000f)) wordPos = hit.point; Vector3 temp = Camera.main.ScreenToWorldPoint (new Vector3(Screen.width / 2, Screen.height - 100.0f, 200.0f)); //Debug.DrawLine (ray.origin, hit.point, Color.cyan,2000.0f); float temp_hauteur = SceneControl.m_hauteur; wordPos = Camera.main.transform.position + Camera.main.transform.forward * 100.0f*(0.0000005f*temp_hauteur+1.0f); position += Camera.main.transform.forward * (1f + SceneControl.m_hauteur * 0.2f)*10f; GameObject go = (GameObject) Instantiate (original, position, new Quaternion (0,0,0,0)); newObj = go.transform; newObj.name = newObj.name + ++iobjectNumber; newObj.parent = objectNode.transform; newObj.gameObject.layer = 9; newObj.gameObject.AddComponent <ObjBehav>(); newObj.gameObject.AddComponent<ApplyShader>(); ObjData data = (ObjData) newObj.gameObject.AddComponent <ObjData>(); yield return new WaitForEndOfFrame(); // attend la fin du start de ObjBehav data.SetObjectModel (oslObj, assetBundle); data.selFab = selFab; data.selTyp = selTyp; data.selObj = selObj; assetBundle.Unload (false); Montage.assetBundleLock = false; //yield return new WaitForEndOfFrame(); // attend la fin du start de ObjBehav // newObj.name = prefix+"_"+o.name; float y = newObj.gameObject.GetComponent<ObjBehav>().init(); // > met a la bonne position en y newObj.transform.position = new Vector3(newObj.transform.position.x, y, newObj.transform.position.z); // -- Si c'est un gros objet, le déplacer un peu vers le fond -- float s = Montage.sm.getCamData().m_s; if(newObj.GetComponent<Collider>().bounds.size.z > 1.4f*s) { Matrix4x4 m = Camera.main.cameraToWorldMatrix; float distance = (newObj.GetComponent<Collider>().bounds.size.z < 4*s ? 4*s : newObj.GetComponent<Collider>().bounds.size.z)*-1.25f; Vector3 p = m.MultiplyPoint(new Vector3(0f, 0f, distance)); Vector3 pos = newObj.transform.position; pos.Set(p.x, pos.y, p.z); newObj.transform.position = pos; } if(oslObj.GetModules().FindModule("sol")!=null) { position = Camera.main.transform.position + Camera.main.transform.forward * 5.0f * Mathf.Max(10,newObj.transform.localScale.x); position.y = y; newObj.transform.position = position; } // newObj.transform.Rotate (new Vector3 (0, 180, 0)); // TODO Ajouter un "si iOS" string pref = newObj.GetComponent<ObjData> ().GetObjectModel ().GetObjectType (); //string pref = originalsNode.transform.GetChild(newObj.GetComponent<ObjData>().getTyp()).name; /* if(pref != "piscines" && pref != "spas") // Pas d'ombres pour les piscines (très moches) { if(_iosShadows) { _iosShadows.GetComponent<IosShadowManager>().AddShadow(newObj.gameObject); } else Debug.LogWarning("Objet \"iosShadows\" introuvable. Les ombres pour iOS risquent de ne pas fonctionner."); } else { //newObj.gameObject.AddComponent ("PoolInitializer"); }*/ // -- Ajout de l'objet au mode2D si besoin -- OSLibObject obj = newObj.transform.GetComponent<ObjData>().GetObjectModel(); if (obj.GetObjectType () == "pool" || obj.GetObjectType () == "dynamicShelter" || obj.IsMode2D ()) { m_mode2D.AddObj (newObj.transform); } if (newObj.GetComponent<ObjData> ().GetObjectModel ().GetModules().FindModule("duck")!=null)//TODO Temporaire a enlever plus tard { // // // This is the parent node // Transform parent = (Transform) newObj.transform.Find("water"); // // This is the prefab // GameObject prefab = (GameObject)Resources.Load("animDuck"); // // Add the instance in the hierarchy // GameObject obj = (GameObject)Instantiate(prefab); // // // Find the instantiate prefab and asign the parent // // obj.transform.localScale = go.transform.localScale;//new Vector3 (8.0f,8.0f,8.0f);//TODO < remplacer 8.0f par le scale de la scene // obj.transform.parent = parent; // obj.transform.position = new Vector3(0,-0.1f,0); newObj.gameObject.AddComponent<Function_duck>(); data.updateSpecFcns(); if(go.GetComponent<MeshCollider>())// <- ca aussi ca doit etre mis dans les libs { go.GetComponent<MeshCollider>().convex = true; } } // if (newObj.GetComponent<ObjData> ().GetObjectModel ().GetModules().FindModule("liner")!=null)//TODO Temporaire a enlever plus tard // { // newObj.FindChild("frise").gameObject.AddComponent<PoolFrise>(); // } yield return new WaitForEndOfFrame(); mainScene.GetComponent<GUIMenuLeft>().updateSceneObj(); Camera.main.GetComponent<ObjInteraction>().setSelected(go); yield return new WaitForEndOfFrame(); m_obj2rotate = go; mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(false); PC.ctxHlp.ShowCtxHelp(m_ctxPanelID); yield return true; }
//Fonction de chargement des objets------------------------------------------- //Elle est ici car du fait des librairies onlines, la fonctions de chargement //doit etre une IEnumerator (s�quen�age et chargement d'assetBundle oblige) //(pas possible de la laisser dans ObjectsModel car pas Monobehaviour) // Attention, fonction longue et imbuvable ^^ //---------------------------------------------------------------------------- IEnumerator loadObjs(BinaryReader buf) { mainScene.GetComponent<PleaseWaitUI>().SetLoadingMode(true); mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(true); if(!mainScene.GetComponent<PleaseWaitUI>().IsDisplayingIcon()) yield return new WaitForEndOfFrame (); if (buf == null) Debug.Log ("BUFFER NULL"); else Debug.Log ("BUFFER PLEIN"); bool cont = true; int nb=0; try { nb = buf.ReadInt32();// Nombre d'objet sauvegard� Debug.Log("nombre objet charg� " + nb); mainScene.GetComponent<GUIMenuMain>().flushObjects();// Enleve tout les objets de la scene courante } catch { Debug.Log("BIZARRE "); cont = false; } if (!cont) { yield return null; } for(int i=0;i<nb;i++) { int typeId, brandId, indexObj; Vector3 tmpPos; Quaternion tmpRot; Vector3 tmpScale; IDictionary conf; OSLibObject oslibObj; OSLibCategory catLvl1; bool lockability; #region chargement parametres objs //Chargement type/marque/objet brandId = -1; try{ brandId = buf.ReadInt32(); } catch { Debug.Log("ALERTE"); } typeId = buf.ReadInt32(); indexObj = buf.ReadInt32(); //correction du bug de la biblioth�que designer de piscine � laquelle on a retir� 2 mod�les if(brandId==4016 && indexObj>1) indexObj=0; /*if(indexObj>1) { indexObj = 0; }*/ Debug.Log("brand "+brandId+", type "+typeId+", and index "+indexObj+" loaded"); oslibObj = new OSLibObject(-1, "", "", "", null, null, null, null, false, false,-1); catLvl1 = null;// = om.getMainLib().GetCategoryList ()[typeId]; if(brandId != -1) { OSLibCategory catLvl2 = null; // int indexObjSearch=0; bool founded = false; foreach (OSLibCategory catLvl1NewSearch in om.getMainLib().GetCategoryList ()) { if(founded) break; // Debug.Log("--------------- Cat�gorie niveau 1 : "+catLvl1NewSearch.GetDefaultText()); foreach (OSLibCategory catLvl2NewSearch in catLvl1NewSearch.GetCategoryList ()) { if(founded) break; // Debug.Log("--------------- Cat�gorie niveau 2 : "+catLvl2NewSearch.GetDefaultText()); foreach (OSLibObject obj in catLvl2NewSearch.GetObjectList()) { if(founded) break; // Debug.Log("---------------- -------------- Objets : "+obj.GetDefaultText()+", cat 1 : "+obj.getCategory().GetParentCategory().GetId()+", cat2 : "+obj.getCategory().GetId()); if((obj.getCategory().GetParentCategory().GetId()==typeId) && (obj.getCategory().GetId()==brandId)) { //if (indexObjSearch==indexObj) if(indexObj == obj.GetId()) { catLvl1= catLvl1NewSearch; catLvl2= catLvl2NewSearch; oslibObj = obj; founded = true; break; } /* else { indexObjSearch++; } */ } } } } Debug.Log("osLib loaded founded "+founded); /* //if(catLvl1!=null) for(int lvl2 = 0;lvl2<catLvl1.GetCategoryList ().Count;lvl2++) { if(catLvl1.GetCategoryList ()[lvl2].GetId() == brandId) catLvl2 = catLvl1.GetCategoryList ()[lvl2]; } //if(catLvl2!=null) // if(catLvl2.GetObjectList().Count>=indexObj) oslibObj = catLvl2.GetObjectList () [indexObj]; */ } else { // if(catLvl1!=null) // if(catLvl1.GetObjectList().Count>=indexObj) Debug.Log("ELSE"); oslibObj = catLvl1.GetObjectList () [indexObj]; } //chargement du spatial //position tmpPos = new Vector3(0, 0, 0); tmpPos.x = buf.ReadSingle(); tmpPos.y = buf.ReadSingle(); tmpPos.z = buf.ReadSingle(); //rotation tmpRot = new Quaternion(0, 0, 0, 0); tmpRot.x = buf.ReadSingle(); tmpRot.y = buf.ReadSingle(); tmpRot.z = buf.ReadSingle(); tmpRot.w = buf.ReadSingle(); tmpScale = new Vector3(1, 1, 1); if(!LibraryLoader.numVersionInferieur(cdm.versionSave,"1.6.5")) { //scale tmpScale.x = buf.ReadSingle(); tmpScale.y = buf.ReadSingle(); tmpScale.z = buf.ReadSingle(); } Debug.Log("pos and rot laded"); //LockAbility lockability = buf.ReadBoolean(); Debug.Log("lockability loaded"); //Chargement Configuration textures/couleurs int confCount = buf.ReadInt32(); Debug.Log("confCount loaded"); conf = new SortedList(); for(int a=0;a<confCount;a++) { try { string objTx = buf.ReadString(); int txIdx = buf.ReadInt32(); if(!conf.Contains(objTx)) conf.Add(objTx,txIdx); } catch (EndOfStreamException e) { Debug.LogWarning("endofstream exception : "+e.Message); m_errorLoading = true; mainScene.GetComponent<PleaseWaitUI>().SetLoadingMode(false); buf.Close(); isLoading = false; canShow(); // mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(false); Camera.main.GetComponent<ObjInteraction>().enabled = true; mainNode.transform.FindChild("_avatar").GetComponent<Avatar>().SetForceDisplay(false); GameObject.Find("mainCam").GetComponent<MainCamManager>().FitViewportToScreen(); mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(false); break; } } if (m_errorLoading) { yield return true; } Debug.Log("config tex/color loaded"); #endregion // } /* catch (EndOfStreamException ue) { Debug.Log("error chargement param objs :" + ue.ToString()); break; }*/ #region instanciation Obj // ----------- d�but de cr�ation de l'objet ------------------ //Ouverture assetBundle //if(oslibObj!=null) { while (assetBundleLock) { yield return new WaitForEndOfFrame (); } OSLib objLib = oslibObj.GetLibrary (); if(objLib != null) { Montage.assetBundleLock = true; WWW www = WWW.LoadFromCacheOrDownload (objLib.GetAssetBundlePath (), objLib.GetVersion ()); yield return www; GameObject go; Transform newObj; AssetBundle assetBundle; ObjData data; try { assetBundle = www.assetBundle; //Chargement de l'objet(GameObject) UnityEngine.Object original = assetBundle.LoadAsset (oslibObj.GetModel ().GetPath (),typeof(GameObject)); //instanciation //Debug.Log(typeId+ " "+brandId+" "+indexObj+" "+original); go = (GameObject)Instantiate(original, tmpPos, tmpRot); go.transform.localScale = tmpScale; if(go.GetComponent<Rigidbody>()) go.GetComponent<Rigidbody>().isKinematic = true; newObj = go.transform; //Configuraton de lobjet newObj.parent = mainNode.transform; newObj.name = newObj.name + i; newObj.gameObject.AddComponent <ObjBehav>(); newObj.gameObject.AddComponent <ApplyShader>(); newObj.gameObject.layer = 9; data = (ObjData)newObj.gameObject.AddComponent<ObjData>(); } catch (UnityException ue) { Debug.Log("instanciation objs :" + ue.ToString()); break; } yield return new WaitForEndOfFrame(); // attend la fin du start du objData data.SetObjectModel (oslibObj, assetBundle); assetBundle.Unload (false); Montage.assetBundleLock = false; yield return new WaitForEndOfFrame(); // attend la fin du start de ObjBehav // data.loadConf(conf); yield return StartCoroutine(data.loadConfIE(conf)); try { float y = newObj.gameObject.GetComponent<ObjBehav>().init(); // > met a la bonne position en y newObj.gameObject.GetComponent<ObjBehav>().iAmLocked(lockability); newObj.transform.position = new Vector3(newObj.transform.position.x, newObj.transform.position.y+y, newObj.transform.position.z); string pref = newObj.GetComponent<ObjData> ().GetObjectModel ().GetObjectType (); //string pref = originalsNode.transform.GetChild(newObj.GetComponent<ObjData>().getTyp()).name; if(go.GetComponent<MeshCollider>())// <- ca aussi ca doit etre mis dans les libs { go.GetComponent<MeshCollider>().convex = true; go.transform.localRotation = tmpRot; // Correction du bug qui fait que la piscinne avec le canard est charg�e de travers } /*if (newObj.GetComponent<ObjData> ().GetObjectModel ().GetModules().FindModule("liner")!=null)//TODO Temporaire a enlever plus tard { newObj.FindChild("frise").gameObject.AddComponent<PoolFrise>(); }*/ mainScene.GetComponent<GUIMenuLeft>().updateSceneObj(); newObj.GetComponent<ObjBehav> ().SetHeighOff7(tmpPos.y); // -- Ajout du nouvel objet au mode2D si besoin -- OSLibObject obj = newObj.transform.GetComponent<ObjData>().GetObjectModel(); if(obj.GetObjectType() == "pool" || obj.GetObjectType() == "dynamicShelter" || obj.IsMode2D()) m_mode2D.AddObj(newObj.transform); // ----------- fin de cr�ation de l'objet ------------------ #endregion Debug.Log("instanciation obj loaded"); } catch (EndOfStreamException ue) { Debug.Log("instanciation objs :" + ue.ToString()); break; } #region chargement SpecFcn //Chargement des Fonctions Sp�cifiques try { int spcFcnNb = buf.ReadInt32(); //GameObject tmp = new GameObject(); for(int s=0;s<spcFcnNb;s++) { string type = buf.ReadString(); if(type == "Function_PergolaAutoFeet" && LibraryLoader.numVersionInferieur(cdm.versionSave,"1.2.95")) { type = "FunctionConf_PergolaAutoFeet"; } int id = buf.ReadInt32(); foreach(Component c in go.GetComponents<MonoBehaviour>()) { if(c.GetType().ToString().StartsWith("Function")) { if(((Function_OS3D)c).GetFunctionId() == id && c.GetType().ToString() == type) { ((Function_OS3D)c).load(buf); } } } /* if(go.GetComponent(type)) { ((Function_OS3D) go.GetComponent(type)).load(buf); } else { go.AddComponent(type); data.updateSpecFcns(); ((Function_OS3D) go.GetComponent(type)).load(buf); } */ } Debug.Log("spcfnc loaded"); } catch (EndOfStreamException ue) { Debug.Log("spec fcn:" + ue.ToString()); break; } //----------------------------------------------------- #endregion mainScene.GetComponent<PleaseWaitUI>().setAdditionalLoadingTxt(TextManager.GetText("Montage.LoadingObject")+" "+(i+1)+"/"+nb); } } } mainScene.GetComponent<PleaseWaitUI>().SetLoadingMode(false); mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(false); // yield return new WaitForEndOfFrame(); // yield return Resources.UnloadUnusedAssets(); yield return null; }
// 2013-10 : le swap d'objet passe par ici public IEnumerator swap(Vector3 p, OSLibObject newO, GameObject oldO, int _selFab, int _selTyp, int _selObj) { mainScene.GetComponent<GUIMenuLeft>().canSwap = false; selFab = _selFab; selTyp = _selTyp; selObj = _selObj; mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(true); yield return new WaitForEndOfFrame(); Quaternion tmpRotate = new Quaternion(0,0,0,0); tmpRotate.eulerAngles = oldO.transform.localRotation.eulerAngles + new Vector3(0,180,0); Transform newObj; //newObj = Instantiate (objToIns, p, tmpRotate) as Transform; OSLib objLib = newO.GetLibrary (); while (Montage.assetBundleLock) { yield return new WaitForEndOfFrame (); } Montage.assetBundleLock = true; WWW www = WWW.LoadFromCacheOrDownload (objLib.GetAssetBundlePath (), objLib.GetVersion ()); yield return www; yield return new WaitForEndOfFrame(); // attend la fin du start AssetBundle assetBundle = www.assetBundle; Object original = assetBundle.LoadAsset (newO.GetModel ().GetPath (), typeof(GameObject)); GameObject go = Instantiate (original/*, p, tmpRotate*/) as GameObject; yield return new WaitForEndOfFrame(); // attend la fin du start //TODO: to remove newObj = go.transform; newObj.name = newObj.name + ++iobjectNumber; newObj.position = p; newObj.rotation = tmpRotate; newObj.parent = objectNode.transform; if(!newO.IsBrandObject()) newObj.localScale = oldO.transform.localScale; newObj.gameObject.AddComponent <ObjBehav>(); newObj.GetComponent<ObjBehav>().iAmLocked(oldO.GetComponent<ObjBehav>().amILocked()); newObj.gameObject.layer = 9; float y = newObj.gameObject.GetComponent<ObjBehav> ().init (); newObj.Rotate (new Vector3 (0, 180, 0)); newObj.gameObject.AddComponent <ApplyShader>(); ObjData data = (ObjData) newObj.gameObject.AddComponent <ObjData>(); yield return new WaitForEndOfFrame(); // attend la fin du start de ObjBehav data.selFab = selFab; data.selTyp = selTyp; data.selObj = selObj; data.SetObjectModel (newO, assetBundle); assetBundle.Unload (false); Montage.assetBundleLock = false; yield return new WaitForEndOfFrame(); // attend la fin du start de ObjBehav yield return StartCoroutine(data.loadConfIE(oldO.GetComponent<ObjData>().getConfiguration())); // float y = newObj.gameObject.GetComponent<ObjBehav>().init(); newObj.position = new Vector3(newObj.position.x, newObj.position.y+y, newObj.position.z); newObj.GetComponent<ObjBehav>().SetHeighOff7(p.y); // -- Suppression de l'ancien objet si besoin -- OSLibObject oldObj = oldO.transform.GetComponent<ObjData>().GetObjectModel(); if(oldObj.GetObjectType() == "pool" || oldObj.IsMode2D()) m_mode2D.RemoveObj(oldO.transform); // -- Ajout du nouvel objet au mode2D si besoin -- OSLibObject obj = newObj.transform.GetComponent<ObjData>().GetObjectModel(); if(obj.GetObjectType() == "pool" || obj.GetObjectType() == "dynamicShelter" || obj.IsMode2D()) m_mode2D.AddObj(newObj.transform); // OSLibObject oldOModel = oldO.GetComponent<ObjData> ().GetObjectModel (); // foreach (OSLibModule mod in oldOModel.GetModules ().GetStandardModuleList ()) // { // Transform modTargetOld = oldO.transform.Find (mod.GetModuleType ()); // Transform modTargetNew = newObj.transform.Find (mod.GetModuleType ()); // // if (modTargetNew != null && modTargetOld != null) // { // modTargetNew.renderer.material.color = modTargetOld.renderer.material.color; // modTargetNew.renderer.material.mainTexture = modTargetOld.renderer.material.mainTexture; // } // } //Chargement de la configuration foreach(MonoBehaviour func in newObj.GetComponents<MonoBehaviour>()) { // Debug.Log("Passe la "+func.GetType()); if(func.GetType().GetInterface("Function_OS3D") != null) { /* if((Function_OS3D)oldO.GetComponent(func.GetType().ToString()) != null) { ArrayList conf = ((Function_OS3D)oldO.GetComponent(func.GetType().ToString())).getConfig(); ((Function_OS3D)func).setConfig(conf); } */ foreach (MonoBehaviour function in oldO.GetComponents<MonoBehaviour>()) { // MonoBehaviour function = (MonoBehaviour)function2; if(function.GetType().ToString().CompareTo(func.GetType().ToString())==0) { if( ((Function_OS3D)function).GetFunctionId()== ((Function_OS3D)func).GetFunctionId() ) { ArrayList conf = ((Function_OS3D)function).getConfig(); ((Function_OS3D)func).setConfig(conf); } } } } } //------------------------------ Camera.main.GetComponent<ObjInteraction>().swapObj(newObj.gameObject,oldO); // cest ici quon fait le delete if (newObj.GetComponent<ObjData> ().GetObjectModel ().GetModules().FindModule("duck")!=null) //TODO Deplacer ca ailleur { // // This is the parent node // Transform parent = (Transform) newObj.transform.Find("water"); // // This is the prefab // GameObject prefab = (GameObject)Resources.Load("animDuck"); // // Add the instance in the hierarchy // GameObject obj = (GameObject)Instantiate(prefab); // // // Find the instantiate prefab and asign the parent // obj.transform.localScale = newObj.transform.localScale;//new Vector3 (8.0f,8.0f,8.0f);//TODO < remplacer 8.0f par le scale de la scene // obj.transform.parent = parent; // obj.transform.position = new Vector3(0,-0.1f,0); newObj.gameObject.AddComponent<Function_duck>(); data.updateSpecFcns(); if(go.GetComponent<MeshCollider>())// <- ca aussi ca doit etre mis dans les libs { go.GetComponent<MeshCollider>().convex = true; } } // mainScene.GetComponent<GUIMenuLeft>().updateSceneObj(); mainScene.GetComponent<PleaseWaitUI>().SetDisplayIcon(false); yield return new WaitForEndOfFrame(); mainScene.GetComponent<GUIMenuLeft>().canSwap = true; yield return new WaitForEndOfFrame(); }