Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LANDO · Jul 01, 2012 at 07:14 AM · iosperformanceupdatetexture2dongui

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LANDO · Jul 01, 2012 at 07:21 AM 0
Share

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...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LANDO · Jul 01, 2012 at 10:02 AM 0
Share

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!

avatar image tomekkie2 · Jul 01, 2012 at 10:20 AM 0
Share

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?

avatar image LANDO · Jul 01, 2012 at 10:39 AM 0
Share

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

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers

Texture2d.LoadImage too slow for use (iOS) 1 Answer

iOS builds run poorly until device restarted after update 0 Answers

ios and onGUi performance / alternatives? 1 Answer

Updating iOS app almost doubles size in iTunes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges