void readImage(string pPath) { FileInfo lFileInfo = new FileInfo(pPath); if (imgPathToData.ContainsKey(lFileInfo.ToString())) { nowPainterOutData = imgPathToData[lFileInfo.ToString()]; } else { Texture2D lImage = new Texture2D(4, 4, TextureFormat.ARGB32, false); using (var lImageFile = new FileStream(fileBrowserDialog.selectedLocation, FileMode.Open)) { BinaryReader lBinaryReader = new BinaryReader(lImageFile); lImage.LoadImage(lBinaryReader.ReadBytes((int)lImageFile.Length)); } nowPainterOutData = new PainterOutData(); nowPainterOutData.modelImage = GameResourceManager.Main.createImage(lImage); //new GenericResource<Texture2D>( lImage); imgPathToData[lFileInfo.ToString()] = nowPainterOutData; } image = nowPainterOutData.modelImage.resource; }
void toModelData(Transform lModelsTransform, PainterOutData pOut) { lModelsTransform.position = Vector3.zero; lModelsTransform.rotation = Quaternion.identity; lModelsTransform.localScale = Vector3.one; //lModelsTransform.position = modelTransform.position; //nowPainterOutData.paintingModels = new GenericResource<PaintingModelData>[lModelsTransform.childCount]; //nowPainterOutData.transforms = new zzTransform[lModelsTransform.childCount]; //nowPainterOutData.modelTexture = new GenericResource<Texture2D>[lModelsTransform.childCount]; nowPainterOutData.modelCount = lModelsTransform.childCount; int i = 0; foreach (Transform lSub in lModelsTransform) { var lTexture = (Texture2D)lSub.Find("Render").GetComponent<Renderer>() .material.mainTexture; var lGameObject = lSub.gameObject; var lModelData = PaintingModelData .createData(lGameObject, new Vector2(lTexture.width, lTexture.height)); //useImageMaterial(lPaintingMesh); nowPainterOutData.paintingModels[i] = GameResourceManager.Main.createModel(lModelData); //= new GenericResource<PaintingModelData>( lModelData ); nowPainterOutData.transforms[i] = new zzTransform(lSub); nowPainterOutData.modelTexture[i] = GameResourceManager.Main.createImage(lTexture); //new GenericResource<Texture2D>(lTexture); ++i; } }
/* public string saveName = "temp"; void saveModel(string pName) { if (!Directory.Exists(pName)) Directory.CreateDirectory(pName); //确定资源共用关系,定义唯一值 var lTextures = new Dictionary<Texture2D, string>(); var lPaintingModelData = new Dictionary<PaintingModelData, string>(); foreach (Transform lModelObjectTransform in sceneManager) { GameObject lModelObject = lModelObjectTransform.gameObject; var lPaintingMesh = lModelObject.GetComponent<PaintingMesh>(); var lTexture2D = lPaintingMesh.material.mainTexture as Texture2D; if (!lTextures.ContainsKey(lTexture2D)) lTextures[lTexture2D] = System.Guid.NewGuid().ToString(); if (!lPaintingModelData.ContainsKey(lPaintingMesh.modelData)) lPaintingModelData[lPaintingMesh.modelData] = System.Guid.NewGuid().ToString(); } //保存图片 foreach (var lImgSave in lTextures) { using (var lImageFile = new FileStream(pName + "/" + lImgSave.Value + ".png", FileMode.Create)) { BinaryWriter lWriter = new BinaryWriter(lImageFile); lWriter.Write(lImgSave.Key.EncodeToPNG()); } } //保存模型 foreach (var lModelDataSave in lPaintingModelData) { using (var lImageFile = new FileStream(pName + "/" + lModelDataSave.Value + ".pmb", FileMode.Create)) { BinaryWriter lWriter = new BinaryWriter(lImageFile); lWriter.Write(lModelDataSave.Key.serializeToString()); } } //保存场景 { Hashtable lGroupData = new Hashtable(); ArrayList lObjectList = new ArrayList(sceneManager.objectCount); foreach (Transform lTransform in sceneManager) { GameObject lModelObject = lTransform.gameObject; var lPaintingMesh = lModelObject.GetComponent<PaintingMesh>(); Hashtable lObjectData = new Hashtable(); lObjectData["modelData"] = lPaintingModelData[lPaintingMesh.modelData]; lObjectData["customImage"] = lTextures[lPaintingMesh.material.mainTexture as Texture2D]; lObjectData["position"] = lTransform.position; lObjectData["rotation"] = lTransform.rotation; lObjectData["scale"] = lTransform.localScale; lObjectList.Add(lObjectData); } lGroupData["objectList"] = lObjectList; using (var lGroupFile = new FileStream(pName + "/" + groupFileName, FileMode.Create)) { BinaryWriter lWriter = new BinaryWriter(lGroupFile); lWriter.Write(zzSerializeString.Singleton.pack(lGroupData)); } } } GameResourceManager resourceManager = new GameResourceManager(); void readGroup(string pPath) { resourceManager.scanDirectory(pPath); string pStringData; using (var lGroupFile = new FileStream(pPath + "/" + groupFileName, FileMode.Open)) { BinaryReader lReader = new BinaryReader(lGroupFile); pStringData = lReader.ReadString(); } readGroupFromTable((Hashtable)zzSerializeString .Singleton.unpackToData(pStringData)); } void readGroupFromTable(Hashtable pData ) { ArrayList lObjectList = (ArrayList)pData["objectList"]; List<GameObject> lModelList = new List<GameObject>(); int i = 0; foreach (Hashtable lObjectData in lObjectList) { GameObject lGameObject = new GameObject("readMoedl"+i); Transform lTransform = lGameObject.transform; string lModelDataName = (string)lObjectData["modelData"]; string lCustomImageName = (string)lObjectData["customImage"]; var lPaintingModelData = resourceManager.getModelFromGuid(lModelDataName); PaintingMesh lPaintingMesh = PaintingMesh.create(lGameObject, lPaintingModelData); lPaintingMesh.useImageMaterial(resourceManager.getImageFromGuid(lCustomImageName)); //先设置Transform,在增加凸碰撞模型时,有时会长生错误 lTransform.position = (Vector3)lObjectData["position"]; lTransform.rotation = (Quaternion)lObjectData["rotation"]; lTransform.localScale = (Vector3)lObjectData["scale"]; lModelList.Add(lGameObject); ++i; //addModelObjects() //lGameObject.GetComponent<Rigidbody>().isKinematic = !inPlaying; } addModelObjects(lModelList.ToArray()); } */ void createObject(PainterOutData pPainterOutData) { var lModelParentObject = new GameObject(); var lModelsTransform = lModelParentObject.transform; List<GameObject> lModelList = new List<GameObject>(); for (int i=0;i<pPainterOutData.paintingModels.Length;++i) { GameObject lGameObject = GameSystem.Singleton.createObject("paintingObject"); lModelList.Add(lGameObject); var lPaintingMesh = PaintingMesh.create(lGameObject,pPainterOutData.paintingModels[i]); //lPaintingMesh.useImageMaterial(pPainterOutData.modelImage); lGameObject.GetComponent<RenderMaterialProperty>().imageResource = pPainterOutData.modelTexture[i]; pPainterOutData.transforms[i].setToTransform(lGameObject.transform); lGameObject.transform.parent = lModelsTransform; } var lPreviewLocalScale = previewTransform.localScale; Vector2 lSize = new Vector2(lPreviewLocalScale.x, lPreviewLocalScale.y); var lFitScale = getFitScale(modelPainterProcessor.modelsSize, lSize); var lScale = new Vector3(lFitScale.x, lFitScale.y, 1.0f); lModelsTransform.localScale = lScale; lModelsTransform.rotation = previewTransform.rotation; lModelsTransform.position = previewTransform.position; addModelObjects(lModelList.ToArray()); Destroy(lModelParentObject); }