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 Dudi00 · Mar 22, 2014 at 01:45 PM · androidiosposition

Convert gameObject position on screen

Hello, I would like to create 6x6 board with toggleable sprites. They should be next to each other. On andEngine I base on anchorCenter which is (.5f,.5f) by default in every new object.

On andEngine I arrange it like this

 for (int i = 0; i < BOARD_ROW; i++) {
     for (int j = 0; j < BOARD_COLUMN; j++) {
         Letter rLetter = rollLetter ();
 
         float texWidth = rLetter.sprite.rect.width;
         float texHeight = rLetter.sprite.rect.height;
         Debug.Log (texWidth + "x" + texHeight); // 50 x 50
 
         //Here I'm not sure which convert I should use. 2.5f because it's 2.5 texture width (50*2.5) form layer/scene center. It would be .5 if I want 2x2 board.
 
         rLetter.position = Camera.main.ScreenToWorldPoint (new Vector2 ((-(texWidth * 2.5f) + (texWidth * j)), (texHeight * 2.5f) - (texHeight * i)));
         lettersOnScreen.Add (rLetter);
     }
 }

And I got some thing like this

alt text

alt text

I can live with (0,0) on down left corner but space between sprites is too big.

Letter class

 class Letter
 {
     private GameObject gameObject;
     public Vector2 position {
         get {
             return this.gameObject.transform.localPosition;
         }
         set {
             this.gameObject.transform.localPosition = value;
         }
     }
     public Sprite sprite {
         get;
         set;
     }
     public BoardPosition boardPosition {
         get;
         set;
     }
     public float likelihood {
         get;
         set;
     }
     public Range likelihoodRange {
         get;
         set;
     }
     public string code {
         get;
         set;
     }
     public bool clicked {
         get;
         set;
     }
 
     public Letter ()
     {
     }
 
     public Letter (GameObject go)
     {
         this.gameObject = go;
     }
         
 
     public Letter (Sprite sprite, string code, float likelihood, Range likelihoodRange)
     {
     
         this.sprite = sprite;
         this.code = code;
         this.likelihood = likelihood;
         this.likelihoodRange = likelihoodRange;
 
     }
 
 
     BoardPosition getBoardPosition ()
     {
         return boardPosition;
     }
 
     void setBoardPosition (BoardPosition boardPosition)
     {
         this.boardPosition = boardPosition;
     }
 
 }

Can you tell me what I'm doing wrong?

Comment
Add comment · Show 4
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 robertbu · Mar 22, 2014 at 02:37 PM 1
Share

Rather than walk through your code, let me make an alternate suggestion about how to do this layout.

For me when I doing something like this, I like to work in a unit grid in world space. I can greatly simplify things like hit detection and movement and in general is just easier to visualize when I'm writing code. So select the texture(s) you are using for the sprite and set the 'Pixels to Units' in the Inspector. For example if your textures are each 50 x 50, and you want them to completely fill the unit space, set the 'Pixels to Units' to 50. If you want a bit of space between them, then set it to something like 54. Then write your code so that it places things one unit apart in world space...no conversion necessary. Adjust the camera orthographic size or field of view as needed.

avatar image Dudi00 · Mar 22, 2014 at 05:13 PM 0
Share

Thanks... I go step ahead and after setting pixelToUnity to 50 I create prefab with all needed components :) Now it's peace of cake :)

avatar image victorbnunes · Apr 22, 2014 at 11:46 PM 0
Share

hi,

I'm trying to do something like that. But I do not understand what you tried to explain in the comment ...

/ / Here I'm not sure which convert Should I use. 2.5f because it's 2.5 texture width (50 * 2.5) form layer / scene center. It would be .5 if I want 2x2 board.  

Could you explain me better?

$$anonymous$$y game has the gameplay of this game, http://s.glbimg.com/jo/g1/f/original/2012/04/05/sem-titulo-2_1.jpg where you need to form a word with the letters. And meumaior problem is in the organization of words, because I need to organize them on the mobile screen.

avatar image robertbu · Apr 23, 2014 at 03:14 AM 0
Share

@victorbnunes - I'm not my suggestion above the the right one for your app. In general, you want to decide on a world size for your sprite. In my comment above, I was encouraging each tile to be one world unit wide, but you can select any width. I only encourage a one unit witch because it simplifies layout code and it can simplify hit testing. Say you wanted each time to be 1.5 units wide. Next you need your texture width. You say it is 50 pixels. So then if you wanted all the letter tiles flush against each other you would use 33.3333 pixels per unit for the sprite (50 / 1.5 = 33.33333).

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Unity3D Screensize remain same Position and Scale 0 Answers

Keep unity running when minimized on iOS and Android 2 Answers

Android & iOS Push Notifications 3 Answers

How to active mobile keyboard ? 1 Answer

iOS -Touch position unchanged, deltaPosition reported -1 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