Exemplo n.º 1
0
        public void Create()
        {
            // Error handling
            if (Source == null)
            {
                Debug.LogError("Please specify a Source Texture2D.");
                return;
            }

            if (AsciiMappings.Count < 1)
            {
                Debug.LogError("Please map at least one glyph to an ASCII value.");
                return;
            }

            // Get starting directory
            var defaultPath = AssetDatabase.GetAssetPath(Source);

            // Get material path
            var path = EditorUtility.SaveFilePanel("Save Material", defaultPath, Source.name, "mat");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            path = "Assets" + path.Substring(Mathf.Clamp(Application.dataPath.Length, 0, int.MaxValue));

            // Create material
            var material = FontUtility.Texture2DToMaterial(Source);

            // Save material
            AssetDatabase.CreateAsset(material, path);

            // Get font path
            path = EditorUtility.SaveFilePanel("Save Font", path, Source.name, "fontsettings");
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            path = "Assets" + path.Substring(Application.dataPath.Length);

            // Get character count
            var charCount = Mathf.Min(AsciiMappings.Count, CharactersPerRow * CharactersPerColumn);
            var charData  = new CharacterInfo[charCount];

            // Create character info
            for (var i = 0; i < charCount; ++i)
            {
                var col = i % CharactersPerColumn;
                var row = i / CharactersPerColumn;

                charData[i] = new CharacterInfo()
                {
                    index   = (int)AsciiMappings[i],
                    advance = CharacterPixelWidth,

                    uvTopLeft     = new Vector2(col / (float)CharactersPerRow, 1.0f - row / (float)CharactersPerColumn),
                    uvTopRight    = new Vector2((col + 1) / (float)CharactersPerRow, 1.0f - row / (float)CharactersPerColumn),
                    uvBottomLeft  = new Vector2(col / (float)CharactersPerRow, 1.0f - (row + 1) / (float)CharactersPerColumn),
                    uvBottomRight = new Vector2((col + 1) / (float)CharactersPerRow, 1.0f - (row + 1) / (float)CharactersPerColumn),

                    minX = 0,
                    maxX = CharacterPixelWidth,
                    minY = -CharacterPixelHeight,
                    maxY = 0,
                };
            }

            // Create font
            var font = new Font()
            {
                material      = material,
                name          = Path.GetFileNameWithoutExtension(path),
                characterInfo = charData,
            };

            // Save and refresh
            AssetDatabase.CreateAsset(font, path);
            AssetDatabase.Refresh();
        }
Exemplo n.º 2
0
        public void Create(Sprite[] sprites)
        {
            if (sprites == null)
            {
                return;
            }

            // Get last numbers in all sprite names
            var asciiStrings = sprites.Select(sprite =>
                                              Regex.Match(sprite.name, @"\d+", RegexOptions.RightToLeft).Value).ToArray();


            var success = true;
            var ascii   = new int[sprites.Length];

            var commonIndex = 0;

            for (var i = 0; i < sprites.Length; ++i)
            {
                int check;
                if (int.TryParse(asciiStrings[i], out check) && check >= 0)
                {
                    // Error detection - if a name has its number in a different position compared to others,
                    // a naming error may have occurred
                    if (i == 0)
                    {
                        commonIndex = sprites[0].name.LastIndexOf(asciiStrings[i], StringComparison.Ordinal);
                    }
                    else
                    {
                        var asciiIndex = sprites[i].name.LastIndexOf(asciiStrings[i], StringComparison.Ordinal);
                        if (asciiIndex != commonIndex)
                        {
                            Debug.LogWarning(string.Format("Possible naming error: " +
                                                           "\"{0}\" vs \"{1}\".", sprites[0].name, sprites[i].name));
                        }
                    }

                    ascii[i] = check;
                    continue;
                }

                Debug.LogError(string.Format("No valid number was found for sprite \"{0}\".", sprites[i].name));
                success = false;
            }

            if (!success)
            {
                Debug.LogError("The font was not created.");
                return;
            }

            if (!ascii.Contains(32))
            {
                Debug.LogWarning("You may want to create a sprite for spaces. The ASCII value is 32.");
            }

            // Get starting directory
            var defaultPath = SourcePath;

            // Get material path
            var path = EditorUtility.SaveFilePanel("Save Material", defaultPath,
                                                   Path.GetFileNameWithoutExtension(SourcePath), "mat");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            path = "Assets" + path.Substring(Mathf.Max(Application.dataPath.Length, 0));

            // Create material
            var material = AssetDatabase.LoadAssetAtPath <Material>(path);
            var exists   = material != null;

            if (!exists)
            {
                material = new Material(Shader.Find("UI/Default"));
            }

            material.mainTexture = SourceSprite.texture;

            if (!exists)
            {
                AssetDatabase.CreateAsset(material, path);
            }

            // Get font path
            path = EditorUtility.SaveFilePanel("Save Font", path,
                                               Path.GetFileNameWithoutExtension(path), "fontsettings");
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            path = "Assets" + path.Substring(Application.dataPath.Length);

            // Create character info
            var charData = new CharacterInfo[ascii.Length];

            for (var i = 0; i < ascii.Length; ++i)
            {
                var sprite = sprites[i];
                charData[i] = FontUtility.Splice(sprites[i].texture, sprite.rect,
                                                 new Rect(new Vector2(), new Vector2(sprite.rect.size.x * Scale.x, sprite.rect.size.y * Scale.y)),
                                                 ascii[i]);
            }

            // Create font
            var font = AssetDatabase.LoadAssetAtPath <Font>(path);

            exists = font != null;

            if (!exists)
            {
                font = new Font();
            }

            font.material      = material;
            font.name          = Path.GetFileNameWithoutExtension(path);
            font.characterInfo = charData;

            FontUtility.SetLineSpacing(font, sprites[0].rect.height * Scale.y);

            // Save and refresh
            if (!exists)
            {
                AssetDatabase.CreateAsset(font, path);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }