示例#1
0
        //ref: https://forum.unity3d.com/threads/getting-original-size-of-texture-asset-in-pixels.165295/
        public static void GetOriginalTextureSize(TextureImporter importer, ref int width, ref int height)
        {
            if (getWidthAndHeightDelegate == null)
            {
                var method = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
                getWidthAndHeightDelegate = Delegate.CreateDelegate(typeof(GetWidthAndHeight), null, method) as GetWidthAndHeight;
            }

            getWidthAndHeightDelegate(importer, ref width, ref height);
        }
示例#2
0
    /// <summary>
    /// Gets the original texture's size as Unit imports it.
    /// </summary>
    /// <returns>
    /// The original texture size.
    /// </returns>
    /// <param name='importer'>
    /// Importer object
    /// </param>
    public static Vector2 GetOriginalTextureSize(TextureImporter importer)
    {
        if (getWidthAndHeightDelegate == null)
        {
            MethodInfo method = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
            getWidthAndHeightDelegate = Delegate.CreateDelegate(typeof(GetWidthAndHeight), null, method) as GetWidthAndHeight;
        }

        Vector2 vec = new Vector2();

        getWidthAndHeightDelegate(importer, ref vec.x, ref vec.y);

        return(vec);
    }
        private void SetSprites(Character c, Texture2D skin, Texture2D outline, int characterOrderIndex)
        {
            // Grab all the sprites out of this texture
            Sprite[] skinSprites    = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(skin)).OfType <Sprite>().ToArray();
            Sprite[] outlineSprites = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(outline)).OfType <Sprite>().ToArray();

            Dictionary <int, CharacterSprite> spritesAtFrame = new Dictionary <int, CharacterSprite>();

            for (int i = 0; i < skinSprites.Length; i++)
            {
                Sprite s = skinSprites[i];

                int textureWidth  = s.texture.width;
                int textureHeight = s.texture.height;

                // In case we are using a texture atlas, find the real original texture size
                if (s.rect != s.textureRect)
                {
                    if (getWidthAndHeightDelegate == null)
                    {
                        var method = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
                        getWidthAndHeightDelegate = Delegate.CreateDelegate(typeof(GetWidthAndHeight), null, method) as GetWidthAndHeight;
                    }

                    string path     = AssetDatabase.GetAssetPath(s);
                    var    importer = AssetImporter.GetAtPath(path) as TextureImporter;
                    if (importer != null)
                    {
                        getWidthAndHeightDelegate(importer, ref textureWidth, ref textureHeight);
                    }
                }

                // Set the sprite information
                int x     = (int)(s.rect.x / s.rect.height);
                int y     = (int)((textureHeight - (s.rect.y + s.rect.height)) / s.rect.width);
                int index = (int)(y * (textureWidth / s.rect.width)) + x;
                spritesAtFrame[index] = new CharacterSprite {
                    Skin = s, Outline = outlineSprites[i]
                };
            }

            bool hasWeapon = false;
            bool hasShield = false;

            // Loop through all the transforms so we don't miss a disabled sprite renderer component
            Transform[] objects = c.GetComponentsInChildren <Transform>(/*includeInactive=*/ true);
            for (int i = 0; i < objects.Length; i++)
            {
                GameObject go = objects[i].gameObject;

                // Determine the sprite type based on the name
                bool   isSkin   = true;
                string partName = go.name;
                if (go.name.Contains("Sprite"))
                {
                    isSkin = !go.name.EndsWith("Sprite-Outline");
                    if (isSkin)
                    {
                        partName = go.name.Replace("Sprite", "");
                    }
                    else
                    {
                        partName = go.name.Replace("Sprite-Outline", "");
                    }
                }
                else if (go.name.EndsWith("Mask"))
                {
                    partName = go.name;
                    isSkin   = true;
                }
                else
                {
                    continue;
                }

                // Update the sprite with the correct one from the skin texture
                if (skinParts.ContainsKey(partName))
                {
                    SkinPart part = skinParts[partName];
                    Renderer r    = go.GetComponent <Renderer>();

                    int index = part.TextureIndex;
                    if (spritesAtFrame.ContainsKey(index))
                    {
                        // Set the correct sprite
                        Sprite         sprite = (isSkin ? spritesAtFrame[index].Skin : spritesAtFrame[index].Outline);
                        SpriteRenderer sr     = go.GetComponent <SpriteRenderer>();
                        if (sr == null)
                        {
                            SpriteMask sm = go.GetComponent <SpriteMask>();
                            sm.sprite = sprite;
                            r.enabled = false;
                        }
                        else
                        {
                            sr.sprite = sprite;
                            r.enabled = true;
                        }

                        // Hide the special sprites that get turned on only in animations
                        if (go.name.Contains("Back") || go.name.Contains("Dead") || go.name.Contains("FX"))
                        {
                            r.gameObject.SetActive(false);
                        }

                        // Update the outline sprite order
                        if (!isSkin)
                        {
                            r.sortingOrder = -100;
                        }
                    }
                    else
                    {
                        // Hide this renderer
                        SpriteRenderer sr = go.GetComponent <SpriteRenderer>();
                        if (sr == null)
                        {
                            SpriteMask sm = go.GetComponent <SpriteMask>();
                            sm.sprite = null;
                        }
                        else
                        {
                            sr.sprite = null;
                        }
                        r.enabled = false;
                    }

                    // Create the weapon collider shape if we find one
                    if (partName == "HeldItemMainCollider" && isSkin)
                    {
                        SpriteRenderer sr = go.GetComponent <SpriteRenderer>();
                        if (sr.sprite != null)
                        {
                            hasWeapon = true;
                        }

                        PolygonCollider2D pc = go.GetComponent <PolygonCollider2D>();
                        if (pc != null)
                        {
                            if (sr.sprite != null)
                            {
                                // Creating a new component will auto set the collider to the shape of the sprite
                                PolygonCollider2D newShape = sr.gameObject.AddComponent <PolygonCollider2D>();

                                // Copy the points to the original collider so we don't make a new component and mess up prefab hierarchy
                                pc.SetPath(0, newShape.points);
                                pc.enabled     = true;
                                pc.isTrigger   = true;
                                sr.enabled     = false;
                                c.WeaponObject = pc;
                                DestroyImmediate(newShape);
                            }
                            else
                            {
                                pc.enabled = false;
                            }
                        }
                    }
                }
            }

            // Change the character if we don't find a weapon sprite
            if (!hasWeapon)
            {
                c.EquippedWeaponType  = Character.WeaponType.None;
                c.ThrowMainProjectile = null;
            }

            // Update based on the shield
            c.IsBlockEnabled = hasShield;

            // Set the tag
            c.gameObject.tag = "Player";
        }