예제 #1
0
    private void Start()
    {
        killButtonManager = UnityEngine.Object.Instantiate(hudManager.KillButton, hudManager.transform);
        startColorButton  = killButtonManager.renderer.color;
        startColorText    = killButtonManager.TimerText.Color;
        killButtonManager.gameObject.SetActive(true);
        killButtonManager.renderer.enabled = true;
        Texture2D tex      = GUIExtensions.CreateEmptyTexture();
        Assembly  assembly = Assembly.GetExecutingAssembly();
        Stream    myStream = assembly.GetManifestResourceStream(ResourceName);

        byte[] buttonTexture = Reactor.Extensions.Extensions.ReadFully(myStream);
        ImageConversion.LoadImage(tex, buttonTexture, false);
        killButtonManager.renderer.sprite = GUIExtensions.CreateSprite(tex);
        PassiveButton button = killButtonManager.GetComponent <PassiveButton>();

        button.OnClick.RemoveAllListeners();
        button.OnClick.AddListener((UnityEngine.Events.UnityAction)listener);
        void listener()
        {
            if (Timer < 0f && canUse)
            {
                killButtonManager.renderer.color = new Color(1f, 1f, 1f, 0.3f);
                if (hasEffectDuration)
                {
                    isEffectActive = true;
                    Timer          = EffectDuration;
                    killButtonManager.TimerText.Color = new Color(0, 255, 0);
                }
                OnClick();
            }
        }
    }
예제 #2
0
        /// <summary>
        /// Call in HudManager.Start
        /// </summary>
        private CooldownButton(Assembly asm, Action onClick, float cooldown, string embeddedResourcePath /*, float pixelsPerUnit*/, Vector2 positionOffset, ButtonCategory category, HudManager hudManager, bool hasEffectDuration, float effectDuration, Action onEffectEnd)
        {
            OnEffectEnd    = onEffectEnd;
            PositionOffset = positionOffset;
            EffectDuration = effectDuration;
            Category       = category;
            //PixelsPerUnit = pixelsPerUnit;
            MaxTimer = cooldown;
            Timer    = MaxTimer;

            Buttons.Add(this);

            string embeddedResourceFullPath = asm.GetManifestResourceNames().FirstOrDefault(resourceName => resourceName.EndsWith(embeddedResourcePath, StringComparison.Ordinal));

            if (string.IsNullOrEmpty(embeddedResourceFullPath))
            {
                throw new ArgumentNullException(nameof(embeddedResourcePath), $"The embedded resource \"{embeddedResourcePath}\" was not found in assembly \"{asm.GetName().Name}\".");
            }

            Stream resourceStream = asm.GetManifestResourceStream(embeddedResourceFullPath);

            KillButtonManager = Object.Instantiate(hudManager.KillButton, hudManager.transform);

            //StartColorButton = killButtonManager.renderer.color;
            StartColorText = KillButtonManager.TimerText.Color;

            KillButtonManager.gameObject.SetActive(true);
            KillButtonManager.renderer.enabled = true;

            Texture2D tex = GUIExtensions.CreateEmptyTexture();

            byte[] buttonTexture = Reactor.Extensions.Extensions.ReadFully(resourceStream);
            ImageConversion.LoadImage(tex, buttonTexture, false);
            KillButtonManager.renderer.sprite = GUIExtensions.CreateSprite(tex);

            PassiveButton button = KillButtonManager.GetComponent <PassiveButton>();

            button.OnClick.RemoveAllListeners();
            button.OnClick.AddListener(new Action(() =>
            {
                if (Timer < 0f && CanUse)
                {
                    KillButtonManager.renderer.color = new Color(1f, 1f, 1f, 0.3f);

                    if (hasEffectDuration)
                    {
                        IsEffectActive = true;

                        Timer = EffectDuration;

                        KillButtonManager.TimerText.Color = new Color(0, 255, 0);
                    }

                    onClick();
                }
            }));
        }
        /// <summary>
        /// Creates an instance of <see cref="KillButtonManager"/> when one does not exist.
        /// </summary>
        private void CreateButton()
        {
            if (KillButtonManager != null || !HudManager.Instance?.KillButton || ImageData == null)
            {
                return;
            }

            KillButtonManager = Object.Instantiate(HudManager.Instance.KillButton, HudManager.Instance.transform);

            KillButtonManager.gameObject.SetActive(HudVisible && Visible);
            KillButtonManager.renderer.enabled = HudVisible && Visible;

            Texture2D tex = GUIExtensions.CreateEmptyTexture();

            ImageConversion.LoadImage(tex, ImageData, false);

            KillButtonManager.renderer.sprite = GUIExtensions.CreateSprite(tex);

            PassiveButton button = KillButtonManager.GetComponent <PassiveButton>();

            button.OnClick.RemoveAllListeners();
            button.OnClick.AddListener(new Action(() =>
            {
                if (!IsUsable)
                {
                    return;
                }

                CancelEventArgs args = new CancelEventArgs();
                OnClick?.SafeInvoke(this, args);

                if (args.Cancel)
                {
                    return;              // Click was cancelled.
                }
                if (HasEffect)
                {
                    StartEffect();

                    return;
                }

                ApplyCooldown();
            }));
        }