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 nathanc · Sep 23, 2014 at 07:49 AM · inputtouchspritesprime31

How do you use Unity Sprites with Prime31's Touchkit?

I can't seem to find any documentation or examples on the best practices for implementing Prime31's Touchkit with basic Sprites.

Currently I'm getting the sprite info such as width, height, originX and originY and then using this info when I setup the Recognizer. But I have a feeling I'm doing this wrong?

I basically hacked the DemoTwo scene that comes with Touchkit as follows;

Firstly I added a sprite to the scene named "pauseBtn"

Secondly I changed the DemoTwo.cs as follows;

 public class DemoTwo : MonoBehaviour
 {
     private VirtualControls _controls;
 
     void Start()
     {
         _controls = new VirtualControls();
         _controls.createDebugQuads();
     }
 
     void Update()
     {
         _controls.update();
     }
 
 }

and then I updated VirtualControls.cs as follows;

 public class VirtualControls
 {
     public bool pauseDown;
     private TKButtonRecognizer _pauseRecognizer;
     public TKRect pauseRect { get { return _pauseRecognizer.boundaryFrame.Value; } }
 
     public GameObject pauseBtn;
     float pauseBtnWidth;
     float pauseBtnHeight;
     float pauseBtnOriginX;
     float pauseBtnOriginY;
 
     void PauseBtnInfo(){
         pauseBtn = GameObject.Find("pauseBtn");
         pauseBtnWidth = pauseBtn.GetComponent<SpriteRenderer>().bounds.size.x;
         pauseBtnHeight = pauseBtn.GetComponent<SpriteRenderer>().bounds.size.y;
         pauseBtnOriginX = pauseBtn.GetComponent<SpriteRenderer>().bounds.center.x;
         pauseBtnOriginY = pauseBtn.GetComponent<SpriteRenderer>().bounds.center.y;
     }
 
     public VirtualControls()
     {
         PauseBtnInfo();
         setupRecognizers();
     }
 
     public void update()
     {
         // reset our state
         pauseDown = false;
 
         // first update our touches then use our recognizers to set our state
         TouchKit.updateTouches();
 
         pauseDown = _pauseRecognizer.state == TKGestureRecognizerState.RecognizedAndStillRecognizing;
     }
 
     public void createDebugQuads()
     {
         createQuadButton( pauseRect, Color.green );
     }
 
     void createQuadButton( TKRect rect, Color color )
     {
         color.a = 0.2f;
 
         // find the center point in Unity units
         var center = Camera.main.ScreenToWorldPoint( rect.center );
         center.z = 0;
 
         // create the quad button
         var button = GameObject.CreatePrimitive( PrimitiveType.Quad );
         button.transform.position = center;
         button.renderer.material.shader = Shader.Find( "Sprites/Default" );
         button.renderer.material.color = color;
 
         // scale the quad button accordingly
         button.transform.localScale = new Vector3
         (
             TouchKit.instance.pixelsToUnityUnitsMultiplier.x * rect.width,
             TouchKit.instance.pixelsToUnityUnitsMultiplier.y * rect.height
         );
     }
 
     void setupRecognizers()
     {
         _pauseRecognizer = new TKButtonRecognizer( new TKRect(TouchKit.instance.designTimeResolution.x - pauseBtnWidth*50f, TouchKit.instance.designTimeResolution.y - pauseBtnHeight*50f, pauseBtnWidth*50f, pauseBtnHeight*50f), 0f );
         TouchKit.addGestureRecognizer( _pauseRecognizer );
     }
 }

As you can see the pauseBtn data has to be "adjusted" a bit so that the debugQuad aligns with the sprite position. But if I move the sprite in the scene I then have to guess the co-ordinates again.

Mike mentions in the readme file that the TKButtonRecognizer class "has been designed to work with any sprite solution for button touch handling. This lets you keep your input totally separate from your rendering." so I'm thinking I'm doing this totally wrong?

If anyone has implemented Sprites with Touchkit sucessfully I would really appreciate any help or tips.

thanks in advance, Nathan

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by nathanc · Sep 23, 2014 at 11:05 AM

Ok so I played around with the code a bit more and realised that I need to convert Worldspace co-ords to Screenspace co-ords so updated the following code;

 void PauseBtnInfo(){
         pauseBtn = GameObject.Find("pauseBtn");
 
         float sizeX = pauseBtn.GetComponent<SpriteRenderer>().bounds.size.x;
         float sizeY = pauseBtn.GetComponent<SpriteRenderer>().bounds.size.y;
         float centerX = pauseBtn.GetComponent<SpriteRenderer>().bounds.center.x; 
         float centerY = pauseBtn.GetComponent<SpriteRenderer>().bounds.center.y;
 
         Vector3 bottomLeft = new Vector3(centerX - (sizeX/2), centerY - (sizeY/2));
         bottomLeft = Camera.main.WorldToScreenPoint(bottomLeft);
         Debug.Log ("bottomLeft = " + bottomLeft);
 
         Vector3 bottomRight = new Vector3(centerX + (sizeX/2), centerY - (sizeY/2));
         bottomRight = Camera.main.WorldToScreenPoint(bottomRight);
         Debug.Log ("bottomRight = " + bottomRight);
 
         Vector3 topLeft = new Vector3(centerX - (sizeX/2), centerY + (sizeY/2));
         topLeft = Camera.main.WorldToScreenPoint(topLeft);
         Debug.Log ("topLeft = " + topLeft);
 
         Vector3 topRight = new Vector3(centerX + (sizeX/2), centerY + (sizeY/2));
         topRight = Camera.main.WorldToScreenPoint(topRight);
         Debug.Log ("topRight = " + topRight);
 
         pauseBtnWidth = bottomRight.x - bottomLeft.x;
         Debug.Log("pauseBtnWidth = " + pauseBtnWidth);
 
         pauseBtnHeight = topLeft.y - bottomLeft.y;
         Debug.Log("pauseBtnHeight = " + pauseBtnHeight);
 
         pauseBtnOriginX = bottomLeft.x;
         Debug.Log("pauseBtnOriginX = " + pauseBtnOriginX);
 
         pauseBtnOriginY = bottomLeft.y;
         Debug.Log("pauseBtnOriginY = " + pauseBtnOriginY);
     
     }

and also

 void setupRecognizers()
     {
         _pauseRecognizer = new TKButtonRecognizer( new TKRect(pauseBtnOriginX, pauseBtnOriginY, pauseBtnWidth, pauseBtnHeight), 0f );
         TouchKit.addGestureRecognizer( _pauseRecognizer );
     }

now I can move the sprite around in the scene and the debugQuad and TKRect both update automatically :) Now I just need to clean up this code, turn it into a method that I can call and input a sprite and get it to spit out all those co-ords that I can pass to the TKRect.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

IOS object touch, collider 1 Answer

Pinching invokes SingleTouch 1 Answer

Problem getting touch input 0 Answers

index out of bounds 6 Answers

Mobile - button press while moving (First Person Controls) 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