iOS OnGUI vs Update for color picker

I’ve made a color picker for an iOS app where the user can tap on a color and the color is passed to a GameObject which is then instantiated, so for instance if the user taps the color blue, a blue cube appears.

It seems that OnGUI() is an expensive call to make with iOS so I’m trying to get my working code into a more performance friendly Update().

The problem I have encountered is if I use a Texture2D instead of a GUITexture, I can’t figure out how to use HitTest like this:

if (Input.touchCount>0)
   {
      for (var touch : Touch in Input.touches)
      {
         if(touch.phase == TouchPhase.Began && gui.HitTest(touch.position))
         {
          	// Just touched the GUITexture
          	frame.renderer.material.color = Color.red;
    		Debug.Log("TouchGUITex");  
         }
      }
   }

If I use a GUITexture, I loose the ability to sample a color and pass it to a game object using GetPixel:

var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
		
cubecol = col;

var cube = Instantiate(blockPrefab, Vector3(0,0,0), Quaternion.identity);
          cube.name = "Cube1_"+cubecnt.ToString();
          cubecnt++;
          cube.renderer.material.color = cubecol;

Not sure, but it seems I may be stuck using OnGUI() along with a Texture2D if I want to keep the ability to use GetPixel, is there any less expensive way of solving this? Thanks!

Im not sure there is much solution above using OnGUI unfortunately… I found a solution that only uses on gui and not both update and ongui(that can get expensive…), the solution i found i use on my GUI script…as i would suggest your structuring to be(gui on a gui script, other stuff on another script…if possible/not always that cut and dry).

if (GUI.RepeatButton (rec,ColorTexture))//ColorTexture is a color bar
{
     var pickpos = Event.current.mousePosition;//Touch Position
     var aaa = pickpos.x-rec.x;
     var bbb = pickpos.y-rec.y;
     ms.curColor = ColorTexture.GetPixel(aaa+4,41-bbb);//offset is custom to your setup

     objInstance.renderer.material.color=ms.curColor;
}

I used this on my Android game and it worked just fine with no lag at all (is was a HUD before level selection, not sure if that matters). Hope this helps a bit, ill look for the forum post i got this from to give credit to the poster.

You definitely don’t want to use OnGUI on IOS - believe me I know from bitter experience.

Why can’t you do a ray cast to see if you hit the object rather than a hit test? Also GUITexture isn’t just OnGUI - it works without that part of the system.