Ejemplo n.º 1
0
    // Let's do this thing!:
    void OnWizardCreate()
    {
        if (disablePixelPerfect)
        {
            disablePixelPerfect = EditorUtility.DisplayDialog("Are you sure?", "Are you sure you wish to disable pixel-perfect on all selected sprites?", "Yes", "No");
        }

        // Get our desired sprites:
        FindSprites();

        float worldUnitsPerScreenPixel = (renderCamera.orthographicSize * 2f) / targetScreenHeight;

        // Now set their sizes:
        for (int i = 0; i < sprites.Count; ++i)
        {
            SpriteRoot sprite = (SpriteRoot)sprites[i];
            Vector2    pxSize = sprite.GetDefaultPixelSize(AssetDatabase.GUIDToAssetPath, AssetDatabase.LoadAssetAtPath);

            if (disablePixelPerfect)
            {
                sprite.pixelPerfect = false;
            }

            if (sprite.spriteMesh == null)
            {
                sprite.width  = pxSize.x * worldUnitsPerScreenPixel;
                sprite.height = pxSize.y * worldUnitsPerScreenPixel;
            }
            else
            {
                sprite.SetSize(pxSize.x * worldUnitsPerScreenPixel, pxSize.y * worldUnitsPerScreenPixel);
            }

            EditorUtility.SetDirty(((SpriteRoot)sprites[i]).gameObject);
        }

        // See if we need to advise the user to reload the scene:
        if (applyToAllPrefabs)
        {
            EditorUtility.DisplayDialog("NOTE", "You may need to reload the current scene for prefab instances to reflect your changes.", "OK");
        }

        Debug.Log(sprites.Count + " sprites sized.");

        // Save our settings for next time:
        SaveSettings();
    }
Ejemplo n.º 2
0
    // Let's do this thing!:
    void OnWizardCreate()
    {
        float worldUnitsPerScreenPixel;
        int   skipped = 0;       // How many sprites had to be skipped because they were prefabs?

        if (disablePixelPerfect)
        {
            disablePixelPerfect = EditorUtility.DisplayDialog("Are you sure?", "Are you sure you wish to disable pixel-perfect on all selected sprites?", "Yes", "No");
        }

        // Get our desired sprites:
        FindSprites();


        if (renderCamera.isOrthoGraphic)
        {
            // Use orthographic logic:
            worldUnitsPerScreenPixel = (renderCamera.orthographicSize * 2f) / targetScreenHeight;

            // Now set their sizes:
            for (int i = 0; i < sprites.Count; ++i)
            {
                SpriteRoot sprite = (SpriteRoot)sprites[i];
                Vector2    pxSize = sprite.GetDefaultPixelSize(AssetDatabase.GUIDToAssetPath, AssetDatabase.LoadAssetAtPath);

                if (disablePixelPerfect)
                {
                    sprite.pixelPerfect = false;
                }

                if (sprite.spriteMesh == null)
                {
                    sprite.width  = pxSize.x * worldUnitsPerScreenPixel;
                    sprite.height = pxSize.y * worldUnitsPerScreenPixel;
                }
                else
                {
                    sprite.SetSize(pxSize.x * worldUnitsPerScreenPixel, pxSize.y * worldUnitsPerScreenPixel);
                }

                EditorUtility.SetDirty(((SpriteRoot)sprites[i]).gameObject);
            }
        }
        else
        {
            // Use perspective logic:
            float dist;
            Plane nearPlane;

            // Now set their sizes:
            for (int i = 0; i < sprites.Count; ++i)
            {
#if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9)
        #if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9)
                PrefabType ptype = PrefabUtility.GetPrefabType(((SpriteRoot)sprites[i]).gameObject);
        #else
                PrefabType ptype = EditorUtility.GetPrefabType(((SpriteRoot)sprites[i]).gameObject);
        #endif
                // We can't do prefabs with perspective cameras:
                if (ptype == PrefabType.Prefab || ptype == PrefabType.ModelPrefab)
                {
                    ++skipped;
                    Debug.LogWarning("Sprite \"" + ((SpriteRoot)sprites[i]).name + "\" skipped because it is a prefab and the selected camera is perspective. Size cannot be calculated for perspective cameras without the object actually being positioned in front of the camera.\n Either use an orthographic camera, or use an instance of the prefab in the scene instead.");
                    continue;
                }
#endif
                SpriteRoot sprite = (SpriteRoot)sprites[i];
                nearPlane = new Plane(renderCamera.transform.forward, renderCamera.transform.position);
                Vector2 pxSize = sprite.GetDefaultPixelSize(AssetDatabase.GUIDToAssetPath, AssetDatabase.LoadAssetAtPath);

                // Determine the world distance between two vertical
                // screen pixels for this camera:
                dist = nearPlane.GetDistanceToPoint(sprite.transform.position);
                worldUnitsPerScreenPixel = Vector3.Distance(renderCamera.ScreenToWorldPoint(new Vector3(0, 1, dist)), renderCamera.ScreenToWorldPoint(new Vector3(0, 0, dist)));

                if (disablePixelPerfect)
                {
                    sprite.pixelPerfect = false;
                }

                if (sprite.spriteMesh == null)
                {
                    sprite.width  = pxSize.x * worldUnitsPerScreenPixel;
                    sprite.height = pxSize.y * worldUnitsPerScreenPixel;
                }
                else
                {
                    sprite.SetSize(pxSize.x * worldUnitsPerScreenPixel, pxSize.y * worldUnitsPerScreenPixel);
                }

                EditorUtility.SetDirty(((SpriteRoot)sprites[i]).gameObject);
            }
        }



        // See if we need to advise the user to reload the scene:
        if (applyToAllPrefabs)
        {
            EditorUtility.DisplayDialog("NOTE", "You may need to reload the current scene for prefab instances to reflect your changes.", "OK");
        }

        Debug.Log((sprites.Count - skipped) + " sprites sized.");

        // Save our settings for next time:
        SaveSettings();
    }