private static void ProcessScene() { GameObject[] allObjects = GameObject.FindObjectsOfType <GameObject>(); List <SVGBasicAtlas> unexportedAtlases = new List <SVGBasicAtlas>(); // scan all game objects in the current scene, and keep track of used atlas generators foreach (GameObject gameObj in allObjects) { if (gameObj.activeInHierarchy) { SVGSpriteLoaderBehaviour loader = gameObj.GetComponent <SVGSpriteLoaderBehaviour>(); if (loader != null) { // if this atlas has not been already flagged, lets keep track of it if (!loader.Atlas.Exporting) { unexportedAtlases.Add(loader.Atlas); loader.Atlas.Exporting = true; } } SVGUISpriteLoaderBehaviour uiLoader = gameObj.GetComponent <SVGUISpriteLoaderBehaviour>(); if (uiLoader != null) { // if this atlas has not been already flagged, lets keep track of it if (!uiLoader.UIAtlas.Exporting) { unexportedAtlases.Add(uiLoader.UIAtlas); uiLoader.UIAtlas.Exporting = true; } } SVGCanvasBehaviour uiCanvas = gameObj.GetComponent <SVGCanvasBehaviour>(); if (uiCanvas != null) { // if this atlas has not been already flagged, lets keep track of it if (!uiCanvas.UIAtlas.Exporting) { unexportedAtlases.Add(uiCanvas.UIAtlas); uiCanvas.UIAtlas.Exporting = true; } } } } foreach (SVGBasicAtlas baseAtlas in unexportedAtlases) { SVGBuildProcessor.ProcessAtlas(baseAtlas); // keep track of this atlas in the global list SVGBuildProcessor.m_Atlases.Add(baseAtlas); } }
// get all game objects with an attached SVGUISpriteLoaderBehaviour component that refers a specified sprite asset public void GetSpritesInstances(List <GameObject> spritesInstances, SVGSpriteRef spriteRef) { GameObject[] allObjects = GameObject.FindObjectsOfType <GameObject>(); foreach (GameObject gameObj in allObjects) { // check if the game object is an "SVG sprite" instance of this atlas generator if (gameObj.activeInHierarchy) { SVGUISpriteLoaderBehaviour loader = gameObj.GetComponent <SVGUISpriteLoaderBehaviour>(); // we must be sure that the loader component must refer to this atlas if ((loader != null) && (loader.UIAtlas == this) && (loader.SpriteReference.Equals(spriteRef))) { // add this instance to the output lists spritesInstances.Add(gameObj); } } } }
// border editing callback private void OnSpriteEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder) { SVGUISpriteLoaderBehaviour spriteLoader = target as SVGUISpriteLoaderBehaviour; if ((spriteLoader != null) && (result == PivotEditingResult.Ok)) { SVGUIAtlas uiAtlas = spriteLoader.UIAtlas; if (uiAtlas != null) { // assign the new border uiAtlas.UpdateBorder(spriteAsset, editedPivot, editedBorder); SVGUtils.MarkObjectDirty(uiAtlas); Image uiImage = spriteLoader.GetComponent <Image>(); if (uiImage != null) { // the Image component does not recognize the change of sprite border, so in order to refresh // instantiated objects we have to unset-set the sprite property uiImage.sprite = null; uiImage.sprite = spriteAsset.SpriteData.Sprite; } } } }
private GameObject InstantiateWidget(Canvas canvas, SVGSpriteAssetFile spriteAsset, SVGUIWidgetType widgetType) { Image uiImage; InputField inputField = null; SVGSpriteRef spriteRef = spriteAsset.SpriteRef; SVGSpriteData spriteData = spriteAsset.SpriteData; GameObject gameObj = new GameObject(); gameObj.layer = LayerMask.NameToLayer("UI"); // add Image component; NB: it will add a RectTransform component too uiImage = gameObj.AddComponent <Image>(); uiImage.fillCenter = true; uiImage.material = null; uiImage.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); uiImage.sprite = spriteData.Sprite; switch (widgetType) { case SVGUIWidgetType.Button: gameObj.AddComponent <Button>(); break; case SVGUIWidgetType.InputField: inputField = gameObj.AddComponent <InputField>(); break; default: break; } // get RectTransform component and set size according to the associated sprite RectTransform rectTransform = gameObj.GetComponent <RectTransform>(); rectTransform.SetParent(canvas.transform); // attach SVGUISpriteLoaderBehaviour component SVGUISpriteLoaderBehaviour spriteLoader = (SVGUISpriteLoaderBehaviour)gameObj.AddComponent <SVGUISpriteLoaderBehaviour>(); spriteLoader.UIAtlas = this; spriteLoader.SpriteReference = spriteRef; spriteLoader.ResizeOnStart = true; // anchor presets if (widgetType == SVGUIWidgetType.Panel) { rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.one; rectTransform.localPosition = Vector3.zero; rectTransform.sizeDelta = Vector2.zero; } else { rectTransform.anchorMin = new Vector2(0.5f, 0.5f); rectTransform.anchorMax = new Vector2(0.5f, 0.5f); rectTransform.sizeDelta = new Vector2(spriteData.Sprite.rect.width, spriteData.Sprite.rect.height); } // set image type uiImage.type = (spriteData.Sprite.border != Vector4.zero) ? Image.Type.Sliced : Image.Type.Simple; if (widgetType == SVGUIWidgetType.InputField) { this.PopulateInputField(canvas, gameObj, inputField); } gameObj.name = spriteData.Sprite.name; gameObj.SetActive(true); return(gameObj); }
private void UpdateEditorSprites(float newScale) { // get the list of instantiated SVG sprites List <GameObject> spritesInstances = new List <GameObject>(); this.GetSpritesInstances(spritesInstances); // regenerate the list of sprite locations this.m_GeneratedSpritesLists = new SVGSpritesListDictionary(); if (this.m_SvgList.Count <= 0) { AssetDatabase.StartAssetEditing(); // delete previously generated textures (i.e. get all this.GeneratedTextures entries and delete the relative files) this.DeleteTextures(); // delete previously generated sprites (i.e. get all this.GeneratedSprites entries and delete the relative files) this.DeleteSprites(); if (spritesInstances.Count > 0) { bool remove = EditorUtility.DisplayDialog("Missing sprite!", string.Format("{0} gameobjects reference sprites that do not exist anymore. Would you like to remove them from the scene?", spritesInstances.Count), "Remove", "Keep"); if (remove) { this.DeleteGameObjects(spritesInstances); } } AssetDatabase.StopAssetEditing(); // input SVG list is empty, simply reset both hash this.m_SvgListHashOld = ""; this.m_SvgListHashCurrent = ""; return; } // generate textures and sprites List <Texture2D> textures = new List <Texture2D>(); List <KeyValuePair <SVGSpriteRef, SVGSpriteData> > sprites = new List <KeyValuePair <SVGSpriteRef, SVGSpriteData> >(); if (SVGRuntimeGenerator.GenerateSprites(// input this.m_SvgList, this.m_MaxTexturesDimension, this.m_SpritesBorder, this.m_Pow2Textures, newScale, this.m_ClearColor, this.m_FastUpload, this.m_GeneratedSpritesFiles, // output textures, sprites, this.m_GeneratedSpritesLists)) { int i, j; // keep track of the new generation scale this.m_EditorGenerationScale = newScale; AssetDatabase.StartAssetEditing(); // delete previously generated textures (i.e. get all this.GeneratedTextures entries and delete the relative files) this.DeleteTextures(); // delete previously generated sprites (i.e. get all this.GeneratedSprites entries and delete the relative files) this.DeleteSprites(); // ensure the presence of needed subdirectories string atlasesPath = this.CreateOutputFolders(); string texturesDir = atlasesPath + "/Textures/"; string spritesDir = atlasesPath + "/Sprites/"; // save new texture assets i = 0; foreach (Texture2D texture in textures) { string textureFileName = texturesDir + "texture" + i + ".asset"; // save texture AssetDatabase.CreateAsset(texture, textureFileName); // keep track of the saved texture this.m_GeneratedTexturesFiles.Add(new AssetFile(textureFileName, texture)); i++; } // save sprite assets j = sprites.Count; for (i = 0; i < j; ++i) { // get sprite reference and its pivot SVGSpriteRef spriteRef = sprites[i].Key; SVGSpriteData spriteData = sprites[i].Value; // build sprite file name string spriteFileName = spritesDir + spriteData.Sprite.name + ".asset"; // save sprite asset AssetDatabase.CreateAsset(spriteData.Sprite, spriteFileName); // keep track of the saved sprite and its pivot this.m_GeneratedSpritesFiles.Add(spriteRef, new SVGSpriteAssetFile(spriteFileName, spriteRef, spriteData)); } AssetDatabase.StopAssetEditing(); // for already instantiated (SVG) game object, set the new sprites // in the same loop we keep track of those game objects that reference missing sprites (i.e. sprites that do not exist anymore) List <GameObject> missingSpriteObjs = new List <GameObject>(); foreach (GameObject gameObj in spritesInstances) { SVGSpriteAssetFile spriteAsset; SVGUISpriteLoaderBehaviour spriteLoader = (SVGUISpriteLoaderBehaviour)gameObj.GetComponent <SVGUISpriteLoaderBehaviour>(); if (spriteLoader.SpriteReference.TxtAsset != null) { if (this.m_GeneratedSpritesFiles.TryGetValue(spriteLoader.SpriteReference, out spriteAsset)) { // link the new sprite to the renderer Image image = (Image)gameObj.GetComponent <Image>(); if (image != null) { SVGSpriteData spriteData = spriteAsset.SpriteData; // assign the new sprite image.sprite = spriteData.Sprite; gameObj.GetComponent <RectTransform>().sizeDelta = new Vector2(spriteData.Sprite.rect.width, spriteData.Sprite.rect.height); } } else { missingSpriteObjs.Add(gameObj); } } } if (missingSpriteObjs.Count > 0) { bool remove = EditorUtility.DisplayDialog("Missing sprite!", string.Format("{0} gameobjects reference sprites that do not exist anymore. Would you like to remove them from the scene?", missingSpriteObjs.Count), "Remove", "Keep"); if (remove) { this.DeleteGameObjects(missingSpriteObjs); } } // now SVG documents are instantiable foreach (SVGAssetInput svgAsset in this.m_SvgList) { svgAsset.Instantiable = true; } // keep track of the new hash this.m_SvgListHashOld = this.m_SvgListHashCurrent; } }