Author Topic: Multi-resolution support with Virtual Controls Suite analog joystick  (Read 3069 times)

jdwieber

  • Guest
I'm using the Virtual Controls Suite (VCS) analog joystick and trying to have the joystick position itself based on the screen width.  I have the dual analog sticks anchored to the bottom left and bottom right of the screen with a relative offset.  This works fine for the background sprit, but for the foreground sprite, it does not allow it to move thus rendering the joystick useless.  I came up with a solution, but it required me writing a specialized script to position the front sprite based on the position of the anchor.  I pasted the script below.  I'm not happy with my solution and would like a better way to do this.  Can anyone suggest a better way?  I would much rather use only functionality present in NGUI and VCS.  The main problem with the attached script is that if I don't have it continuously update (it's a kludge), then it doesn't work.  This is because the VCS joystick script caches the original position of the front sprite (wherever it was placed in the editor) before my script adjusts the position.  Forcing a script execution order for these two scripts is dirty as it will require me to remember to do it for every project I use NGUI and VCS in.  I tried different combinations of anchors, panels, and sprites, but nothing worked correctly and yielded wrong positions, or distorted sprites.  This is an image of the current hierarchy for my joysticks:


The script below is a component of the Front sprite, and the Transform realtiveFrom is assigned the Anchor.  Anyone that likes the script may freely use it.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UIPositionRelative : MonoBehaviour
  5. {
  6.  
  7.     // I thought Awake would get it to work, but it's no better than Start
  8.     void Awake ()
  9.     {
  10.         m_transform = transform;
  11.         m_camera = NGUITools.FindCameraForLayer ( gameObject.layer );
  12.         SetPosition ();
  13.     }
  14.  
  15.     // using Update instead of LateUpdate to allow the analog joystick front to be draggable.
  16.    // LateUpdate locks it in place and the joystick front is not draggable
  17.     void Update ()
  18.     {
  19.         //In continuous mode it acts like an anchor, but relative to the
  20.         //given transform instead of the screen.
  21.         if ( continuous )
  22.         {
  23.             SetPosition ();
  24.         }
  25.     }
  26.  
  27.     private void SetPosition ()
  28.     {
  29.         Vector3 pos = m_camera.WorldToScreenPoint( relativeFrom.position );
  30.         //Debug.Log ( "Setting position relative to: " + pos.ToString () );
  31.         pos.x += offset.x;
  32.         pos.y += offset.y;
  33.         pos.z += offset.z;
  34.  
  35.         if ( m_camera.orthographic )
  36.         {
  37.             pos.x = Mathf.RoundToInt ( pos.x );
  38.             pos.y = Mathf.RoundToInt ( pos.y );
  39.         }
  40.  
  41.         pos = m_camera.ScreenToWorldPoint ( pos );
  42.  
  43.         if ( m_transform.position != pos )
  44.         {
  45.             m_transform.position = pos;
  46.             //Debug.Log ( "My position is: " + m_transform.position.ToString () );
  47.         }
  48.  
  49.     }
  50.  
  51.     //The transform of the object to position ourselves by
  52.     public Transform relativeFrom;
  53.     //An offset to adjust our position
  54.     public Vector3 offset = Vector3.zero;
  55.     //should I follow this object, or just position once
  56.     public bool continuous = false;
  57.  
  58.     //cache our transform
  59.     private Transform m_transform;
  60.  
  61.     //used to map points between screen and world
  62.     private Camera m_camera;
  63. }
  64.  

Thanks.