0
\$\begingroup\$

Maybe using somehow with IPointer[...]Handler interfaces ? Not sure. I have 3 GUI buttons and 3 GameObjects I want to interact with the buttons. The 3 GameObjects are: Robot Kyle , hero , AircraftJet

I have also a canvas and under it as children 3 buttons: Mouse Over , User Call , Allways On

In the Allways On button in the Inspector in the On Click I'm calling a method AllwaysOn and in the Mouse Over button I'm calling a method OnMouseOverMethod

Allways On

Mouse Over

And a script that is attached to the 3 GameObjects:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class OutlinedObject : MonoBehaviour
{
    public Color outlineColor = Color.red;

    //public enum OutlineMode { MouseOver, UserCall, AllwaysOn }
    //public OutlineMode outlineMode = OutlineMode.MouseOver;
    public bool flashing = false;
    [Range(0.5f, 5)]
    public float flashSpeed = 1.5f;
    public float threshold = 1.0f;

    float timeBlink, deltaRate;

    Texture2D maskTexture, maskNone;
    private List<Material> materials = new List<Material>();

    bool ApplyEffect = true;
    bool currentMode = false;
    bool blinkMode = false;

    float andThr;
    Color outAnt;

    private bool mouseOver = false;

    void Awake()
    {

        materials.Clear();
        maskTexture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
        outlineColor.a = 1.0f;
        maskTexture.SetPixel(0, 0, outlineColor);
        maskTexture.SetPixel(1, 0, outlineColor);
        maskTexture.SetPixel(0, 1, outlineColor);
        maskTexture.SetPixel(1, 1, outlineColor);
        maskTexture.Apply();

        foreach (Renderer rend in GetComponentsInChildren<Renderer>())
            materials.Add(rend.material);

        andThr = threshold;
        outAnt = outlineColor;

        maskNone = new Texture2D(2, 2, TextureFormat.ARGB32, false);
        maskNone.SetPixel(0, 0, new Color(0, 0, 0, 0));
        maskNone.SetPixel(1, 0, new Color(0, 0, 0, 0));
        maskNone.SetPixel(0, 1, new Color(0, 0, 0, 0));
        maskNone.SetPixel(1, 1, new Color(0, 0, 0, 0));
        maskNone.Apply();

        AllwaysOn();
        /*if (outlineMode == OutlineMode.AllwaysOn)
            ApplyEffect = true;
        else
            ApplyEffect = false;*/

    }

    void OnEnable()
    {
        if (ApplyEffect)
        {
            foreach (Material material in materials)
                material.SetTexture("_SpriteMask", maskTexture);
        }
        else
        {
            foreach (Material material in materials)
                material.SetTexture("_SpriteMask", maskNone);
        }
        foreach (Material material in materials)
            material.SetFloat("_Threshold", threshold);
    }

    public void OnMouseEnter()
    {
        if (mouseOver == true)
            ApplyEffect = true;

        //if (outlineMode == OutlineMode.MouseOver)
        //  ApplyEffect = true;
    }

    public void UserCall(bool isOn)
    {
        ApplyEffect = isOn;
    }

    public void OnMouseExit()
    {
        if (mouseOver == true)
            ApplyEffect = false;

        //if (outlineMode == OutlineMode.MouseOver)
        //  ApplyEffect = false;
    }

    void LightOn()
    {
        if (currentMode)
            return;
        currentMode = true;
        foreach (Material material in materials)
            material.SetTexture("_SpriteMask", maskTexture);
    }

    void LightOff()
    {
        if (!currentMode)
            return;
        currentMode = false;
        foreach (Material material in materials)
            material.SetTexture("_SpriteMask", maskNone);
    }

    void Update()
    {
        if (flashing && ApplyEffect)
        {
            deltaRate = 1f / (2f * flashSpeed);

            if (Time.time - timeBlink > deltaRate)
            {
                timeBlink = Time.time;
                blinkMode = !blinkMode;
                if (!blinkMode)
                    LightOff();
                else
                    LightOn();
            }
        }
        else
        {
            if (ApplyEffect)
                LightOn();
            else
                LightOff();
        }

        if (andThr != threshold)
        {
            andThr = threshold;
            foreach (Material material in materials)
                material.SetFloat("_Threshold", threshold);
        }

        if (!outAnt.Equals(outlineColor))
        {
            outlineColor.a = 1.0f;
            outAnt = outlineColor;
            maskTexture.SetPixel(0, 0, outlineColor);
            maskTexture.SetPixel(1, 0, outlineColor);
            maskTexture.SetPixel(0, 1, outlineColor);
            maskTexture.SetPixel(1, 1, outlineColor);
            maskTexture.Apply();
            foreach (Material material in materials)
                material.SetTexture("_SpriteMask", maskTexture);
        }
    }

    void OnDisable()
    {
        foreach (Material material in materials)
            material.SetTexture("_SpriteMask", null);
    }

    public void AllwaysOn()
    {
        mouseOver = false;
        ApplyEffect = !ApplyEffect;
    }

    public void OnMouseOverMethod()
    {
        mouseOver = true;
        ApplyEffect = false;
    }
}

On the script bottom there is the two methods I'm using in the buttons On Click events.

In this case I dragged over to the On Click in both buttons Allways On and Mouse Over the AircraftJet

Now when I'm clicking the buttons it's applying it only for the AircraftJet. And I want now to make that each GameObject for example the AircraftJet will have it's own buttons events something like enum.

In the script I used enum before but now I want to use canvas and gui buttons. The problem is that there is no gui enum.

In general what I want to do is somehow that I will be able to click on something and select for example the AircraftJet gameobject and then when clicking the buttons it will effect only the aircraft and if i will click on the hero and then the buttons it will effect only the hero. Something like enum idea.

\$\endgroup\$
1
\$\begingroup\$

Your button code should not be storing the states for your objects. Your objects should be storing the states for your objects (all the button code is get the currently selected object, get the data script, and modify the data).

So the script you have shown should be attached to the camera (or some other neutral object) that knows one thing:

  • what object is selected

Then your update function does 1 thing:

  • determine if a different object should be selected

Your AlwaysOn / MouseOver / Use Call buttons do one thing:

  • take the selected object, call GetComponent<>() (for a custom script), and modify the value contained within that script
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.