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 headkitgames · Nov 05, 2010 at 03:17 PM · iphonetouch

iPhone Touch detection programing difference

hi there!

i wonder if there is a difference between

    if (iPhoneInput.touchCount > 0) {
        touch = iPhoneInput.GetTouch(0);
        pos = touch.position;
        var ray : Ray = Camera.main.ScreenPointToRay(Vector3(touch.position.x, touch.position.y));
        var hit : RaycastHit;
        if (Physics.Raycast(ray,hit))
        {...

and

    if( Input.GetButtonDown("Fire1") )
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray, out hit))
        {...

in performance or sensibility or is it just the same at the end...

thnx!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by skovacs1 · Nov 05, 2010 at 04:45 PM

The short answer is that they are essentially the same both in terms of speed and function although one is written explicitly for a touch interface while the other is written to access Unity's Input system. Depending on how they compile out for the target platform and hardware specific optimizations, comparing the two becomes more challenging.

Obvious differences:

  1. One is javascript and the other is C#. Both compile out more or less the same.
  2. One is checking against touch input while the other is checking against mouse input. Which might involve linking in more assemblies for the touch version, depending on how Unity is setup to compile this and what platform, etc. If they're both for a touch platform, then the Touch version is both clearer and should be more performant.
  3. As Eric5h5 says also, you'd want to check the touch phase for identical simulation.

Performance-wise, let's consider the instructions involved in the different steps of what you are doing:

if statement:

if(iPhoneInput.touchCount > 0) touch = iPhoneInput.GetTouch(0);

//To implement the same behaviour, you would need to use if(iPhoneInput.touchCount > 0) { touch = iPhoneInput.GetTouch(0); if(touch.phase == TouchPhase.Began) { //the rest... } }

//v.

if(Input.GetButtonDown("Fire1"))

//The touch version checks what is likely a value stored at the head of an //array already in memory. GetTouch(0) which probably has the overhead of //indexing an array of up to 5 Touch structs. Since this doesn't involve //allocating any temporary variables, it is looking at already allocated //memory and the lookup for this is quite fast if it is already loaded and //returning a reference to the object.

//For GetButtonDown, I'm not sure of the magic that Unity's input does, but //this is likely at minimum a lookup into a table of various inputs with some //function overhead and if the table is not already loaded, some extra paging. //At most, this is accessing a context interface to the hardware which is //usually slower. Then it returns a logical comparison against a value //returned. Depending on how/when/where they are stored, the table look-ups //could be comparable fast, but I as it is optimized for it, the touch version //probably outpaces the non-touch version.

//For the same functionality, the touch version would also have to access //.phase which is at least 5 bits for the enum and possibly an extra function //call+.

//The touch version should be fairly fast and if it proves false will likely //exit at less cost than the other version.

extra touch stuff:

pos = touch.position;

//.position involves accessing the memory that touch reference is pointing to //and returning a copy of the struct of 2 floats stored there - overhead the //non-touch version doesn't have both in storage and operations performed on //non-touch platforms, but for the same platform, mouse.position is essentially //doing the same thing and therefore has the same cost.

world coordinates:

//You perform two extra lookups here, creating temporary allocation of at the //least two floats if not the two of the entire struct when you already have //the same Vector2 of the position stored in the variable pos Vector3(touch.position.x, touch.position.y)

//You should try Vector3(pos.x, pos.y)

//Also, storing this Vector3 and only updating the x and y would cut the cost //of allocation in exchange for the cost of accessing the existing Vector3 //struct's floats, trading off allocation and assignment for just assignment.

//v.

Input.mousePosition

//Even in the minimal case, the touch version allocates a temporary Vector3. //Depending on the magic that Unity's input does, the input.mousePosition could //be doing something as simple as a look-up to some memory which may or may //not already be paged (with the associated overhead that entails) or it could //be actually accessing a context interface for the hardware which might be even //slower. Either way Unity likely allocates this into a temporary Vector3 also. //If the non-touch version doesn't allocate a new Vector3, this becomes overhead //that the touch version alone has, but either way the operations performed //(by you in the one case and by Unity in the other case) are generally the same //of allocating a Vector3, accessing two floating point numbers and copying them //into the Vector3. At this point, the two are likely equivalent, but the //non-touch version could be slower depending on what Input.mousePosition does.

They are probably fairly similar in terms of speed and performance, but since it should be optimized for it, despite a small amount of extra memory footprint involved in the operations, the total number of instructions and operational cost for the Touch version is likely marginally better.

Comment
Add comment · Show 1 · 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 headkitgames · Nov 08, 2010 at 02:38 PM 0
Share

thanx to both of you!

avatar image
1

Answer by Eric5h5 · Nov 05, 2010 at 04:29 PM

It would probably be more sensible to use touch events, since there's no guarantee that "Fire1" is set to mouse button 0 (which emulates a touch event). Although you'd need to check for TouchPhase.Began to have the same behavior as GetButtonDown.

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

No one has followed this question yet.

Related Questions

Iphone top down view touch drag control on Z axis? 1 Answer

Pokemon clone touch movement. 0 Answers

Using touch doesn't get the correct vector. 1 Answer

Block UnityGUI Hit 1 Answer

EZGUI Font that Support Currencies symbols 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