コード例 #1
0
        public static SpriteMesh CreateSpriteMesh(Sprite sprite)
        {
            SpriteMesh     spriteMesh     = SpriteMeshPostprocessor.GetSpriteMeshFromSprite(sprite);
            SpriteMeshData spriteMeshData = null;

            if (!spriteMesh && sprite)
            {
                string spritePath = AssetDatabase.GetAssetPath(sprite);
                string directory  = Path.GetDirectoryName(spritePath);
                string assetPath  = AssetDatabase.GenerateUniqueAssetPath(directory + Path.DirectorySeparatorChar + sprite.name + ".asset");

                spriteMesh = ScriptableObject.CreateInstance <SpriteMesh>();
                InitFromSprite(spriteMesh, sprite);
                AssetDatabase.CreateAsset(spriteMesh, assetPath);

                spriteMeshData           = ScriptableObject.CreateInstance <SpriteMeshData>();
                spriteMeshData.name      = spriteMesh.name + "_Data";
                spriteMeshData.hideFlags = HideFlags.HideInHierarchy;
                InitFromSprite(spriteMeshData, sprite);
                AssetDatabase.AddObjectToAsset(spriteMeshData, assetPath);

                UpdateAssets(spriteMesh, spriteMeshData);

                AssetDatabase.SaveAssets();
                AssetDatabase.ImportAsset(assetPath);

                Selection.activeObject = spriteMesh;
            }

            return(spriteMesh);
        }
コード例 #2
0
        static void ContextCreateSpriteMesh(MenuCommand menuCommand)
        {
            GameObject     spriteRendererGO = Selection.activeGameObject;
            SpriteRenderer spriteRenderer   = null;
            SpriteMesh     spriteMesh       = null;

            int sortingLayerID = 0;
            int sortingOrder   = 0;

            if (spriteRendererGO)
            {
                spriteRenderer = spriteRendererGO.GetComponent <SpriteRenderer>();
            }

            if (spriteRenderer &&
                spriteRenderer.sprite)
            {
                sortingLayerID = spriteRenderer.sortingLayerID;
                sortingOrder   = spriteRenderer.sortingOrder;

                SpriteMesh overrideSpriteMesh = SpriteMeshPostprocessor.GetSpriteMeshFromSprite(spriteRenderer.sprite);

                if (overrideSpriteMesh)
                {
                    spriteMesh = overrideSpriteMesh;
                }
                else
                {
                    spriteMesh = SpriteMeshUtils.CreateSpriteMesh(spriteRenderer.sprite);
                }
            }

            if (spriteMesh)
            {
                Undo.SetCurrentGroupName("create SpriteMeshInstance");
                Undo.DestroyObjectImmediate(spriteRenderer);
                SpriteMeshInstance spriteMeshInstance =
                    SpriteMeshUtils.CreateSpriteMeshInstance(spriteMesh, spriteRendererGO, true);

                spriteMeshInstance.sortingLayerID = sortingLayerID;
                spriteMeshInstance.sortingOrder   = sortingOrder;

                Selection.activeGameObject = spriteRendererGO;
            }
            else
            {
                Debug.Log("Select a SpriteRenderer with a Sprite to convert to SpriteMesh");
            }
        }
コード例 #3
0
        static bool ValidateCreateSpriteMesh(MenuCommand menuCommand)
        {
            bool valid = false;

            Sprite sprite = Selection.activeObject as Sprite;

            if (sprite && !SpriteMeshPostprocessor.GetSpriteMeshFromSprite(sprite))
            {
                valid = true;
            }

            List <Texture2D> selectedTextures = Selection.objects.ToList().Where(o => o is Texture2D).ToList().ConvertAll(o => o as Texture2D);

            valid = valid || selectedTextures.Count > 0;

            return(valid);
        }