public void AddImage(HogScene.ImageType imageType, GameObject imageGo) { if (imageGos == null) { imageGos = new GameObject[(int)HogScene.ImageType.Count]; } imageGos[(int)imageType] = imageGo; // if we have an obscured image, hide the whole if ((imageGos[(int)HogScene.ImageType.Obscured] != null) && (imageGos[(int)HogScene.ImageType.Whole] != null)) { imageGos[(int)HogScene.ImageType.Whole].SetActive(false); } }
//-------------------------------------------------------------------------- // private methods //-------------------------------------------------------------------------- static private void ImportHogScene(string assetPath) { // before we do anything else, try to deserialize the input file and be sure it's actually the right kind of file HogScene hogScene = (HogScene)DeserializeXml(assetPath, typeof(HogScene)); if (hogScene == null) { Debug.Log("The file " + assetPath + " wasn't able to generate a HOG scene."); return; } // next, we're going to be creating scenes, allow the user to save if they want // see if user wants to save current scene, bail if they don't if (EditorApplication.SaveCurrentSceneIfUserWantsTo() == false) { return; } // cache some useful variables string baseFilename = Path.GetFileNameWithoutExtension(assetPath); string baseDirectory = "Assets/" + Path.GetDirectoryName(assetPath.Remove(0, Application.dataPath.Length + 1)) + "/"; // if the scene already exists, delete it string scenePath = baseDirectory + baseFilename + " Scene.unity"; if (File.Exists(scenePath) == true) { File.Delete(scenePath); AssetDatabase.Refresh(); } // now create a new scene EditorApplication.NewScene(); Debug.Log(hogScene.layers.Length); // load up all the textures used and put them into a list List <Texture2D> textureList = new List <Texture2D>(); for (int layerIndex = 0; layerIndex < hogScene.layers.Length; layerIndex++) { Debug.Log(hogScene.layers[layerIndex].images.Length); for (int imageIndex = 0; imageIndex < hogScene.layers[layerIndex].images.Length; imageIndex++) { // we need to fixup all images that were exported from PS string texturePathName = baseDirectory + hogScene.layers[layerIndex].images[imageIndex].name; Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePathName, typeof(Texture2D)); // modify the importer settings TextureImporter textureImporter = AssetImporter.GetAtPath(texturePathName) as TextureImporter; if (textureImporter) { Debug.Log(layerIndex + " " + texturePathName); } else { Debug.Log(layerIndex + " " + texturePathName); } textureImporter.mipmapEnabled = false; textureImporter.isReadable = true; textureImporter.npotScale = TextureImporterNPOTScale.None; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.filterMode = FilterMode.Bilinear; // joachim mentioned internally Unity calls the next line before importing AssetDatabase.WriteImportSettingsIfDirty(texturePathName); AssetDatabase.ImportAsset(texturePathName); // does it go into our atlas array? if ((hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Whole) || (hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Obscured) || (hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Shadow)) { textureList.Add(inputTexture); } } } // create material string materialPath = baseDirectory + baseFilename + " Material.mat"; // remove previous one if it exists if (File.Exists(materialPath) == true) { File.Delete(materialPath); AssetDatabase.Refresh(); } // make a material and link it to atlas, save that too Material material = new Material(Shader.Find("Transparent/Diffuse")); AssetDatabase.CreateAsset(material, materialPath); AssetDatabase.Refresh(); // load it back material = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)); // make a new atlas texture // Texture2D atlas = new Texture2D(2048, 2048); Texture2D atlas = new Texture2D(4096, 4096); // to make an atlas we need an array instead of a list Texture2D[] textureArray = textureList.ToArray(); // pack it with all the textures we have // Rect[] atlasRects = atlas.PackTextures(textureArray, 1, 2048); Rect[] atlasRects = atlas.PackTextures(textureArray, 1, 4096); // save it to disk byte[] atlasPng = atlas.EncodeToPNG(); string atlasPath = baseDirectory + baseFilename + " Atlas.png"; if (File.Exists(atlasPath) == true) { File.Delete(atlasPath); AssetDatabase.Refresh(); } File.WriteAllBytes(atlasPath, atlasPng); AssetDatabase.Refresh(); TextureImporter atlasTextureImporter = AssetImporter.GetAtPath(atlasPath) as TextureImporter; atlasTextureImporter.mipmapEnabled = false; // atlasTextureImporter.maxTextureSize = 2048; atlasTextureImporter.maxTextureSize = 4096; atlasTextureImporter.wrapMode = TextureWrapMode.Clamp; atlasTextureImporter.filterMode = FilterMode.Bilinear; // joachim mentioned internally Unity calls the next line before importing AssetDatabase.WriteImportSettingsIfDirty(atlasPath); // re-import it back into Unity AssetDatabase.ImportAsset(atlasPath); // grab a reference to it atlas = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(Texture2D)); // be sure atlas is linked material.mainTexture = atlas; // create meshes for each image Vector3 position = Vector3.zero; int textureIndex = 0; int totalItemCount = 0; for (int layerIndex = 0; layerIndex < hogScene.layers.Length; layerIndex++) { // keep track of item count if (hogScene.layers[layerIndex].type == HogScene.LayerType.Custom) { continue; } totalItemCount++; // create a root game object for the images GameObject layerGo = new GameObject(hogScene.layers[layerIndex].name); position = Vector3.zero; position.z = -layerIndex; layerGo.transform.position = position; ItemController itemController = (ItemController)layerGo.AddComponent <ItemController>(); itemController.AddLayer(hogScene.layers[layerIndex].type); HogScene.ImageType hitTestImageType = (HogScene.ImageType)((int)HogScene.ImageType.Whole + 1); for (int imageIndex = 0; imageIndex < hogScene.layers[layerIndex].images.Length; imageIndex++) { // check hit tests first before image index possibly changes if (hogScene.layers[layerIndex].images[imageIndex].type < hitTestImageType) { Rect hitTestRect = new Rect(); Texture2D hitTestTexture = null; if (hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Hotspot) { string hitTestTexturePathName = baseDirectory + hogScene.layers[layerIndex].images[imageIndex].name; hitTestTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(hitTestTexturePathName, typeof(Texture2D)); } else { hitTestTexture = textureArray[textureIndex]; } //edited hitTestRect.x = (hogScene.layers[layerIndex].images[imageIndex].x - (hitTestTexture.width / 2)) / 100; hitTestRect.y = (hogScene.layers[layerIndex].images[imageIndex].y - (hitTestTexture.height / 2)) / 100; hitTestRect.width = (float)hitTestTexture.width / 100; hitTestRect.height = (float)hitTestTexture.height / 100; HitTestPixelData hitTest = ExtractHitTestData(hitTestTexture); itemController.AddHitTestData(hitTest.hitTestData, hitTestTexture.width, hitTestRect, hitTest.pixels); } // add visible images to the item controller // this must match the test used to determine atlas textures otherwise the index will be wrong if ((hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Whole) || (hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Obscured) || (hogScene.layers[layerIndex].images[imageIndex].type == HogScene.ImageType.Shadow)) { // setup the game object GameObject imageGo = new GameObject(Path.GetFileNameWithoutExtension(hogScene.layers[layerIndex].images[imageIndex].name)); ConfigureGo(imageGo, textureArray[textureIndex], material, atlasRects[textureIndex], baseDirectory + textureArray[textureIndex].name + " Mesh.asset"); position.x = hogScene.layers[layerIndex].images[imageIndex].x / 100f; position.y = hogScene.layers[layerIndex].images[imageIndex].y / 100f; imageGo.transform.parent = layerGo.transform; imageGo.transform.position = position; itemController.AddImage(hogScene.layers[layerIndex].images[imageIndex].type, imageGo); textureIndex++; } } } // setup our game camera Camera.main.gameObject.AddComponent <HOGController>(); SearchListController searchListController = (SearchListController)Camera.main.gameObject.AddComponent <SearchListController>(); searchListController.SetTotalItemCount(totalItemCount); position = Vector3.zero; position.z = -hogScene.layers.Length; Camera.main.transform.position = position; Camera.main.orthographic = true; // Camera.main.orthographicSize = 3;//(768.0f/2.0f); Camera.main.orthographicSize = 3; Camera.main.nearClipPlane = 0; Camera.main.farClipPlane = hogScene.layers.Length + 2; RenderSettings.ambientLight = Color.white; atlas = null; atlasRects = null; textureList.Clear(); textureList = null; textureArray = null; // one final save and refresh AssetDatabase.Refresh(); EditorApplication.SaveScene(scenePath); }