Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 adriandevera · Aug 09, 2018 at 02:17 AM · itweeneventsysteminterfacehandles

iTween in Handlers such as OnPointerDown, OnBeginDrag,OnDrag Etc

I have used iTween perfectly fine in Start() however when calling it in the interface handlers nothing happens. There are no errors or warnings.

What I aim to accomplish is moving an object with the mouse but on these different events animate the scale of the object. I have it done manually just transforming the local scale with no issues; however, I think itween would really spice it up a bit.

Any ideas? Below is a snippet of my code where

 public void OnDrag(PointerEventData eventData)
 {
     Vector3 scaleCardBeingDragged = new Vector3(0.5f, 0.5f, 1f); 
     state = State.DRAG;
     // this works as intended
     this.transform.localScale = scaleCardBeingDragged;
     // this does not work at all
     //iTween.ScaleTo(gameObject, iTween.Hash("scale", scaleCardBeingDragged, "easeType", "easeInOutExpo", "time", 0.3f));
     transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0) - touchOffsetPosition;
 }
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
2

Answer by JVene · Aug 09, 2018 at 06:30 AM

@adriandevera I must qualify by saying I've not used iTween. I'm an old hand in software engineering and related 3D rendering/physics/gaming work, but I am recent to Unity. That said, a few points about dragging should be considered with care.


As you know, Update and FixedUpdate are called periodically as a means of performing work in sync with important engine operations. Update will typically run at 60 calls per second, in line with the display sync (though it can be other speeds, even odd and unequal times when under load).


Mouse operation is not in sync with the engine. It does not run at the frame rate or the physics engine rate. It runs as an interrupt, way under the hood, which then feeds message traffic in real time. Drag is basically a stream of calls at a rate determined by the mouse itself, showing the stream of incoming coordinate positions as the mouse moves. Some mice can run 1200 of these messages per second, others 200, with any rate between depending on model, driver and possibly configuration. The operating system and mouse driver determine that rate, somewhat dictated by the hardware.


My reading is that iTween is expecting 1 call. It will then animate the transition over time at an engine oriented Update cycle until it is completed. A call to iTween like this should (I must assume by comparison to two decades of experience with other tools similar to it) start an animation cycle that operates frame by frame to perform the activity, scaling over 0.3 seconds in this case. OnDrag, as you've posted it, will stuff that with hundreds, possibly thousands of repeated calls to scale the object over 0.3 seconds. It is probably choking.


Like I said, I don't know iTween, but there's a chance it can't operate from OnDrag. If they accounted for calls from mouse messaging, perhaps I'm wrong, but if they don't account for the nature of OnDrag being out of cycle with Update, perhaps the call to iTween should be made in Update after OnDrag sets some bool or other state information to indicate the iTween scale should be requested. In any case, if iTween does not permit you to cancel an operation in progress, you'll have to consider how to stop the scaled effect when the mouse button ends the drag, even if the scale hasn't completed (that is, the mouse stops dragging within the 0.3 second time frame of the scale). Perhaps iTween at least lets you detect when the requested operation is still in progress, and when it is completed. That could be your hook to solve that particular UI timing issue.


In your working version (changing transform's scale) will also be repeating on every OnDrag call, but it will be of no consequence, it is the same on every call. Keep in mind, you're also creating new Vector3's at whatever rate the mouse updates when you create a new scaleCardToBeDragged. Since it is always the same, you shouldn't, even though it appears to be working. What you need is to check the state of drag initialization, do that once, set the transform, then avoid repeating all of that (creating of the Vector3 and re-assigning the transform) during each OnDrag. The transform.position update, on the other hand, is obviously more functional as it tracks the mouse. Even this is going to likely happen much more often and rapidly than Update can fire, which suggests all those new Vector3's should be avoided, and it is possible. You can drop updates to a sustained Vector3 member's x, y and z.


This is merely a foreshadow to what I'm about to suggest regarding the call to iTween, and to the update of the position. iTween should be called only once at drag initialization, not on every OnDrag message.


I'm recommending that the position updates you've exampled here are happening too frequently and creating too many Vector3's without good cause. There are too many possible solutions for me to pick one for you, but at least stop making new Vector3's just because they seem to be a local concept. This repeats too fast for that to be a good idea. Create a Vector3 member to be used by OnDrag, allocate a Vector3 for it, and update the x, y and z values directly in OnDrag.


None of this has dealt with the drag's end, where scale should be returned (and you may, then, want to iTween that). The hint is that you may do well to think in terms of OnDrag (or other mouse events that start and end the drag) as preparing "messages" to be discovered by Update. In other words, it may be better for iTween and all other design issues to not perform updates to the transform.position or the call to iTween within drag event messages, but in Update. Merely let the drag events drop 'commands' by setting bools and other information, like that sustained Vector3 I suggested for position. That way transform.position, which is a property that may call unknown operations when updated, is only changed at the Update cycle, and not at the OnDrag speed.


Likewise, since it appears you have some synchronization issues with iTween (if drag is ended in 0.1 seconds, what do you do?), you could let this disconnection between drag events and Update be a means of handling that. If, for example, iTween is scaling downward over 0.3 seconds, but the drag ends in 0.1 seconds, what would you expect would happen if another iTween call were made to upscale, or a quick change of transform.localScale happened, while that original iTween scaling downward were still in progress? I have no idea myself, but it sounds like a problem. That problem could be solved by letting Update make the calls to iTween based on state information, which can then be independent of the actual timing of the drag events (including drag start, drag end and drag move).

In this way you can allow Update to determine of the drag has been ended before the iTween animation completed, and schedule the return to normal scale appropriately.

Comment
Add comment · Show 4 · 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 adriandevera · Aug 09, 2018 at 05:34 PM 0
Share

Thank you for the reply. The portion Vector3 scaleCardBeingDragged = new Vector3(0.5f, 0.5f, 1f); is a property of the object and is allocated when it is created. I attempted to cleanly share the snippet but did so poorly. $$anonymous$$y apologizes. I have tried using a state system or flags to run the iTween as I too suspected it was calling too many times but still failed to do so. Perhaps you are correct in that I must stop the current animation prior to beginning a new one.

I also found an odd behavior when calling iTween.ScaleTo( ...) with the same parameters in Start() fails to do anytjhing whereas iTween.ScaleBy( ...)does.

avatar image JVene adriandevera · Aug 09, 2018 at 08:49 PM 0
Share

Consider using OnBeginDrag if you're having trouble using some state initialization to simulate a begin drag within OnDrag. If you still find ScaleBy isn't working inside OnDrag, see if you can manage to communicate the shift in drag state to Update, and let Update make the calls to iTween.

avatar image adriandevera JVene · Aug 11, 2018 at 04:31 AM 0
Share

I was not able to get iTween.ScaleTo working. On the bright side I did get iTweenScaleBy to work with a state machine. It took quite the effort. In the end I further looked into the documentation I found that there exist a iTween.ScaleUpdate. This in combination with a simple state machine in the update method I achieved a very low cost animation. ScaleUpdate internally sacrifices animation control with curves such as easingin and out which was fine with me. I will submit the answer seperately. Feel free to further critique the code. Thanks again!

If I do figure out how to do ScaleTo paired with a state machine I will post that as it allows callback functions and more control.

Show more comments
avatar image
0

Answer by adriandevera · Aug 11, 2018 at 04:33 AM

I found that this was the best solution i was able to come up with. Note to properly setup Interface Handlers please see the post by diegoleao: https://forum.unity.com/threads/ibegindraghandler-idraghandler-ienddraghandler-script-attached-to-sprite-object.294168/

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class TestScale : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
 {
 
     private Vector3 scaleUp = new Vector3(2.0f, 2.0f, 2.0f);
     private Vector3 scaleNormal = new Vector3(1.0f, 1.0f, 1.0f);
 
     public enum AnimationStates
     {
         NORMAL = 0,
         SCALING
     }
 
     [SerializeField]
     private AnimationStates animationState;
 
     public void OnPointerDown(PointerEventData pointerEventData)
     {
         animationState = AnimationStates.SCALING;
     }

     public void OnPointerUp(PointerEventData eventData)
     {
         animationState = AnimationStates.NORMAL;
     }
 
     void Update()
     {
         switch (animationState)
         {
             case AnimationStates.NORMAL:
                 iTween.ScaleUpdate(gameObject, scaleNormal, 0.3f);
                 break;
             case AnimationStates.SCALING:
                 iTween.ScaleUpdate(gameObject, scaleUp, 0.3f);
                 break;
         }
         
     }
 
 }
 
 
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

91 People are following this question.

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

Related Questions

Canvas-prefab elements are not interactable 2 Answers

How to call an implemented custom Interface 1 Answer

UI: Navigate 2 Players in the Same Menu 1 Answer

Event Trigger component vs Interfaces 0 Answers

Can I use EventSystems to receive PointerEvents anywhere on screen? 0 Answers


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