- Home /
 
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!
I should clarify that the first block of code is in Update() to detect touch and "do something".
The second block of code is inside OnGUI()...
I just need GetPixel or something similar to work in Update() somehow...
Answer by hijinxbassist · Jul 01, 2012 at 07:36 AM
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.
I was afraid there wouldn't be a solution outside of using OnGUI, sounds like that is the case. Not using update along with on gui is an awesome tip though, I should be able to keep them separate without a problem (or so i think..).
I have something similar to your code working at the moment, I'll keep testing it as I build the app and update this thread if I find anything new out. Thanks for the code snippet and suggestion!
The above code is a standard solution. I have picked the same from somewhere works quite well in the WebPlayer for me. Would that be any reason for that to work any worse on iOS?
I was trying to avoid OnGUI() because I've read it is very expensive to use on iOS.. it's been suggested on a few forums to use it as little as possible.
http://www.$$anonymous$$dthecube.com/blog/2010/09/avoiding-performance-cost-of-ongui
Answer by whydoidoit · Jul 01, 2012 at 11:44 AM
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.
Your answer