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 Makkasu · Jun 14, 2015 at 01:45 PM · 2duitouch

How do I instantiate a UI image at touch position?

The game is top-down 2D with the camera fixed on the player as they move around the map.

We want to have an indicator of where the last touch position was, which seems to mean having the indicator as a prefab, instantiating it on finger press and then transforming it correctly:

Prefabs of UI elements are instantiated as normal using the Instantiate method. When setting the parent of the instantiated UI element, it’s recommended to do it using the Transform.SetParent method with the worldPositionStays parameter set to false.

There are 2 problems that I don't know how to tackle:

  1. Drawing the prefab image on the UI.

  2. Drawing it at the correct position (i.e. directly under the touch position in screen space).

Can anyone provide some C# example code to help achieve this? I have tried using the "Transform.SetParent method" (as advised here) but I'm not having much luck figuring out how to implement it.

Thanks

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

3 Replies

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

Answer by Makkasu · Jun 16, 2015 at 05:44 AM

Finally got that to work. Followed advice from @Xabrough and made the icon an image on my canvas, disabling it in the editor and then enabling it from script.

If anyone else gets stuck:

 public class icon : MonoBehaviour
 {
     public GameObject icon;
 
     public void createIcon (Vector2 touchPos) {
         icon.SetActive(true);
         icon.transform.position = new Vector3(touchPos.x, touchPos.y, 0);
     }
     
     public void destroyIcon () {
         newJoystick.SetActive(false);
     }
 }    


Then call these functions from the correct places in your input handling script, passing the position of the touch to createIcon.

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
avatar image
1

Answer by Xarbrough · Jun 14, 2015 at 11:55 PM

If you're using a UI image on a canvas, you could simply but it on there in the editor and then enable it when you press the mouse, then set its transform.position = Input.mousePosition and disable it after a few seconds or on MouseUp. Instantiating would only eat up cpu resources.

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 Makkasu · Jun 15, 2015 at 08:01 PM 0
Share

Followed this advice and will post my solution separately, thanks!

avatar image
0

Answer by IsouEU · Dec 28, 2021 at 12:29 PM

I just don't understand, when exactly .enable replaced instantiate ?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Ui sidescroller 2d touch ? 0 Answers

Changing multiple toggle states. 0 Answers

Touch Buttons 0 Answers

Unity UI button does not detect click 2 Answers

UI Image has no sprite definition 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