4 or 5 Making a button transparent

Did a search, how would you make an invisible button (child of a Canvas)?

I would like confirmation of the best solution.

As already mentioned - DO NOT use transparent images or similar tricks. A fully transparent image still renders at exactly the same cost as a regular one, so it’s a very bad idea.

Improving on the answer by SignalZak, there are 2 options. For both of them, use the EventTrigger component to handle events.

  • the lazy one, no coding required: Create a Text object with no text and set the rectangle to the desired size.

  • the proper one (just a few lines of code): Create a custom UI control, derived from UnityEngine.UI.Graphic, and override Raycast to always return true (testing against the rect bounds was done already), and also OnFillVBO to do nothing (the default implementation creates a white opaque quad to fill the rectangle).

     public class Touchable : Graphic
     {
         public override bool Raycast(Vector2 sp, Camera eventCamera)
         {
             //return base.Raycast(sp, eventCamera);
             return true;
         }
    
         protected override void OnFillVBO(List<UIVertex> vbo)
         {
             //base.OnFillVBO(vbo);
         }
     }
    

    [CustomEditor(typeof(Touchable))]
    public class TouchableEditor : Editor
    {
    public override void OnInspectorGUI()
    {
    // nothing
    }
    }

We ran into the same problem, how to provide a touchable, invisible area. We tried a fully-alpha’d image with no sprite attached, but you still get the overdraw (which is an issue on handhelds).

We tried implementing various UnityEngine.UI & EventSystem interfaces to see how the touch detection worked (and inheriting from various UI components) before settling on creating a Touchable component that inherits from Text (since Text detects touch outside of its rendered area and has no overdraw).

This script is called Touchable.cs Quite simply,

  1. make a Button in the normal way

  2. simply eliminate the “text” object which comes below a Button as standard

  3. simply delete the “Image” which comes on a Button as standard

  4. Simply drop this script, Touchable.cs, on to the button - you’re done.

You now have a perfect invisible button. Also, say you have perhaps a compicated image or panel. You can drop an invisible button like this “inside” it, and just set the RectTransform to expand all ways. It then “makes that thing become” a perfect button. This is exactly how you can make any unusual or dynamic object in to a button.

// Touchable component

using UnityEngine;
using UnityEngine.UI;

public class Touchable : Text
{
	protected override void Awake()
	{
		base.Awake();
	}
}

// Touchable_Editor component, to prevent treating the component as a Text object.

using UnityEditor;

[CustomEditor(typeof(Touchable))]
public class Touchable_Editor : Editor
{
	public override void OnInspectorGUI ()
	{
		// Do nothing
	}
}

That’s the whole script. There’s only one script (there’s no separate Editor script or anything like that.)

The answers above are close, but not working because the button DOES NOT WORK once you delete or take out the invisible button.

Steps to having a WORKING invisible button:

  1. Create the button as a UI Button
  2. Delete the Text property that the button has assigned to it
  3. Go to the Button’s “Image (Script)”, change the sprite to UIMask. UIMask is used as a placeholder to hide elements but they will still be there

I hope this helps sum things up.

Simply attach a button component to an empty GameObject

Rendering transparent items is expensive. If the button is to be invisible do away with the image component altogether.

Remember, UI elements are just GameObjects. GameObjects are defined by the attached components. You can combine components in any way you like to produce the behaviour you are after. There is nothing forcing you to stick with the defaults from the Create->UI menu, and in many cases sticking to the default is a bad idea.

Updated @bcristian 's Touchable for Unity 5.3+

// Touchable.cs
using UnityEngine;

namespace UnityEngine.UI {

	public class Touchable : Graphic {
		
		public override bool Raycast(Vector2 sp, Camera eventCamera) {
			return true;
		}

		protected override void OnPopulateMesh(VertexHelper vh) {
			vh.Clear();
		}
	}
}


// TouchableEditor.cs
using UnityEngine;
using UnityEditor;

namespace UnityEngine.UI {

	[CustomEditor(typeof(Touchable))]
	public class TouchableEditor : Editor {

		public override void OnInspectorGUI() {}
	}
}

Currently you can use the editor.
Click on the button image, select color, use the alpha slider to change it.

93907-capture.png

Start from Unity 2018.2, you don’t need to do any walkaround like this any more:
Just Set image’s alpha to 0 and check cullTransparentMesh on CanvasRenderer component.
This leads to an invisible button without any rendering (draw call)