Пример #1
0
        /// <summary>
        /// Creates an OP2Animation object.
        /// Initializes automatically.
        /// </summary>
        public static OP2Animation_UGUI Create(OP2UtilityDotNet.Sprite.Animation op2Animation, string name = "OP2Animation")
        {
            GameObject    root          = new GameObject(name, typeof(RectTransform));
            RectTransform rectTransform = root.GetComponent <RectTransform>();

            rectTransform.anchorMin = new Vector2(0, 1);
            rectTransform.anchorMax = new Vector2(0, 1);
            rectTransform.pivot     = new Vector2(0, 1);

            OP2Animation_UGUI animation = root.AddComponent <OP2Animation_UGUI>();

            animation._CreateFromSerializedFields = false;
            animation.Initialize(op2Animation);

            return(animation);
        }
Пример #2
0
        private void Release()
        {
            // Destroy layers
            foreach (Image renderImage in _RenderLayers)
            {
                Destroy(renderImage.gameObject);
            }

            _OP2Animation = null;
            _RenderLayers = null;

            _CurrentFrame = 0;
            _CurrentTime  = 0;

            enabled = false;             // Disable update
        }
Пример #3
0
        private void Start()
        {
            // Disable update if animation has not been initialized
            if (_OP2Animation == null)
            {
                enabled = false;

                if (_CreateFromSerializedFields)
                {
                    // Initialize animation
                    OP2UtilityDotNet.Sprite.Animation animationData = AssetManager.GetAnimation(_AnimationIndex);
                    if (animationData != null)
                    {
                        Initialize(animationData);
                    }
                    else
                    {
                        Debug.LogWarning("Animation data not found for index: " + _AnimationIndex);
                    }
                }
            }
        }
Пример #4
0
        private void Initialize(OP2UtilityDotNet.Sprite.Animation op2Animation)
        {
            if (_OP2Animation != null)
            {
                Release();
            }

            _OP2Animation = op2Animation;

            // Get max layers for animation
            int maxLayers = _OP2Animation.frames.Max((frame) => frame.layers.Count);

            // Add layers
            _RenderLayers = new Image[maxLayers];

            for (int i = 0; i < maxLayers; ++i)
            {
                GameObject goLayer = new GameObject("Layer" + i);
                Image      image   = goLayer.AddComponent <Image>();

                image.rectTransform.SetParent(transform);

                image.rectTransform.localScale    = Vector3.one;
                image.rectTransform.localRotation = Quaternion.identity;

                image.rectTransform.anchorMin        = new Vector2(0, 1);
                image.rectTransform.anchorMax        = new Vector2(0, 1);
                image.rectTransform.pivot            = new Vector2(0, 1);
                image.rectTransform.anchoredPosition = Vector2.zero;

                _RenderLayers[i] = image;
            }

            enabled = true;

            // Show first frame
            SetFrame(0);
        }
Пример #5
0
        private void Awake()
        {
            // Initialize animation
            OP2UtilityDotNet.Sprite.Animation animationData = AssetManager.GetAnimation(_AnimationIndex);
            if (animationData == null)
            {
                Debug.LogWarning("Animation data not found for index: " + _AnimationIndex);
                return;
            }

            _OP2Animation = animationData;

            // Create toggle
            _ToggleButton       = gameObject.AddComponent <Toggle>();
            _ToggleButton.group = _Group;

            // Background image
            //GameObject goBackground = new GameObject("Background");
            //goBackground.transform.SetParent(transform);
            Image backgroundImage = gameObject.GetComponent <Image>();

            if (backgroundImage == null)
            {
                backgroundImage = gameObject.AddComponent <Image>();
            }
            backgroundImage.sprite = GetSpriteForFrame(0);
            //backgroundImage.rectTransform.anchoredPosition = Vector2.zero;
            backgroundImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, backgroundImage.sprite.rect.width);
            backgroundImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, backgroundImage.sprite.rect.height);
            _ToggleButton.targetGraphic = backgroundImage;

            _ToggleButton.transition       = Selectable.Transition.SpriteSwap;
            _ToggleButton.toggleTransition = Toggle.ToggleTransition.None;

            SpriteState state = new SpriteState();

            state.highlightedSprite = backgroundImage.sprite;
            state.pressedSprite     = backgroundImage.sprite;
            state.selectedSprite    = backgroundImage.sprite;
            state.disabledSprite    = GetSpriteForFrame(2);

            _ToggleButton.spriteState = state;

            // Checkmark image
            GameObject goCheckmark = new GameObject("Checkmark");

            goCheckmark.transform.SetParent(transform);
            Image checkImage = goCheckmark.AddComponent <Image>();

            checkImage.sprite = GetSpriteForFrame(1);
            checkImage.rectTransform.anchoredPosition = Vector2.zero;
            checkImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, checkImage.sprite.rect.width);
            checkImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, checkImage.sprite.rect.height);
            _ToggleButton.graphic = checkImage;

            // Set toggle button size to match image size
            GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, backgroundImage.sprite.rect.width);
            GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, backgroundImage.sprite.rect.height);

            // No update for toggle buttons
            enabled = false;
        }